java jdbc操作数据库简单基本连接实现/**
* 封装insert(),update(),delete()方法对数据库的操作
* @param sql
* @param params
* @return result 返回影响的行数
* @throws Exception
*/
public static int MySqlExecuteUpdate(String sql,Object...params)throws Exception{
Connection conn = TransAnctionFilter.Transcon.get();//从本地线程里取出conn
//Connection conn = getConn();
PreparedStatement pStatement = conn.prepareStatement(sql);
for(int i = 0;i < params.length;i++){
pStatement.setObject(i + 1, params[i]);
}
int result = pStatement.executeUpdate();
close(null, pStatement, null);
return result;
}
/**
* 封装select()方法对数据库的操作
* @param sql
* @param params
* @return 返回查询的结果集
* @throws Exception
*/
public static ResultSet MySqlExecuteSelect(String sql,Object...params)throws Exception{
Connection conn = TransAnctionFilter.Transcon.get();//从本地线程里取出conn
//Connection conn = getConn();
PreparedStatement pStatement = conn.prepareStatement(sql);
for(int i = 0;i < params.length;i++){
pStatement.setObject(i + 1, params[i]);
}
ResultSet rSet = pStatement.executeQuery();
return rSet;
}
由AXIN编辑于2014-2-13 9:39:34
由最代码官方编辑于2014-10-14 9:54:17