Java 连接数据库的工具包
来源:互联网 发布:网络艺术名字大全 编辑:程序博客网 时间:2024/06/05 03:11
import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;public class DBUtil{ private static final String DBDRIVER="com.mysql.jdbc.Driver";//驱动类类名 private static final String DBNAME="test";//数据库名 private static final String DBURL="jdbc:mysql://localhost:3306/"+DBNAME;//连接URL private static final String DBUSER="root";//数据库用户名 private static final String DBPASSWORD="root";//数据库密码 private static Connection conn=null; private static PreparedStatement ps=null; private static ResultSet rs=null; //获取数据库连接 public static Connection getConnection(){ try{ Class.forName(DBDRIVER);//注册驱动 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//获得连接对象 }catch(ClassNotFoundException e){//捕获驱动类无法找到异常 e.printStackTrace(); }catch(SQLException e){//捕获SQL异常 e.printStackTrace(); } return conn; } //查询数据 public ResultSet select(String sql)throws Exception{ try{ conn=getConnection(); ps=conn.prepareStatement(sql); rs=ps.executeQuery(sql); return rs; }catch(SQLException sqle){ throw new SQLException("select data Exception: "+sqle.getMessage()); }catch(Exception e){ throw new Exception("System error: "+e.getMessage()); } } //插入数据 public int insert(String sql)throws Exception{ int num=0;//计数 try{ conn=getConnection(); ps=conn.prepareStatement(sql); num=ps.executeUpdate(); }catch(SQLException sqle){ throw new SQLException("insert data Exception: "+sqle.getMessage()); }finally{ try{ if(ps!=null){ ps.close(); } }catch(Exception e){ throw new Exception("ps close exception: "+e.getMessage()); } try{ if(conn!=null){ conn.close(); } }catch(Exception e){ throw new Exception("conn close exception: "+e.getMessage()); } } return num; } //删除数据 public int delete(String sql)throws Exception{ int num=0;//计数 try{ conn=getConnection(); ps=conn.prepareStatement(sql); num=ps.executeUpdate(); }catch(SQLException sqle){ throw new SQLException("delete data Exception: "+sqle.getMessage()); }finally{ try{ if(ps!=null){ ps.close(); } }catch(Exception e){ throw new Exception("ps close Exception: "+e.getMessage()); } try{ if(conn!=null){ conn.close(); } }catch(Exception e){ throw new Exception("conn close Exception: "+e.getMessage()); } } return num; } //修改数据 public int update(String sql)throws Exception{ int num=0;//计数 try{ conn=getConnection(); ps=conn.prepareStatement(sql); num=ps.executeUpdate(); }catch(SQLException sqle){ throw new SQLException("update data Exception: "+sqle.getMessage()); }finally{ try{ if(ps!=null){ ps.close(); } }catch(Exception e){ throw new Exception("ps close Exception: "+e.getMessage()); } try{ if(conn!=null){ conn.close(); } }catch(Exception e){ throw new Exception("conn close Excepiton: "+e.getMessage()); } } return num; }}
0 0
- Java 连接数据库的工具包
- 【java基础:JDBC】基于DPCP连接池工具包和DBUtils工具包的数据库改查Demonstration
- JDBC连接数据库的工具包代码
- java连接库的JDBC工具包
- Java之数据库工具包DBUtils和连接池
- java--DBUtils数据库操作工具包
- Commons DbUtils -->Java 中处理数据库的工具包下载网站
- Java链接数据库,以及做添删查改所用的工具包--源代码
- 我的Java工具包
- 数据库的连接 java
- 工具包sharepage.java的解读
- 个人封装的Java工具包
- 我的Java链接数据库,以及做添删查改所用的工具包--源代码
- java连接数据库常用的连接字符串
- Java连接常见数据库的连接字符串
- java连接常见数据库的连接字符串
- java连接常见数据库的连接字符串
- java连接常见数据库的连接字符串
- phpstorm使用总结(一)
- IPO与上市的关系?
- github pages gh-pages 分支图片链接失效
- ogre 引擎 框架追踪 第四章 资源加载之资源组初始化
- 今天的世界,未来的世界会不会相同
- Java 连接数据库的工具包
- 画图理解软件工程-UML统一建模语言-类图和时序图(四)
- 二值分类模型的评价指标
- websphere报表应用挂起原因及解决方法
- Error:Execution failed for task ':app:buildInfoDebugLoader'. Exception while doing past iteration b
- 堆排序
- PhoneGAP如何禁止手机横屏竖屏旋转,如何限制应用只能竖屏
- 按两个字段排序
- Html5中input标记的type属性