【学习笔记】提取工具类
我们去查询数据库时,只有sql代码是核心业务,其他的代码如果每次都写,十分重复,所以我们要将一些重复的代码提取出来,做工具类,每次执行业务,只调用这个工具类即可。
首先把 driver、 url 、username、 password 放到配置文件中,在工具类中将这些读取
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&character=utf8&useSSL=true
username=root
password=123456
工具类:
package com.lesson02.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static{
try{
//读取配置文件
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(in);
//获取资源
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//加载驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
//关闭连接
public static void release(Connection conn, Statement st, ResultSet rs) throws SQLException {
if (rs != null){
rs.close();
}
if (st != null){
st.close();
}
if (rs != null){
rs.close();
}
}
}
测试工具类:
-
添加数据
package com.lesson02; import com.lesson02.utils.JdbcUtils; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class TestInsert { public static void main(String[] args) throws SQLException { //获取连接 Connection connection = JdbcUtils.getConnection(); //执行sql对象 Statement statement = connection.createStatement(); String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,email,birthday)" + "VALUES(4,'路飞','123456','123456@qq.com','2022-01-01')"; int i = statement.executeUpdate(sql); if (i>0){ System.out.println("插入成功"); } JdbcUtils.release(connection,statement,null); } }
遇到的问题:
在连接时,url为null
说明我们的配置文件没有被读取到
我们要将配置文件放在src目录下,才能被读取到
或者在文件名前面加上文件所在的目录名
-
修改数据
package com.lesson02; import com.lesson02.utils.JdbcUtils; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class TestUpdate { public static void main(String[] args) throws SQLException { Connection connection = JdbcUtils.getConnection(); Statement statement = connection.createStatement(); String sql = "UPDATE users SET `NAME` = '小明' WHERE id = 4"; int i = statement.executeUpdate(sql); if (i>0){ System.out.println("修改成功"); } JdbcUtils.release(connection,statement,null); } }
-
查询数据
package com.lesson02; import com.lesson02.utils.JdbcUtils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestQuery { public static void main(String[] args) throws SQLException { Connection connection = JdbcUtils.getConnection(); Statement statement = connection.createStatement(); String sql = "SELECT * FROM users WHERE `NAME` = '小明'"; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()){ System.out.println("id:" + resultSet.getObject("id")); System.out.println("name:" + resultSet.getObject("NAME")); System.out.println("pwd:" + resultSet.getObject("PASSWORD")); System.out.println("email:" + resultSet.getObject("email")); System.out.println("birthday:" + resultSet.getObject("birthday")); } JdbcUtils.release(connection,statement,resultSet); } }