有时候
使用框架,或许没有直接操作数据库来的快,
或者说是使用框架太麻烦,有个直接操作数据库的工具类多好,故直接上干货,写下如下代码:
private static logger logger = logger.getlogger(dbhelper.class.getname());
/**
* 纯 java 式的连接 定义常量来存储配置
*/
public static string driver = null;
public static string url = null;
public static string user = null;
public static string pass = null;
connection conn = null;
preparedstatement pstmt = null;
statement stmt = null;
resultset rs = null;
// 获得数据连接信息.
static {
properties pops = new properties();
inputstream instream;
try {
instream = dbcommandutils.class.getresourceasstream(/jdbc.properties);
pops.load(instream);
} catch (filenotfoundexception e) {
logger.error(e.getmessage());
} catch (ioexception e) {
logger.error(e.getmessage());
}
url = pops.getproperty(datasource.url);
user = pops.getproperty(datasource.username);
pass = pops.getproperty(datasource.password);
driver = pops.getproperty(datasource.driverclassname);
}
/**
* 得到数据库连接
*/
public connection getconn() {
try {
if (driver == null || user == null || pass == null || url == null) {
properties pops = new properties();
inputstream instream;
try {
instream = dbcommandutils.class
.getresourceasstream(/jdbc.properties);
pops.load(instream);
} catch (filenotfoundexception e) {
logger.error(e.getmessage());
} catch (ioexception e) {
logger.error(e.getmessage());
}
url = pops.getproperty(datasource.url);
user = pops.getproperty(datasource.username);
pass = pops.getproperty(datasource.password);
driver = pops.getproperty(datasource.driverclassname);
}
// 获得链接.
class.forname(driver);
conn = (connection) drivermanager.getconnection(url, user, pass);
return conn;
} catch (exception e) {
logger.error(获取链接失败! + e.getlocalizedmessage());
return null;
}
}
/**
* 要执行的增 ,删 ,改 的操作,不执行查询 (注意参数的使用)
*/
public int executesql(string preparedsql, string[] param) {
int count = 0;
/**
* 执行的操作
*/
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
pstmt = conn.preparestatement(preparedsql);// 要执行的 sql 语句
if (param != null) {
for (int i = 0; i pstmt.setstring(1 + i, param[i]);// 为预编译sql设置参数
}
}
count = pstmt.executeupdate(); // 执行 sql 语句
} catch (sqlexception e) {
logger.error(e.getmessage()); // 处理sqlexception异常
return -1;
}
return count; // 返回结果
}
/**
* 要执行的增 ,删 ,改 的操作,不执行查询 (注意参数的使用)
*/
public int executesql(string preparedsql) {
int count = 0;
/**
* 执行的操作
*/
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
pstmt = conn.preparestatement(preparedsql);// 要执行的 sql 语句
count = pstmt.executeupdate(); // 执行 sql 语句
} catch (sqlexception e) {
logger.error(e.getmessage()); // 处理sqlexception异常
return -1;
}
return count; // 返回结果
}
/**
* 要执行的增 ,删 ,改 的操作,不执行查询 (注意参数的使用)
*/
public int[] executesqls(string[] sqls) {
int[] count = null;
/**
* 执行的操作
*/
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
stmt = conn.createstatement();
for (string sql : sqls) {
stmt.addbatch(sql);
}
count = stmt.executebatch(); // 执行 sql 语句
} catch (sqlexception e) {
logger.error(e.getmessage()); // 处理sqlexception异常
return null;
}
return count; // 返回结果
}
/**
* 要执行的复杂操作
*/
public boolean executeallsql(string preparedsql) {
boolean result = false;
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
pstmt = conn.preparestatement(preparedsql);// 要执行的 sql 语句
result = pstmt.execute();
} catch (sqlexception e) {
logger.error(e.getmessage()); // 处理sqlexception异常
}
return result; // 返回结果
}
/**
* 使用preparedstatement查询数据
*
* @param sql
* @param params
* 参数列表
* @return 结果集 不要关闭连接
*/
public resultset selectsql(string sql, string[] param) {
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
pstmt = conn.preparestatement(sql); // 执行sql语句
for (int i = 0; i pstmt.setstring(i + 1, param[i]);
}
rs = pstmt.executequery(); // 执行的结果
} catch (sqlexception e1) {
logger.error(e1.getmessage());
}
return rs;
}
/**
* 使用statement执行查询
*
* @param sql
* 执行的sql语句
* @return 不可关闭连接
*/
public resultset selectsql(string sql) {
try {
if (conn == null) {
conn = getconn(); // 获得连接
}
pstmt = conn.preparestatement(sql);
rs = pstmt.executequery();
} catch (sqlexception e1) {
logger.error(e1.getmessage());
}
return rs;
}
/**
* 关闭所有的接口 (注意括号中的参数)
*/
public void closeall() {
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (exception e) {
logger.error(e.getmessage());
}
}
// 判断是否关闭,要时没有关闭,就让它关闭,并给它附一空值(null),下同
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (sqlexception e) {
logger.error(e.getmessage()); // 异常处理
}
}
if (rs != null) {
try {
rs.close();
rs = null;
} catch (sqlexception e) {
logger.error(e.getmessage()); // 异常处理
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (sqlexception e) {
logger.error(e.getmessage()); // 异常处理
}
}
}
/**
* 检查数据库连接
*
* @param manager
* @return true:无法连接;false:正常
*/
public boolean checkcon(dbcommandutils manager) {
boolean result = false;
try {
result = getconn().isclosed();
} catch (sqlexception e) {
logger.error(e.getmessage());
}
return result;
}
/**
* 编写测试类来进行对数据库的检验
*/
public static void main(string[] args) {
dbcommandutils manager = new dbcommandutils();
try {
system.out.println(manager.getconn().isclosed());
} catch (sqlexception e) {
logger.error(e.getmessage()); // 抛出异常
}
}
这个玩意就可以直接拿来用了,其实还是很不错的玩意。。。
自然是没有使用关系型框架舒服了。
不过: 有了这个有时候不用框架也是比较舒服的。
