在Java中增删改Oracle表的万能方法

    public class JdbcUtil {
    	// 数据库驱动类
    	private static String driver;
    	// 数据库服务器的地址
    	private static String url;
    	// 连接服务器的用户名
    	private static String user;
    	// 连接服务器的用户密码
    	private static String password;
    	// 加载驱动只需要加载一次,可以放到静态代码块中
    	static {
    		try {
    
    			// 对db.properties文件的读取
    			Properties prop = new Properties();
    			// 加载文件,并转换为输入流,从流中读取数据
    			InputStream stream = JdbcUtil.class.getResourceAsStream(
    			                                 "/db.properties");
    			prop.load(stream);
    			// 调用getProperty(String key),根据key获取相应的value
    			url = prop.getProperty("url");
    			user = prop.getProperty("user");
    			password = prop.getProperty("password");
    			driver = prop.getProperty("driver");
    			// 加载驱动
    			Class.forName(driver);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	// 获取一个连接
    	public static Connection getConnection() {
    		Connection conn = null;
    		try {
    			conn = DriverManager.getConnection(url, user, password);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return conn;
    	}
    
    	// 关闭资源
    public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {
    		if (rs != null) {
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		if (stmt != null) {
    			try {
    				stmt.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    
    	/*
    	 * 通用增删改方法 参数1:要执行的DML语句(insert,update,delete语句)
    	 * 参数2:给DML语句中的?赋的一组值,把占位符值统一放到可变参数中,
    	 * 可变参数可以当成数组用
    	 * 返回值:执行增删改影响数据库的行数,如果成功,返回1,失败返回0
    	 */
    
    	public static int executeUpdate(String sql, Object... params) {
    		int count = 0;
    		// 创建连接
    		Connection conn = JdbcUtil.getConnection();
    		PreparedStatement stmt = null;
    		try {
    			// 创建预编译的statement
    			stmt = conn.prepareStatement(sql);
    			// 给占位符?赋值
    			for (int i = 0; i < params.length; i++) {
    				stmt.setObject(i + 1, params[i]);
    			}
    			count = stmt.executeUpdate();
    
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    
    		} finally {
    			// 关闭资源
    			JdbcUtil.closeAll(null, stmt, conn);
    		}
    		return count;
    	}
    }
    
db.properties:

    #key=value
    driver=oracle.jdbc.driver.OracleDriver
    url = jdbc:oracle:thin:@localhost:1521:orcl
    user=*****        //自己账户
    password=*********      //自己密码

猜你喜欢

转载自blog.csdn.net/qq_44013790/article/details/86661851