使用JDBC实现CRUD

来源:互联网 发布:广告联盟评测源码 编辑:程序博客网 时间:2024/06/15 08:55

配置文件 res/dbinfo.properties

driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/testusername=rootpassword=root

package com.jdbc;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;public class DBUtil {private static String driverClass;private static String url;private static String username;private static String password;static {InputStream inputStream = null;String proFilePath = System.getProperty("user.dir") +"\\res\\dbinfo.properties";  try {inputStream = new BufferedInputStream(new FileInputStream(proFilePath));ResourceBundle bundle = new PropertyResourceBundle(inputStream);  driverClass = bundle.getString("driverClass");url = bundle.getString("url");username = bundle.getString("username");password = bundle.getString("password");Class.forName(driverClass);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally{if(inputStream!=null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}/** * 获取连接 * @return Connection * @throws SQLException */public static Connection getConnection() throws SQLException{return DriverManager.getConnection(url,username, password);}public static int add(String sql,Object[] param) throws SQLException{int insertNum = 0;Connection connection = null;PreparedStatement preparedStatement = null;try {connection = getConnection();preparedStatement =connection.prepareStatement(sql);if(param!=null){for(int i =0; i<param.length; i++){preparedStatement.setObject(i+1,param[i]);}}insertNum = preparedStatement.executeUpdate();return insertNum;} catch (SQLException e) {throw e;} finally{closeAll(null,preparedStatement,connection);}}public static int delete(String sql,Object[] param) throws SQLException{int insertNum = 0;Connection connection = null;PreparedStatement preparedStatement = null;try {connection = getConnection();preparedStatement =connection.prepareStatement(sql);if(param!=null){for(int i =0; i<param.length; i++){preparedStatement.setObject(i+1,param[i]);}}insertNum = preparedStatement.executeUpdate();return insertNum;} catch (SQLException e) {throw e;} finally{closeAll(null,preparedStatement,connection);}}public static int update(String sql,Object[] param) throws SQLException{int insertNum = 0;Connection connection = null;PreparedStatement preparedStatement = null;try {connection = getConnection();preparedStatement =connection.prepareStatement(sql);if(param!=null){for(int i =0; i<param.length; i++){preparedStatement.setObject(i+1,param[i]);}}insertNum = preparedStatement.executeUpdate();return insertNum;} catch (SQLException e) {throw e;} finally{closeAll(null,preparedStatement,connection);}}public static ResultSet find(String sql,Object[] param) throws SQLException{Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet =null;try {connection = getConnection();preparedStatement =connection.prepareStatement(sql);if(param!=null){for(int i =0; i<param.length; i++){preparedStatement.setObject(i+1,param[i]);}}resultSet = preparedStatement.executeQuery();return resultSet;} catch (SQLException e) {throw e;} finally{closeAll(resultSet,preparedStatement,connection);}}/** * 关闭资源 * @param resultSet * @param statement * @param connection * @throws SQLException  */public static void closeAll(ResultSet resultSet,Statement statement,Connection connection) throws SQLException{if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {throw e;}resultSet= null;}if(statement!=null){try {statement.close();} catch (SQLException e) {throw e;}statement= null;}if(connection!=null){try {connection.close();} catch (SQLException e) {throw e;}connection= null;}}}


原创粉丝点击