加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JDBC的工具类的抽取

发布时间:2020-12-15 07:25:39 所属栏目:Java 来源:网络整理
导读:1.1.1 抽取一个JDBC的工具类 因为传统JDBC的开发,注册驱动,获得连接,释放资源这些代码都是重复编写的。所以可以将重复的代码提取到一个类中来完成。 /**?* JDBC的工具类?* @author jt?*?*/public class JDBCUtils {????????private static final String d
1.1.1 抽取一个JDBC的工具类

因为传统JDBC的开发,注册驱动,获得连接,释放资源这些代码都是重复编写的。所以可以将重复的代码提取到一个类中来完成。

/**

?* JDBC的工具类

?* @author jt

?*

?*/

public class JDBCUtils {

????????private static final String driverClassName;

????????private static final String url;

????????private static final String username;

????????private static final String password;

?????????

????????static{

????????????????driverClassName="com.mysql.jdbc.Driver";

????????????????url="jdbc:mysql:///web_test3";

????????????????username="root";

????????????????password="abc";

????????}

?

????????/**

?????????* 注册驱动的方法

?????????*/

????????public static void loadDriver(){

????????????????try {

????????????????????????Class.forName(driverClassName);

????????????????} catch (ClassNotFoundException e) {

????????????????????????e.printStackTrace();

????????????????}

????????}

?????????

????????/**

?????????* 获得连接的方法

?????????*/

????????public static Connection getConnection(){

????????????????Connection conn = null;

????????????????try{

????????????????????????// 将驱动一并注册:

????????????????????????loadDriver();

????????????????????????// 获得连接

????????????????????????conn = DriverManager.getConnection(url,username,password);

????????????????}catch(Exception e){

????????????????????????e.printStackTrace();

????????????????}

????????????????return conn;

????????}

?????????

????????/**

?????????* 释放资源的方法

?????????*/

????????public static void release(Statement stmt,Connection conn){

????????????????if(stmt != null){

????????????????????????try {

????????????????????????????????stmt.close();

????????????????????????} catch (SQLException e) {

????????????????????????????????e.printStackTrace();

????????????????????????}

?????????????????????????

????????????????????????stmt = null;

????????????????}

????????????????if(conn != null){

????????????????????????try {

????????????????????????????????conn.close();

????????????????????????} catch (SQLException e) {

????????????????????????????????e.printStackTrace();

????????????????????????}

????????????????????????conn = null;

????????????????}

????????}

?????????

????????public static void release(ResultSet rs,Statement stmt,Connection conn){

????????????????// 资源释放:

????????????????if(rs != null){

????????????????????????try {

????????????????????????????????rs.close();

????????????????????????} catch (SQLException e) {

????????????????????????????????e.printStackTrace();

????????????????????????}

?????????????????????????

????????????????????????rs = null;

????????????????}

????????????????if(stmt != null){

????????????????????????try {

????????????????????????????????stmt.close();

????????????????????????} catch (SQLException e) {

????????????????????????????????e.printStackTrace();

????????????????????????}

?????????????????????????

????????????????????????stmt = null;

????????????????}

????????????????if(conn != null){

????????????????????????try {

????????????????????????????????conn.close();

????????????????????????} catch (SQLException e) {

????????????????????????????????e.printStackTrace();

????????????????????????}

????????????????????????conn = null;

????????????????}

????????}

}

1.1.2 测试工具类

@Test

????????/**

?????????* 查询操作:使用工具类

?????????*/

????????public void demo1(){

????????????????Connection conn = null;

????????????????Statement stmt = null;

????????????????ResultSet rs = null;

????????????????try{

????????????????????????// 获得连接:

????????????????????????conn = JDBCUtils.getConnection();

????????????????????????// 创建执行SQL语句的对象:

????????????????????????stmt = conn.createStatement();

????????????????????????// 编写SQL:

????????????????????????String sql = "select * from user";

????????????????????????// 执行查询:

????????????????????????rs = stmt.executeQuery(sql);

????????????????????????// 遍历结果集:

????????????????????????while(rs.next()){

????????????????????????????????System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));

????????????????????????}

????????????????}catch(Exception e){

????????????????????????e.printStackTrace();

????????????????}finally{

????????????????????????// 释放资源:

????????????????????????JDBCUtils.release(rs,stmt,conn);

????????????????}

????????}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读