使用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;}}}
阅读全文
0 0
- 使用JDBC实现CRUD
- JDBC学习笔记 -- day04 使用jdcb实现CRUD操作
- jsp+servlet+jdbc 实现crud
- jdbc-使用DBUtils进行crud
- 3.使用jdbc完成 crud
- 使用mybatis实现CRUD
- JDBC CRUD
- 使用JDBC对数据库进行CRUD
- 使用JDBC操作数据库(CRUD基本操作)
- 使用JDBC对数据库进行CRUD
- jdbc基本使用CRUD(二)
- 使用JDBC对数据库进行CRUD
- javaWeb-使用JDBC完成CRUD的操作
- 从零开始学JDBC--1.17 使用DbUtils组件实现CRUD和结果集快速封装
- JDBC(三)JDBC实现增删改查(CRUD)
- Hibernate和JDBC实现CRUD操作的比较
- Spring + JDBC + Struts联合开发(实现单表的CRUD)
- Spring + JDBC + Struts联合开发(实现单表的CRUD)
- 【JavaScript学习】BOM:history对象
- (最小点覆盖 匈牙利算法)--pojMachine Schedule
- RabbitMQ四种交换机
- 算法学习(2):BFS/DFS-广度优先/深度优先
- IO流(copy文件,流转成PDF,流编码)
- 使用JDBC实现CRUD
- Gym 101503I Just Matrix【思维+拓扑排序】好题~
- 修改 mysql-binlog 二进制日志文件至其它目录
- jprofiler_监控远程linux服务器的JVM进程
- 端口扫描器的实现
- 重装-jdk配置,tomcat配置,sdk配置
- 理解 JavaScript 中的 for…of 循环
- Java将时间字符串转成时间
- MMU介绍