JDBCUtils 的通用代码
来源:互联网 发布:管家婆无法连接数据库 编辑:程序博客网 时间:2024/05/18 01:47
刚接触javaweb开发的人,要想和数据库打交道,都绕不开JDBCUtils的代码编写,下面分享一个比较好的代码风格的JDBCUtils类的编写。
public class JDBCUtils {
private final static BasicDataSource ds;
static{
Properties pro=new Properties();
try {
pro.load(JDBCUtils.class.getResourceAsStream("/dbcp2.properties"));
ds=BasicDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException{//得到连接
return ds.getConnection();
}
public static void closeQuietly(AutoCloseable c){//安静地关闭资源,包括connection,statement,resultSet
if(c!=null){
try {
c.close();
} catch (Exception e) {
}
}
}
public static void rollBack(Connection conn){//事务回滚
if(conn!=null){
try {
conn.rollback();
} catch (SQLException e) {
}
}
}
public static void closeAll(ResultSet rs){
if(rs!=null){
try {
Statement stmt=rs.getStatement();
Connection conn=stmt.getConnection();
closeQuietly(rs);
closeQuietly(stmt);
closeQuietly(conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public static void closeStatementAndConnection(Statement stmt){
if(stmt!=null){
try {
Connection conn=stmt.getConnection();
closeQuietly(stmt);
closeQuietly(conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* 执行非查询代码
* @param sql
* @param params
* @throws SQLException
*/
public static void executeNonQuery(String sql, Object... params) throws SQLException{
Connection conn=null;
try {
conn=ds.getConnection();
executeNonQuery(conn, sql, params);
} finally{
closeQuietly(conn);
}
}
public static void executeNonQuery(Connection conn,String sql, Object... params) throws SQLException{
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1, params[i]);
}
ps.execute();
} finally{
closeQuietly(ps);
}
}
public static ResultSet executeQuery(String sql,Object... params) throws SQLException{
Connection conn=null;
try{
conn=ds.getConnection();
return executeQuery(conn, sql, params);
}catch(SQLException e){
closeQuietly(conn);
throw e;
}
}
public static ResultSet executeQuery(Connection conn,String sql,Object... params) throws SQLException{
PreparedStatement ps=null;
try{
ps=conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1, params[i]);
}
return ps.executeQuery();
}catch(SQLException e){
closeQuietly(ps);
throw e;
}
}
/**
* 得到最后插入的数据的id
* @param sql
* @param params
* @return
* @throws SQLException
*/
public static long executeInsert(String sql,Object... params) throws SQLException{
Connection conn=null;
try {
conn=ds.getConnection();
return executeInsert(conn, sql, params);
} finally{
closeQuietly(conn);
}
}
public static long executeInsert(Connection conn,String sql,Object... params) throws SQLException{
PreparedStatement ps=null;
PreparedStatement psForInsertId=null;
ResultSet rs=null;
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1, params[i]);
}
ps.execute();
psForInsertId=conn.prepareStatement("select last_insert_id()");//需要使用同一个连接
rs=psForInsertId.executeQuery();
if(rs.next()){
return rs.getLong(1);
}else{
throw new RuntimeException("没有找到id字段");
}
} finally{
closeQuietly(ps);
closeQuietly(rs);
closeQuietly(psForInsertId);
}
}
public static Object executeSingle(String sql,Object... params) throws SQLException{//适用于查询结果只有一行一列,比如说count(*)
Connection conn=null;
try{
conn=ds.getConnection();
return executeSingle(conn, sql, params);
}
finally{
closeQuietly(conn);
}
}
public static Object executeSingle(Connection conn,String sql,Object... params) throws SQLException{
PreparedStatement ps=null;
ResultSet rs=null;
try{
ps=conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
rs= ps.executeQuery();
if(rs.next()){
return rs.getObject(1);
}else{
return null;
}
}
finally{
closeQuietly(rs);
closeQuietly(ps);
}
}
}
代码当中我还使用了dbcp数据库连接池,从而提高系统的性能。数据库是mysql数据库。为了使代码能够正常运行需要引入3个jar包,jar包下载地址:http://download.csdn.net/detail/tonghangli/9859580
dbcp2.properties文件内容如下:(只要放在src下)
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名字?seUnicode=true&characterEncoding=UTF8
username=数据库账号
password=数据库密码
好了,本次的分享到此就结束了,赶快去happy地试一试吧
- JDBCUtils 的通用代码
- JDBC:编写通用的 JDBCUtils工具类
- JdbcUtils工具的抽取
- JdbcUtils
- jdbcUtils
- JdbcUtils
- JDBCUTils
- JDBCUtils
- JDBCUtils
- JDBCUtils
- 使用工具类来简化代码JDBCUtils
- 使用工具类来简化代码JDBCUtils
- 使用工具类来简化代码JDBCUtils
- 能做事务的JdbcUtils工具
- JDBC学习之JDBCUtils的编写
- 数据库的通用访问代码
- TreeView控件的通用代码
- 通用的表单验证代码
- Data Binding
- 哈夫模型-arcgis
- wifi 简单实习
- 解决 Source Insight中的parse too complex问题
- ES6焦点图(已实现)
- JDBCUtils 的通用代码
- java web在linux环境下上传文件出现的一些问题
- Hadoop DataNode 无法连接到主机NameNode
- LeetCode 41. First Missing Positive
- |UVA 11729|贪心|Commando War
- java核心编程笔记
- hosts和DNS等网络设置
- shell中变量的定义
- telnet不是内部或外部命令怎么办