工具类:
package com.java.jdbc; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; public class JDBCUtils { private static String url; private static String user; private static String password; static { try { Properties properties = new Properties(); // 反射获取路径 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL resource = classLoader.getResource("jdbc.properties"); String path = resource.getPath(); properties.load(new FileReader(path)); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } /** * 获取连接 * @return 连接 */ public static Connection getConnection(){ Connection connection = null; try { connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { e.printStackTrace(); }return connection; } /** * 关闭连接 * @param statement * @param connection */ public static void close(Statement statement,Connection connection){ if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭查询连接 * @param resultSet * @param statement * @param connection */ public static void close(ResultSet resultSet,Statement statement, Connection connection){ if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
properties配置文件:
url=jdbc:mysql://localhost:3306/school?characterEncoding=utf-8&useSSL=false user=root password=root driver=com.mysql.jdbc.Driver
装载数据的student类:
package com.java.jdbc; public class Student { private int sno; private String name; private int age; private String sex; @Override public String toString() { return "Student{" + "sno=" + sno + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
测试demo:
package com.java.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class JDBCDemo01 { public static void main(String[] args) throws SQLException { JDBCDemo01 demo01 = new JDBCDemo01(); List<Student> students = demo01.findAll(); System.out.println(students); } /** * 查询所有student对象 * @return */ public List<Student> findAll()throws SQLException { Connection connection = JDBCUtils.getConnection(); String sql = "select * from student"; // 获取sql执行对象 Statement statement = connection.createStatement(); // 执行Sql拿到结果集 ResultSet resultSet = statement.executeQuery(sql); // 遍历结果集 List<Student> list = new ArrayList<>(); while (resultSet.next()){ // 获取数据 int sno = resultSet.getInt("sno"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String sex = resultSet.getString("sex"); // 创建对象,赋值 Student student = new Student(); student.setSno(sno); student.setName(name); student.setAge(age); student.setSex(sex); // 装载数据 list.add(student); } JDBCUtils.close(resultSet,statement,connection); return list; } }
运行效果: