包结构:
===================================== jdbc.properties路径:/jdbc-1/src/jdbc.properties 内容: #连接MySQL jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/jdbc1 #连接Oracle #jdbc.user=scott #jdbc.password=tiger #jdbc.driverClass=oracle.jdbc.driver.OracleDriver #jdbc.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:ORCL001 ==========================================工具类:获取连接、关闭资源
package com.atguigu.jdbc; 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 操作的工具类 */ public class JdbcUtils { public static void close(ResultSet resultSet){ try { if(resultSet != null){ resultSet.close(); } } catch (Exception e) { e.printStackTrace(); } } //关闭数据库资源 public static void close(Connection connection){ try { if(connection != null){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement statement){ try { if(statement != null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } } //根据配置文件获取数据库连接 public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{ Connection connection = null; //0. 读取 Properties 文件 Properties properties = new Properties(); InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(in); //1. 准备连接数据库的四个基本信息: String driverClassName = properties.getProperty("jdbc.driverClass"); String url = properties.getProperty("jdbc.jdbcUrl"); String user = properties.getProperty("jdbc.user"); String password = properties.getProperty("jdbc.password"); //2. 加载驱动 Class.forName(driverClassName); //3. 调用 DriverManager.getConnection(url, user, password) 获取连接 connection = DriverManager.getConnection(url, user, password); return connection; }公用update代码实现:
//用于 insert、update、delete 的方法. public void update(String sql, Object ... args){ Connection connection = null; PreparedStatement preparedStatement = null; try { //1. 获取连接 connection = JdbcUtils.getConnection(); //2. 调用 Connection#prepareStatement(sql) 创建 PreparedStatement 对象 preparedStatement = connection.prepareStatement(sql); //3. 调用 PreparedStatement 的 setXxx 方法来填充占位符 if(args != null && args.length > 0){ for(int i = 0; i < args.length; i++){ preparedStatement.setObject(i + 1, args[i]); } } //4. 执行 SQL. 调用 execute() 方法. 而不能再调用 Statement 的 execute(sql) 方法 preparedStatement.execute(); } catch (Exception e) { e.printStackTrace(); } finally{ //5. 关闭数据库资源 JdbcUtils.close(preparedStatement); JdbcUtils.close(connection); } } ========================================== //测试 @Test public void testUpdate2() throws ClassNotFoundException{ String sql = null; // sql = "INSERT INTO users(username, password) VALUES(? ,?)"; // update(sql, "abcd", "123456"); // sql = "UPDATE users SET username = ?, password = ? WHERE id = ?"; // update(sql, "bcde", "3457678", 6); sql = "DELETE FROM users WHERE id = ?"; update(sql, 6); //String className = ""; //把 className 对应的 class 文件加载到内存中. 得到 Class 对象 //1. 导致 className 对应的 class 的静态变量完成初始化 //2. 执行静态代码块的代码 //Class.forName(className); }