经过以前的案例回顾,不难发现,有不少的代码操做是重复的,好比“获取连接”和“释放资源”等,未来在增删改查中常常遇到,开发中遇到这种状况,将采用工具类的方法进行抽取,从而达到代码的重复利用。java
此处使用V1版本,以后还有替他版本。mysql
获取连接git
/** * 获取链接方法 * * @return */ public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "root"); } catch (Exception e) { e.printStackTrace(); } return conn; }
释放资源 github
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
package cn.jayvee.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 提供获取连接和资源的方法 * @author Jayvee * @date 2019-5-12 下午4:22:24 * @version V1.0 * */ public class JDBCUtils_V1 { /** * 获取连接方法 * @return */ public static Connection getConnection(){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest","root","123456"); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt!=null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.jayvee.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.jayvee.jdbc.JDBCUtils_V1; /** * 测试工具类 * * @author Jayvee * @date 2019-5-12 下午4:38:54 * @version V1.0 */ public class TestUtils { /** * 根据id查询用户信息 */ @Test public void testFindUserById() { Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { // 1.获取连接 conn = JDBCUtils_V1.getConnection(); // 2.编写sql语句 String sql = "select * from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数(1表示第一个问号,2表示第一个问号的值是2) pstmt.setInt(1, 2); // 5.执行查询操做 rs = pstmt.executeQuery(); // 6.处理结果集 while (rs.next()) { // rs.getString(2)表示获取这条数据的第二列的数据 // rs.getString("age")表示获取这条数据中age字段的之 System.out.print(rs.getString(2) + "----" + rs.getString("age")); } // 释放资源放在这里不可行! } catch (SQLException e) { e.printStackTrace(); } finally { // 7.释放资源 JDBCUtils_V1.release(conn, pstmt, rs); } } }
开发中得到连接的四个参数(驱动、URL、用户名、密码)一般都存在在配置文件中,方便后期维护,程序若是须要更换数据库,只须要修改配置文件便可。web
一般状况下,咱们习惯使用 properties 文件,此文件咱们将做以下要求:sql
1. 文件位置:任意,建议src下。数据库
2. 文件名称:任意,扩展名为 properties 。ide
3. 文件内容:一行一组数据,格式是“ key = value ”。工具
a)key 命名自定义,不能带有空格,若是是多个单词,习惯使用点分隔。例如“ jdbc.driver ”测试
b)value值不支持中文,不能带有空格,若是须要使用非英文字符,将进行 Unicode 转换。
在 src 目录下建立一个 db.properties 配置文件。
文件内编写以下配置(具体状况具体改)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf8 username=root password=123456
咱们在V2版本中使用JDK提供的工具类 ResourceBundle 加载 properties 文件,ResourceBundle 提供 getBundle() 方法用于只提供 properties 文件便可,以后使用 getString(key) 经过 key 得到 value的值。
package cn.jayvee.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ResourceBundle; /** * 提供获取连接和资源的方法 * @author Jayvee * @date 2019-5-12 下午4:22:24 * @version V1.0 * */ public class JDBCUtils_V2 { private static String driver; private static String url; private static String username; private static String password; /** * 静态代码块加载配置文件信息 */ static{ // 加载配置文件 ResourceBundle bundle = ResourceBundle.getBundle("db"); driver = bundle.getString("driver"); url = bundle.getString("url"); username = bundle.getString("username"); password = bundle.getString("password"); } /** * 获取连接方法 * @return */ public static Connection getConnection(){ Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url,username,password); } catch (Exception e) { 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) { e.printStackTrace(); } } if (pstmt!=null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.jayvee.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.management.RuntimeErrorException; import org.junit.Test; import cn.jayvee.jdbc.JDBCUtils_V1; import cn.jayvee.jdbc.JDBCUtils_V2; /** * 测试工具类 * * @author Jayvee * @date 2019-5-12 下午4:38:54 * @version V1.0 */ public class TestUtils { /** * 添加用户信息方法 */ @Test public void testAdd(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V2.getConnection(); // 2.编写 sql 语句 String sql = "insert into student values(null,?,?)"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setString(1, "wangjiawei"); pstmt.setString(2, "25"); // 5.执行插入操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("添加成功!"); }else{ System.out.println("添加失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V2.release(conn, pstmt, null); } } /** * 根据id查询用户信息 */ public void testFindUserById() { Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { // 1.获取连接 conn = JDBCUtils_V1.getConnection(); // 2.编写sql语句 String sql = "select * from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数(1表示第一个问号,2表示第一个问号的值是2) pstmt.setInt(1, 2); // 5.执行查询操做 rs = pstmt.executeQuery(); // 6.处理结果集 while (rs.next()) { // rs.getString(2)表示获取这条数据的第二列的数据 // rs.getString("age")表示获取这条数据中age字段的之 System.out.print(rs.getString(2) + "----" + rs.getString("age")); } // 释放资源放在这里不可行! } catch (SQLException e) { e.printStackTrace(); } finally { // 7.释放资源 JDBCUtils_V1.release(conn, pstmt, rs); } } }
对应 properties 文件的处理,开发中也会使用 Properties 对象将进行,在V3版本中,咱们将采用加载 properties 文件得到流,而后使用 properties 对象进行处理。
package cn.jayvee.jdbc; 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; /** * 提供获取连接和资源的方法 * * @author Jayvee * @date 2019-5-12 下午4:22:24 * @version V1.0 * */ public class JDBCUtils_V3 { private static String driver; private static String url; private static String username; private static String password; /** * 静态代码块加载配置文件信息 */ static { try { // 1.经过当前类获取类加载器 ClassLoader classLoader = JDBCUtils_V3.class.getClassLoader(); // 2.经过类加载器的方法获取一个输入流 InputStream is = classLoader.getResourceAsStream("db.properties"); // 3.建立一个properties对象 java.util.Properties props = new java.util.Properties(); // 4.加载输入流 props.load(is); // 5.获取相关参数的值 driver = props.getProperty("driver"); url = props.getProperty("url"); username = props.getProperty("username"); password = props.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } /** * 获取连接方法 * * @return */ public static Connection getConnection() { Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { 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) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.jayvee.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.management.RuntimeErrorException; import org.junit.Test; import cn.jayvee.jdbc.JDBCUtils_V1; import cn.jayvee.jdbc.JDBCUtils_V2; import cn.jayvee.jdbc.JDBCUtils_V3; /** * 测试工具类 * * @author Jayvee * @date 2019-5-12 下午4:38:54 * @version V1.0 */ public class TestUtils { /** * 根据id删除信息 */ @Test public void testDeleteById(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V3.getConnection(); // 2.编写 sql 语句 String sql = "delete from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setInt(1, 3); // 5.执行删除操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("删除成功!"); }else{ System.out.println("删除失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V3.release(conn, pstmt, null); } } /** * 添加用户信息方法 */ public void testAdd(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V2.getConnection(); // 2.编写 sql 语句 String sql = "insert into student values(null,?,?)"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setString(1, "wangjiawei"); pstmt.setString(2, "25"); // 5.执行插入操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("添加成功!"); }else{ System.out.println("添加失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V2.release(conn, pstmt, null); } } /** * 根据id查询用户信息 */ public void testFindUserById() { Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { // 1.获取连接 conn = JDBCUtils_V1.getConnection(); // 2.编写sql语句 String sql = "select * from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数(1表示第一个问号,2表示第一个问号的值是2) pstmt.setInt(1, 2); // 5.执行查询操做 rs = pstmt.executeQuery(); // 6.处理结果集 while (rs.next()) { // rs.getString(2)表示获取这条数据的第二列的数据 // rs.getString("age")表示获取这条数据中age字段的之 System.out.print(rs.getString(2) + "----" + rs.getString("age")); } // 释放资源放在这里不可行! } catch (SQLException e) { e.printStackTrace(); } finally { // 7.释放资源 JDBCUtils_V1.release(conn, pstmt, rs); } } }
package cn.jayvee.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.jayvee.jdbc.JDBCUtils_V1; import cn.jayvee.jdbc.JDBCUtils_V2; import cn.jayvee.jdbc.JDBCUtils_V3; /** * 测试工具类 * * @author Jayvee * @date 2019-5-12 下午4:38:54 * @version V1.0 */ public class TestUtils { /** * 根据id更新信息 */ @Test public void testUpdateById(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V3.getConnection(); // 2.编写 sql 语句 String sql = "update student set age=? where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setInt(1, 18); pstmt.setInt(2, 1); // 5.执行更新操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("更新成功!"); }else{ System.out.println("更新失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V3.release(conn, pstmt, null); } } /** * 根据id删除信息 */ public void testDeleteById(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V3.getConnection(); // 2.编写 sql 语句 String sql = "delete from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setInt(1, 3); // 5.执行删除操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("删除成功!"); }else{ System.out.println("删除失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V3.release(conn, pstmt, null); } } /** * 添加用户信息方法 */ public void testAdd(){ Connection conn = null; PreparedStatement pstmt = null; try { // 1.获取链接 conn = JDBCUtils_V2.getConnection(); // 2.编写 sql 语句 String sql = "insert into student values(null,?,?)"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数 pstmt.setString(1, "wangjiawei"); pstmt.setString(2, "25"); // 5.执行插入操做 int row = pstmt.executeUpdate(); if (row>0) { System.out.println("添加成功!"); }else{ System.out.println("添加失败!"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ // 6.释放资源 JDBCUtils_V2.release(conn, pstmt, null); } } /** * 根据id查询用户信息 */ public void testFindUserById() { Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { // 1.获取连接 conn = JDBCUtils_V1.getConnection(); // 2.编写sql语句 String sql = "select * from student where id=?"; // 3.获取执行sql语句对象 pstmt = conn.prepareStatement(sql); // 4.设置参数(1表示第一个问号,2表示第一个问号的值是2) pstmt.setInt(1, 2); // 5.执行查询操做 rs = pstmt.executeQuery(); // 6.处理结果集 while (rs.next()) { // rs.getString(2)表示获取这条数据的第二列的数据 // rs.getString("age")表示获取这条数据中age字段的之 System.out.print(rs.getString(2) + "----" + rs.getString("age")); } // 释放资源放在这里不可行! } catch (SQLException e) { e.printStackTrace(); } finally { // 7.释放资源 JDBCUtils_V1.release(conn, pstmt, rs); } } }
项目代码:https://github.com/wjw1014/JavaMysqlStudy/tree/master/web09 (小白操做,仅供参考!)