1.JDBC工具类
import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; /** * 设计JDBC工具类 */ public class JDBCUtil { private static String driver; private static String url; private static String user; private static String pwd; /** * 只需要读取一次读取配置文件jdbc.properties */ static { try { //1.创建Properties集合类 Properties prop = new Properties(); //获取src路径下的文件的方式:ClassLoader类加载器 ClassLoader loader = JDBCUtil.class.getClassLoader(); URL propUrl = loader.getResource("jdbc.properties"); String path = propUrl.getPath(); System.out.println(path); //2.加载文件 prop.load(new FileReader(path)); //3.获取值 driver = prop.getProperty("driver"); url = prop.getProperty("url"); user = prop.getProperty("user"); pwd = prop.getProperty("pwd"); //4.注册驱动 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获取连接 * * @return 连接对象 */ public static Connection getConnection() { try { return DriverManager.getConnection(url, user, pwd); } catch (SQLException e) { e.printStackTrace(); return null; } } /** * 释放资源 * * @param stmt Statement对象 * @param conn Connection对象 */ public static void close(Statement stmt, Connection conn) { close(null, stmt, conn); } /** * 释放资源 * * @param rs ResultSet对象 * @param rs ResultSet对象 * @param stmt Statement对象 * @param conn Connection对象 */ public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/数据库名称 user=用户名 pwd=密码
2.Druid连接池工具类
import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.sql.*; import java.util.Properties; /** * 设计Druid连接池工具类 */ public class DruidUtil { //定义成员变量DataSource private static DataSource ds; /** * 对DataSource初始化赋值 */ static { try { //1.加载配置文件 Properties prop = new Properties(); InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); //2.赋值 ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取连接 * * @return 连接对象 */ public static Connection getConnection() { try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); return null; } } /** * 释放资源 * * @param stmt Statement对象 * @param conn Connection对象 */ public static void close(Statement stmt, Connection conn) { close(null, stmt, conn); } /** * 释放资源 * * @param rs ResultSet对象 * @param rs ResultSet对象 * @param stmt Statement对象 * @param conn Connection对象 */ public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close();//归还连接 } catch (SQLException e) { e.printStackTrace(); } } } }
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/数据库名称 username=用户名 password=密码 initialSize=5 maxActive=10 maxWait=3000
JDBC很容易踩坑,踩过两个坑了:
1.配置文件中参数问题:https://www.cnblogs.com/deng3/p/12752477.html
2.使用Druid连接池,配置文件中的参数名称:https://www.cnblogs.com/deng3/p/12755430.html