driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:XE user=briup password=briup
配置文件 jdbc.properties
Package com.briup.driver.form.Dao; Import java.io.IOException; Import the java.sql.Connection; Import the java.sql.DriverManager; Import the java.sql.ResultSet; Import java.sql.SQLException; Import the java.sql .Statement; Import the java.util.Properties; // package configuration file, registration, connection, switch resources // Note: // (1) configuration class path (2) exception handling public class the ConnectionFactory { Private static String Driver; Private static String url; Private static String the User; private static String password; static Connection conn; // 配置信息 static { Properties p = new Properties(); try { p.load(ConnectionFactory.class.getResourceAsStream("jdbc.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver = p.getProperty("driver"); System.out.println(driver); url = p.getProperty("url"); user = p.getProperty("user"); password = p.getProperty("password"); } // 注册 public static Connection getConnection() { // 1.注册驱动 try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 2.建立连接 try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } // 关闭资源 // PreparedStatement继承自Statement public static void close(Connection conn, Statement stmt) { close(null, conn, stmt); } public static void close(ResultSet rs, Connection conn, Statement stmt) { try { if (rs != null) rs.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (stmt != null) stmt.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // public static void main(String[] args) {} }
package com.briup.driver.form.Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; //封装工具类 public class JDBCUtil { public static void execute_DML_stmt(String sql) { Connection conn = null; Statement stmt = null; try { conn = ConnectionFactory.getConnection(); stmt = conn.createStatement(); stmt.execute(sql); System.out.println("操作完成"); ConnectionFactory.close(conn, stmt); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // select,需要处理结果集 public static void executeQuery_select_stmt(String sql, IWorkAdapter work) { Connection conn = null; Statement stmt = null; try { conn = ConnectionFactory.getConnection(); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 处理结果集 work.processRs(rs); ConnectionFactory.close(conn, stmt); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static int executeUpdate_DML_ps(String sql, IWorkAdapter work) { Connection conn = null; PreparedStatement ps = null; int change = 0; try { conn = ConnectionFactory.getConnection(); ps = conn.prepareStatement(sql); work.setValues(ps); change = ps.executeUpdate(); // ps.executeBatch(); ConnectionFactory.close(conn, ps); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return change; } public static void executeBatch_DML_ps(String sql, IWorkAdapter work) { Connection conn = null; PreparedStatement ps = null; try { conn = ConnectionFactory.getConnection(); ps = conn.prepareStatement(sql); work.setValues(ps); ps.executeBatch(); // ps.executeBatch(); ConnectionFactory.close(conn, ps); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // select,prepareStatement方法 public static Object executeQuery_select_ps(String sql, IWorkAdapter work) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Object object = null; try { conn = ConnectionFactory.getConnection(); // 预定义 ps = conn.prepareStatement(sql); // 设置参数 work.setValues(ps); rs = ps.executeQuery(); object = work.processRs(rs); ConnectionFactory.close(conn, ps); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return object; } }
Package com.briup.driver.form.Dao; Import java.sql.PreparedStatement; Import the java.sql.ResultSet; public interface IWork { // result set public Object processRS (the ResultSet RS); // after the predefined set of parameters PreparedStatement public void the setValues (the PreparedStatement PS); }
package com.briup.driver.form.Dao; import java.sql.PreparedStatement; import java.sql.ResultSet; public class IWorkAdapter implements IWork { @Override public Object processRs(ResultSet rs) { return null; // TODO Auto-generated method stub } @Override public void setValues(PreparedStatement ps) { // TODO Auto-generated method stub } }
Package com.briup.driver.form.Dao; // defects transaction is automatically committed, there may be some variation in the sequence of operations CRUD @ as required to change the code Import java.sql.PreparedStatement; Import Java. sql.ResultSet; Import java.sql.SQLException; Import of java.util.ArrayList; Import java.util.List; Import com.briup.driver.Student; public class JDBCFunction { // insert, PreparedStatement inserted predefined public static void iNSERT (Student S) { String SQL = "INSERT INTO t_student values (,,???)" ; JDBCUtil.executeUpdate_DML_ps (SQL,new IWorkAdapter() { @Override public void setValues(PreparedStatement ps) { try { ps.setInt(1, s.getId()); ps.setString(2, s.getName()); ps.setInt(3, s.getAge()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); System.out.println("插入成功"); } // 改 根据id public static void update(Student s) { String sql = "update t_student set name= ? where id = ?"; int change = JDBCUtil.executeUpdate_DML_ps(sql, new IWorkAdapter() { @Override public void setValues(PreparedStatement ps) { try { ps.setString(1, "fys"); ps.setInt(2, s.getId()); } catch (SQLException e) { //Auto-Generated Block the catch the TODO IWorkAdapter () { e.printStackTrace (); } } }); IF (Change == 0 ) { System.out.println ( "modified data is not present" ); } the else { System.out.println ( "modified successfully" ); } } // DML deleted, according to the above mentioned id public static void the delete (Student S) { String SQL = "t_student the delete from the WHERE the above mentioned id =?" ; int Change = JDBCUtil.executeUpdate_DML_ps (SQL, new new @Override public void the setValues (the PreparedStatement PS) { the try { ps.setInt ( . 1 , s.getId ()); } the catch (SQLException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } }) ; IF (Change == 0 ) { System.out.println ( "there is no data to delete" ); } the else { System.out.println ( "deleted successfully" ); } } // check, according to ID public static Student select(Student s) { String sqlS = "select * from t_student where id=?"; Object object = JDBCUtil.executeQuery_select_ps(sqlS, new IWorkAdapter() { @Override public void setValues(PreparedStatement ps) { try { ps.setInt(1, s.getId()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public Student processRs(ResultSet rs) { Student s1 = null; try { while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); s1 = new Student(id, name, age); } } catch (SQLException e) { //Auto-Generated Block the catch the TODO e.printStackTrace(); } return S1; } }); IF (Object == null ) { System.out.println ( "data that does not exist" ); return null ; } the else { return (Student) Object; } } // insert batch public static void insertBatch (List <Student> List) { String sqli = "iNSERT INTO t_student values (,,???)" ; JDBCUtil.executeBatch_DML_ps (sqli, new new IWorkAdapter () { @Override public void setValues(PreparedStatement ps) { try { for (int i = 0; i < list.size(); i++) { ps.setLong(1, list.get(i).getId()); ps.setString(2, list.get(i).getName()); ps.setInt(3, list.get(i).getAge()); ps.addBatch(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); System.out.println("插入完毕"); } // 插入批删除 public static void deleteBatch(List<Student> list) { String sqlI = "delete from t_student where id =?"; JDBCUtil.executeBatch_DML_ps(sqlI, new IWorkAdapter() { @Override public void setValues(PreparedStatement ps) { try { for (int i = 0; i < list.size(); i++) { ps.setInt(1, list.get(i).getId()); ps.addBatch(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); System.out.println("删除完毕"); } }