目录
一 、 硬编码方式
在包 cn.benz.utils; 下创建类 JDBCUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package cn.benz.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCUtils { //JDBC链接所需参数 private static final String driver = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql://localhost:3306/java_we?useUnicode=true&characterEncoding=utf-8"; private static final String dbusername = "root"; private static final String dbpassword = "root"; private static Connection conn = null; //自动执行 static{ try { Class.forName(driver); conn = DriverManager.getConnection(url,dbusername,dbpassword); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //将获得的数据库与java的链接返回(返回的类型为Connection) public static Connection getConnection(){ return conn; } /** * 释放数据库资源 * @param conn * @param pstmt * @param rs */ public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs){ if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
在包 cn.benz.test; 下创建类 TestUtils 调用工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package cn.benz.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.benz.utils.JDBCUtils; public class TestUtils { @Test public void testUtils(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { //1. 获取链接 conn = JDBCUtils.getConnection(); //2. 编写SQL语句 String sql = "select * from user where id = ?"; //3. 获取 预处理 对象 pstmt = conn.prepareStatement(sql); //4. 设定参数 pstmt.setInt(1, 1); //5. 执行操作 rs = pstmt.executeQuery(); //6. 处理结果集 while (rs.next()) { System.out.println(rs.getString(2)+"--------"+rs.getString("pw")); } } catch (SQLException e) { e.printStackTrace(); }finally{ //7. 释放资源 JDBCUtils.release(conn, pstmt, rs); } } } |
此方式数据库参数都已经写死,更改不方便,需要到源码处更改
二、使用 properties 配置文件,ResourceBundle对象
开发中获得链接的4个参数(驱动、URL、用户名、密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需修改配置文件即可。
通常情况下,我们习惯使用properties文件,此文件我们将作如下要求:
- 文件位置:任意,建议src下
- 文件名称:任意,但扩展名必须为properties
- 文件内容:一行一组数据,格式是”key = value”
a) key 命名自定义,如果是多个单词,习惯使用点分隔。例如:jdbc.driver。
b) value 值不支持中文,如果需要使用非英文字符,将进行Unicode转换。
创建properties文件
创建名为db.properties文件,格式如下
1 2 3 4 |
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java_we?useUnicode=true&characterEncoding=utf8 dbuser=root dbpassword=root |
加载配置文件:ResourceBundle对象
使用JDK提供的工具类ResourceBundle加载properties文件,ResourceBundle提供getBundle()方法用于只提供properties文件即可,之后使用getString(key),通过key获得value的值。
示例如下:创建一个工具类JDBCUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package cn.benz.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ResourceBundle; public class JDBCUtils { private static final String driver; private static final String url ; private static final String dbuser; private static final String dbpassword ; private static Connection conn = null; /** * 静态代码块加载配置信息 */ static{ //使用JDK提供的工具类ResourceBundle加载properties文件 ResourceBundle bundle = ResourceBundle.getBundle("db");//properties文件名为db //使用getString方法获取对应的字段 driver = bundle.getString("driver"); url = bundle.getString("url"); dbuser = bundle.getString("dbuser"); dbpassword = bundle.getString("dbpassword"); } //将获得的数据库与java的链接返回(返回的类型为Connection) public static Connection getConnection(){ try { Class.forName(driver); conn = DriverManager.getConnection(url,dbuser,dbpassword); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 释放数据库资源 * @param conn * @param pstmt * @param rs */ public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs){ if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
三、使用 properties 配置文件,properties对象(可选)
对于properties文件处理,开发中也会使用Properties对象进行,在这里,我们将采用流的形式加载properties文件,然后使用Properties对象进行处理。
示例如下:创建一个工具类JDBCUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
package cn.benz.utils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class JDBCUtils { private static String driver; private static String url ; private static String dbuser; private static String dbpassword ; private static Connection conn = null; /** * 静态代码块加载配置信息 */ static{ try { //1. 通过当前类(JDBCUtils.class)获取类的加载器(getClassLoader()) ClassLoader classLoader = JDBCUtils.class.getClassLoader(); //2. 通过类加载的一个方法(getResourceAsStream())获得一个输入流 InputStream is = classLoader.getResourceAsStream("db.properties");//文件名必须写全称 //3. 创建一个properties对象 Properties props = new Properties(); //4. 加载输入流 props.load(is);//抛异常 //5. 使用 properties对象 的方法 getProperty() 获取相关参数的值 driver = props.getProperty("driver"); url = props.getProperty("url"); dbuser = props.getProperty("dbuser"); dbpassword = props.getProperty("dbpassword"); } catch (IOException e) { e.printStackTrace(); } } //将获得的数据库与java的链接返回(返回的类型为Connection) public static Connection getConnection(){ try { Class.forName(driver); conn = DriverManager.getConnection(url,dbuser,dbpassword); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 释放数据库资源 * @param conn * @param pstmt * @param rs */ public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs){ if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
四、最后
硬编码方式不方便修改,于是我们采用properties配置文件。对于properties文件的处理,有两种方式,一个是使用ResourceBundle对象直接获取配置文件,另一个以流的形式使用Properties对象读取配置文件。Properties对象比ResourceBund对象多了配置文件流处理这一步,相对比较繁琐一些,所以,一般使用ResourceBundle对象处理properties配置文件。