一、将连接数据库所需的相关信息写入到配置文件、通过读取配置文件来获取数据库的相关信息
package edu.aeon.aeonutils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * [说明]:jdbc工具类 * 封装了jdbc里面的重复步骤:数据库的连接和数据库资源的释放 * @author aeon * @version 1.2(该版本将连接数据库的各种数据库配置信息(用户名、密码、驱动及url)单独提取到配置文件中) */ public class AeonJdbcUtils { private static String username; private static String password; private static String driverClass; private static String url; /** * 静态代码块处理读取之前的数据 */ static{ InputStream inputStream = AeonJdbcUtils.class.getClassLoader().getResourceAsStream("config/database/database.properties"); Properties properties=new Properties(); try { properties.load(inputStream); username = properties.getProperty("username"); password = properties.getProperty("password"); driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); } catch (IOException e) { System.out.println("初始化读取数据库配置文件--->database.properties失败!"); e.printStackTrace(); } } /** * 连接数据库 * @return 数据库连接对象 * @throws ClassNotFoundException * @throws SQLException */ public static Connection getMySqlConnection() throws ClassNotFoundException, SQLException{ Class.forName(driverClass); return DriverManager.getConnection(url, username, password); } /** * 释放数据库资源 * @param resultSet 结果集 * @param statement 执行sql语句的对象 * @param connection 数据库连接对象 */ public static void closeDB(ResultSet resultSet,Statement statement,Connection connection){ if(null!=resultSet){ try { resultSet.close(); } catch (SQLException e) { System.out.println("释放数据库资源失败!--->resultSet"); e.printStackTrace(); } } if(null!=statement){ try { statement.close(); } catch (SQLException e) { System.out.println("释放数据库资源失败!--->statement"); e.printStackTrace(); } } if(null!=connection){ try { connection.close(); } catch (SQLException e) { System.out.println("释放数据库资源失败!--->connection"); e.printStackTrace(); } } } }
测试代码:
package edu.aeon.aeonutils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * [说明]:测试连接数据库工具类(version 1.2) * @author aeon * */ public class TestAeonJdbcUtils { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = AeonJdbcUtils.getMySqlConnection(); statement = connection.createStatement(); String sql = "select * from user"; resultSet = statement.executeQuery(sql); System.out.println("用户id\t用户名\t用户密码"); while (resultSet.next()) { int userId = resultSet.getInt("userId");// 根据表字段名获取该行记录上的字段名所对应的字段值 String userName = resultSet.getString("userName"); String userPw = resultSet.getString("userpw");// 数据库中的字段不区分大小写 System.out.println(userId + "\t" + userName + "\t" + userPw); } } catch (ClassNotFoundException e) { System.out.println("加载驱动失败!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("连接数据库失败!"); e.printStackTrace(); } AeonJdbcUtils.closeDB(resultSet, statement, connection); } }
database.properties中的配置信息:
username=root password=root driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db_test
项目包视图截图如下:
建库及表语句:
drop database db_test; create database db_test; use db_test; create table user( userId int(5) primary key comment ‘用户id‘, userName varchar(16) comment ‘用户姓名‘, userPw varchar(16) comment ‘用户密码‘ ); insert into user(userId,userName,userPw) values(10001,‘user1‘,‘user1‘); insert into user(userId,userName,userPw) values(10002,‘user2‘,‘user2‘); insert into user(userId,userName,userPw) values(10003,‘user3‘,‘user3‘); insert into user(userId,userName,userPw) values(10004,‘user4‘,‘user4‘); insert into user(userId,userName,userPw) values(10005,‘user5‘,‘user5‘);
数据库信息截图:
测试结果截图:
原文地址:https://www.cnblogs.com/aeon/p/10072674.html
时间: 2024-11-08 10:05:36