db.properties文件
driverClassName=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe username=system password=123456
db.properties
JDBCUtilProperties.java文件
对数据连接和释放资源的封装,
package com.xdl.util; 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; public class JDBCUtilProperties { public static String driverClassName; public static String url; public static String username; public static String password; static { try { // 读取 db.properties 文件 ,路径是相对于项目的路径 InputStream inputStream = JDBCUtilProperties.class.getClassLoader(). getResourceAsStream("com/xdl/util/db.properties"); /* Properties 就是一个key value 结构 */ Properties pro = new Properties(); try { pro.load(inputStream); driverClassName = pro.getProperty("driverClassName"); url = pro.getProperty("url"); username = pro.getProperty("username"); password = pro.getProperty("password"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Class.forName(driverClassName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* 获取数据库 连接的方法 */ public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /* 释放资源的方法 */ public static void releaseResource(Connection conn,Statement st,ResultSet rs){ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ rs = null; } } if(st != null){ try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ st = null; } } if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ conn = null; } } } }
JDBCUtilProperties.java
Test.java文件
package com.xdl.test; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.xdl.entity.User; import com.xdl.util.JDBCUtilProperties; public class Test1 { public static void main(String[] args) { //使用工具类 来获取连接 Connection conn = null; Statement st = null; ResultSet rs = null; //建立连接 conn = JDBCUtilProperties.getConnection(); //获得执行环境 try { st = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { rs = st.executeQuery("select * from MyUser"); // while(rs.next()){ // 希望把数据 封装成java的对象 User user = new User(); // user.setId(rs.getInt("ID")); // user.setName(rs.getString("NAME")); // user.setPassword(rs.getString("PASSWORD")); // System.out.println(user); // } //上面这种方式是使用硬编码的方式,也是最常用的方式。 //下面这种方式是采用软编码的方法, //利用java中的反射机制, //当前文件分别建立和User类、查询结果的反射, //然后再拼装出上面的功能。 ResultSetMetaData rsmd = rs.getMetaData(); //获得此ResultSet对象中列的数量 int columns = rsmd.getColumnCount(); String userStr = "com.xdl.entity.User"; List<User> datas = new ArrayList<User>(); //将光标从当前位置向前移一位 while (rs.next()) { // 里面使用底层的信息 包装对象,获得User类的Class对象 Class<?> u = Class.forName(userStr); Object obj = null; //获得User对象 obj = u.newInstance(); Field[] fs = u.getDeclaredFields(); for (int i = 1; i <= columns; i++) { for (Field f : fs) { //判断User对象是否有和MyUser表中相等的字段 if (f.getName().toUpperCase().equals(rsmd.getColumnName(i))) { //当前字段的类型名称是NUMBER if (rsmd.getColumnTypeName(i).equals("NUMBER")) { //取消对User类的访问权限检查 f.setAccessible(true); try { //向User对象中插入对应的值 f.setInt(obj, rs.getInt(rsmd.getColumnName(i))); } catch (IllegalArgumentException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //当前字段的类型名称是Varchar2 if (rsmd.getColumnTypeName(i).equals("VARCHAR2")) { f.setAccessible(true); try { f.set(obj, rs.getString(rsmd.getColumnName(i))); } catch (IllegalArgumentException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } //将当前User对象存储到一个User集合中 datas.add((User) obj); } for(User user:datas){ System.out.println(user); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { JDBCUtilProperties.releaseResource(conn, st, rs); } } }
Test.java
时间: 2024-10-10 06:55:49