工具类
- 需要使用反射获取配置文件输入流
- 将扫描配置文件db.properties的代码放在静态代码块里面,保证项目启动时,文件扫描完成
- 扫描配置文件之后,获取连接工具类就完成了他的功能
- 最后提供close方法关闭连接中使用的ResultSet,Preparedstatement,Connnectiton,遵循先开后关原则
db.properties配置文件
#驱动类的全限定名,如果数据库版本是8以上就需要在路径中加入cj,如下图
driverClassName=com.mysql.cj.jdbc.Driver
#jdbc的url 后面加了时区,以及编码
url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
#用户名
username=root
#密码
password=root
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PI0rey0y-1658300140559)(C:\Users\zjx95\Desktop\Blog java\2022\JDBC工具类\img\1.png)]](https://i-blog.csdnimg.cn/blog_migrate/65170bd6ddbe163a2b5303dee28d6daa.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uzlzsFYu-1658300140564)(C:\Users\zjx95\Desktop\Blog java\2022\JDBC工具类\img\2.png)]](https://i-blog.csdnimg.cn/blog_migrate/cf279369c982147e19a532cf74b5f70a.png)
Jdbcutil类的编写
public class JdbcUtil {
private static final Properties props = new Properties();
static {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
try {
props.load(in);
try {
Class.forName(props.getProperty("driverClassName"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"));
}
public static void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
try {
if (rs != null)
rs.close();
if (pstm != null) pstm.close();
if (conn != null) conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
Jdbcutil类的使用
public class ManagerDaoImpl implements ManagerDao {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
@Override
public boolean login(String str1, String str2) {
try {
conn = JdbcUtil.getConnection();
String sql = "SELECT * FROM manager WHERE mangername = ? AND mangerpassword = ?";
pstm = conn.prepareStatement(sql);
pstm.setString(1,str1);
pstm.setString(2,str2);
rs = pstm.executeQuery();
return rs.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtil.close(rs,pstm,conn);
}
return false;
}
- 当在数据查询了许多数据,需要使用的时候需要对查询结果ResultSet进行解析
public class InfoDaoImpl implements InfoDao {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
@Override
public List<Info> queryAll() {
try {
conn = JdbcUtil.getConnection();
String sql = "SELECT * FROM info";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
return analyseResultSet(rs);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtil.close(rs,pstm,conn);
}
return null;
}
public List<Info> analyseResultSet(ResultSet rs) throws SQLException{
if (rs!=null){
List<Info> list = new ArrayList<>();
while (rs.next()){
Info info = new Info();
info.setName(rs.getString("name"));
info.setGender(rs.getString("gender"));
info.setAddress(rs.getString("address"));
info.setAge(rs.getInt("age"));
info.setQq(rs.getString("qq"));
info.setEmail(rs.getString("email"));
list.add(info);
}
return list;
}
return null;
}
}