使用工具类完成JDBC

来源:互联网 发布:帝国仿内涵吧网源码 编辑:程序博客网 时间:2024/05/29 13:10

使用工具类完成JDBC

工具类1

在对MySQL数据库中表进行操作时“获取连接”和“释放资源”两个代码部分在增删改查所有功能中都存在,开发中遇到此情况将采用工具类的方法进行抽取,从而达到代码的重复利用。

此处添加V1

 

1.获取连接

在工具类中书写getConnection()方法来加载驱动和获取连接

public static Connection getConnection() {

Connection conn = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "123456");

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

 

 

2.释放资源

以为在数据库连接情况下会占用系统资源当不需要连接数据库时需要及时释放资源来减少系统压力

通过传参的方式来释放资源

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();

}

}

 

 

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * 提供获取连接和释放资源的 方法 *  * @author Never Say Never * @date 2016年7月29日 * @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/web08", "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) {e.printStackTrace();}}}}

工具类2

开发中火得连接的4个参数(驱动、URL、用户名、密码)可以放在配置文件中,方便后期维护。比如程序如果需要更换数据库,只需要修该配置文件即可。

配置文件要求:

 

1.文件位置放在src

2.文件名称:任意但扩展名必须为.properties

3.文件内容:一行一组数据,格式为key=value

key命名自定义,如果是多个单词,习惯用点分隔。例如:jdbc.driver

value值不支持中文,如果需要使用非英文字符,将进行unicode转换

4.driver=com.mysql.jdbc.Driver

5.url=jdbc:mysql://localhost:3306/web08?useUnicode=true&characterEncoding=utf8

6.username=root

7.password=123456

 

 

将使用JDK使用的工具类ResourceBundle加载properties文件,ResourceBundle提供getBundle()方法用于只提供properties文件即可,之后使用getString(key)通过key获得value的值。

static{

ResourceBundle bundle = ResourceBundle.getBundle("db");

driver = bundle.getString("driver");

url = bundle.getString("url");

username = bundle.getString("username");

password = bundle.getString("password");

}

 

 

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 Never Say Never * @date 2016年7月29日 * @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;}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();}}}}

工具类3

采用加载properties文件获得流,然后使用properties对象进行处理。

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;/** * 提供获取连接和释放资源的 方法 *  * @author Never Say Never * @date 2016年7月29日 * @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对象Properties props=new 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) {// TODO Auto-generated catch blocke.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;}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();}}}}
对三个工具类的测试写到一个测试类中了
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import cn.itheima.jdbc.JDBCUtils_V1;import cn.itheima.jdbc.JDBCUtils_V2;import cn.itheima.jdbc.JDBCUtils_V3;/** * 测试工具类 *  * @author Never Say Never * @date 2016年7月29日 * @version V1.0 */public class TestUtils {@Testpublic void testUpdateById() {Connection conn = null;PreparedStatement pstmt = null;try {// 1.获取连接conn = JDBCUtils_V3.getConnection();// 2.编写sql语句String sql = "update tbl_user set upassword=? where uid=?";// 3.获取执行sql语句对象pstmt = conn.prepareStatement(sql);// 4.设置参数pstmt.setString(1, "999");pstmt.setInt(2, 4);// 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);}}/** * 添加用户信息方法 */@Testpublic void testAdd() {Connection conn = null;PreparedStatement pstmt = null;try {// 1.获取连接conn = JDBCUtils_V2.getConnection();// 2.编写sql语句String sql = "insert into tbl_user values(null,?,?)";// 3.获取执行sql语句对象pstmt = conn.prepareStatement(sql);// 4.设置参数pstmt.setString(1, "lisi");pstmt.setString(2, "hehe");// 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查询用户信息 */@Testpublic void testFindUserById() {Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try {// 1.获取连接conn = JDBCUtils_V1.getConnection();// 2.编写sql语句String sql = "select * from tbl_user where uid=?";// 3.获取执行sql语句对象pstmt = conn.prepareStatement(sql);// 4.设置参数pstmt.setInt(1, 2);// 5.执行查询操作rs = pstmt.executeQuery();// 6.处理结果集while (rs.next()) {System.out.println(rs.getString(2) + "----" + rs.getString("upassword"));}// 释放资源放在此处行么?【不行滴!】} catch (SQLException e) {e.printStackTrace();} finally {// 7.释放资源JDBCUtils_V1.release(conn, pstmt, rs);}}}



原创粉丝点击