TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
MathJax.Hub.Config({ showMathMenu: false });
改写成工具类
我们发现连接和释放数据库的代码在进行数据库操作时用的很多,所以我们将连接数据库的方法写成一个工具类,方便调用
我们希望能在配置文件中设置数据库连接参数,如果我们的数据库更改了,或者用户名和密码更改了,我们不用修改代码,修改配置文件就好了
JdbcUtils.java
package com.qiaolei;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 静态代码块加载配置文件信息,只会被初始化一次
*/
static {
try {
// 1.通过当前类获取类加载器
ClassLoader classLoader = JdbcUtils.class.getClassLoader();
// 2.通过类加载器的方法获得一个输入流,path 不以’/"开头时默认是从此类所在的包下取资源,以’/"开头则是从
//ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。
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) {
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;
}
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();
}
}
}
}
配置文件db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ecshop?useUnicode=true&characterEncoding=utf8
username=root
password=123456
接下来我们只需要在以下代码里填充sql操作代码就好了
public static void mode(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
//获取连接
conn = JdbcUtils.getConnection();
//sql代码
} catch (Exception e) {
} finally {
//释放资源
JdbcUtils.release(conn, pstmt, null);
}
}
增删查改实例
增加
/**
* 添加用户信息方法
*/
@Test
public 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删除信息方法
*/
@Test
public void testDeleteById() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1.获取连接
conn = JDBCUtils_V3.getConnection();
// 2.编写sql语句
String sql = "delete from tbl_user where uid=?";
// 3.获取执行sql语句对象
pstmt = conn.prepareStatement(sql);
// 4.设置参数
pstmt.setInt(1, 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);
}
}
查找
/**
* 根据id查询用户信息
*/
@Test
public 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);
}
}
更新
/**
* 根据id更新用户信息方法
*/
@Test
public 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, 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);
}
}
|
|