Java培训学习课程编写工具类JDBCUtils

编写工具类JDBCUtils

package com.atguigu.utils;

 

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

 

/*

 * 工具类:

 * 1、注册驱动:只要运行一次

 * 2、获取连接

 * 3、关闭资源

 */

public class JDBCUtils {

         private static String drivername;

         private static String url;

         private static String user;

         private static String password;

         private static Properties pro = new Properties();

         static{

                  try {

                          //加载,读取jdbc.properties配置的信息

                          //pro.load的作用是把jdbc.properties文件中配置的信息,一一put到pro这个map中

                          pro.load(ClassLoader.getSystemClassLoader().getResourceAsStream(“jdbc.properties”));

 

//                       drivername = pro.getProperty(“key”)

                          drivername = pro.getProperty(“drivername”);

                          url = pro.getProperty(“url”);

                          user = pro.getProperty(“user”);

                          password = pro.getProperty(“password”);

 

                          //注册驱动,加载驱动

                          Class.forName(drivername);

                  } catch (ClassNotFoundException e) {

                          e.printStackTrace();

                  } catch (IOException e) {

                          e.printStackTrace();

                  }

         }

 

         public static Connection getConnection()throws SQLException{

                  Connection conn = DriverManager.getConnection(url, user, password);

                  return conn;

         }

 

         public static void closeQuietly(Connection conn){

                  try {

                          if(conn!=null){

                                   conn.close();

                          }

                  } catch (SQLException e) {

                          e.printStackTrace();

                  }

         }

 

         public static void closeQuietly(Statement st){

                  try {

                          if(st!=null){

                                   st.close();

                          }

                  } catch (SQLException e) {

                          e.printStackTrace();

                  }

         }

 

         public static void closeQuietly(ResultSet rs){

                  try {

                          if(rs!=null){

                                   rs.close();

                          }

                  } catch (SQLException e) {

                          e.printStackTrace();

                  }

         }

 

         public static void closeQuietly(Statement st,Connection conn){

                  closeQuietly(st);

                  closeQuietly(conn);

         }

 

         public static void closeQuietly(ResultSet rs,Statement st,Connection conn){

                  closeQuietly(rs);

                  closeQuietly(st);

                  closeQuietly(conn);

         }

}

想要了解跟多关于Java培训机构课程内容欢迎关注尚硅谷Java培训机构,尚硅谷除了这些技术文章外还有免费的高质量Java培训机构课程视频供广大学员下载学习


上一篇:
下一篇:
关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园综合楼6层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)