1.jdbc.properties 配置文件
url = jdbc:mysql:///db2
user = root
password = 12345678
driver = com.mysql.cj.jdbc.Driver
2.Utils.JDBCUtils抽取的工具类
package Utils;import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;/*** @author Alina* @date 2022年02月05日 10:14 下午* JDBC的工具类,* 1.动态注册驱动* 2.释放资源* 对应文件6*/
public class JDBCUtils {private static String url;private static String user;private static String password;private static String driver;/***** @author Alina* @date 2022/2/5 10:53 下午* @param null* @return null* 声明静态代码块,以方便调用类时,代码块的内容就被执行*/static {try {//使用Properties 类读取配置文件中内容Properties pro = new Properties();//使用class类的Classload 方法获得绝对地址ClassLoader loader = JDBCUtils.class.getClassLoader();URL res_url = loader.getResource("jdbc.properties");String path = res_url.getPath();//读取配置文件中内容pro.load(new FileReader(path));//此处后面的变量记得加双引号url = pro.getProperty("url");user = pro.getProperty("user");password = pro.getProperty("password");Class.forName(pro.getProperty("driver"));} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}/***** @author Alina* @date 2022/2/5 10:57 下午* @return java.sql.Connection* 使用方法获得数据库链接方式*/public static Connection getconnection() throws SQLException {return DriverManager.getConnection(url,user,password);}public static void close(Statement stmt, Connection coon) {if (stmt != null) {try {stmt.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (coon != null) {try {coon.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}public static void close(ResultSet res ,Statement stmt, Connection coon) {if (res != null) {try {res.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (stmt != null) {try {stmt.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (coon != null) {try {coon.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}
}
3.获取的数据库内容
package com.jdsc;import Utils.JDBCUtils;
import domain.Emp;import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** @author Alina* @date 2022年02月05日 8:14 下午* 将数据库内查询到的数据封装为对象储存*/
public class jdbcDemo6 {public static void main(String[] args) {List<Emp> list = new jdbcDemo6().findAll();System.out.println(list);}public List<Emp> findAll() {// 解析class文件 链接驱动Connection conn = null;Statement stml = null;ResultSet res = null;List<Emp> list = null;try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// //链接数据库
// conn = DriverManager.getConnection(
// "jdbc:mysql:///db2",
// "root",
// "12345678");conn = JDBCUtils.getconnection();//获取执行sql的对象 Statementstml = conn.createStatement();//定义Sql语句String sql = "select * from db2.emp";//获取执行SQL语句后的结果集对象res = stml.executeQuery(sql);list = new ArrayList<Emp>();Emp emp = null;//如果当前游标有下一个目标while (res.next()){//获取指定行数中的列的值int id = res.getInt("id");String name = res.getString("NAME");String gander = res.getString("gender");double salary = res.getDouble("salary");Date date = res.getDate("join_date");int dept_id = res.getInt("dept_id");//创建emp对象emp = new Emp();emp.setId(id);emp.setName(name);emp.setGender(gander);emp.setSalary(salary);emp.getDate(date);emp.setDept_id(dept_id);list.add(emp);}} catch ( SQLException e) {e.printStackTrace();}finally {JDBCUtils.close(res,stml,conn);}return list;}}