javaweb数据库增删改查公共类:
package com.kuang.dao;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class BaseDao {
private static String driver;
private static String url;
private static String username;
private static String password;
static{//静态代码块,在调用这个类的地方优先执行
//获取properties对象;
Properties properties = new Properties();
//使用类加载器读取数据,并转换成流
//getClassLoader 获取类加载器 getResourceAsStream 获取资源流
InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
//1、编写获取数据库的连接对象的公共方法
public static Connection getconnection(){
Connection connection= null;
try {
//加载驱动
Class.forName(driver);
//连接数据库对象
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
return connection;
}
//编写数据库查询公共方法;
public static ResultSet excuteQuery(String sql,Object[] params,Connection connection,PreparedStatement pstmt,ResultSet rs) throws SQLException {
pstmt = connection.prepareStatement(sql);
for (int i = 1; i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
}
rs = pstmt.executeQuery();
return rs;
// try {
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setObject(1,"1");
// preparedStatement.setObject(2,"狂神");
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// 将“1”和“狂神”放到Object[] params中遍历,Object[] params序号从0开始;
// proparedStatement.setObject对象中的序号是从1开始的;
// 所以:
// for(int i=1;i<params.length;i++){
// preparedStatement.setObject(i,params[i-1])
// }
}
//3、编写删,改,增,公共方法
public static int excuteUpdate(String sql,Object[] params,Connection connection,PreparedStatement pstmt) throws SQLException {
int result = 0;
pstmt = connection.prepareStatement(sql);
for (int i=1;i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
}
result = pstmt.executeUpdate();
return result;
}
//4、关闭连接,回收资源。
public static boolean close(ResultSet rs,PreparedStatement pstmt,Connection conn){
boolean flag = true;
if (rs!=null){
try {
rs.close();
rs = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (pstmt!=null){
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn!=null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return flag;
}
@Test
public void test() throws SQLException {
//获取数据库连接
Connection connection = BaseDao.getconnection();
String sql = "select * from smbms_user where id=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
Object[] params = new Object[]{1};
try {
pstmt = connection.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
ResultSet resultSet = BaseDao.excuteQuery(sql, params, connection, pstmt, rs);
while(resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("userCode"));
}
}
}
拆分理解
1、静态代码块读取资源
使用静态代码块读取资源,可以在调用这个类的地方优先执行,数据库的信息都放在db.properties文件中,使用类加载器BaseDao.class.getClassLoader().getResourceAsStream("db.properties");的方法转换成流,通过properties.load(in);将数据流放在properties中,使用getproperties()方法去除db.properties中的数据;
static{//静态代码块,在调用这个类的地方优先执行
//获取properties对象;
Properties properties = new Properties();
//
InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
2、获取数据库连接对象
使用数据库第一件事就是加载驱动,Class.forName("com.mysql.jdbc.Driver");然后使用DriverManager.getConnection(url,username,password);获取数据库连接对象,最后返回数据库连接对象;
public static Connection getconnection(){
Connection connection= null;
try {
//加载驱动
Class.forName(driver);
//连接数据库对象
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
return connection;
}
3、数据库查询公共方法
在数据库连接中获取预编译方法 connection.prepareStatement(sql);比如要将需要将参数“1”和“狂神”,添加到sql语句“select * from user where id = ? and username = ?”中,需要用到preparedStatement.setObject(1,"1");preparedStatement.setObject(2,"狂神"); 把“1"和“狂神”放到 Object[] params中,因为preparedStatement.setObject()的序号要从1开始,Object[]的序号0开始,所以:
for (int i = 1; i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
}
pstmt.executeQuery();获取查询的到ResultSet rs 将数据集返回;
//编写数据库查询公共方法;
public static ResultSet excuteQuery(String sql,Object[] params,Connection connection,PreparedStatement pstmt,ResultSet rs) throws SQLException {
pstmt = connection.prepareStatement(sql);
for (int i = 1; i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
}
rs = pstmt.executeQuery();
return rs;
// try {
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setObject(1,"1");
// preparedStatement.setObject(2,"狂神");
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// 将“1”和“狂神”放到Object[] params中遍历,Object[] params序号从0开始;
// proparedStatement.setObject对象中的序号是从1开始的;
// 所以:
// for(int i=1;i<params.length;i++){
// preparedStatement.setObject(i,params[i-1])
// }
}
4、数据库增、删、改公共方法;
类似于查找的方法,但是修改的返回值不同,返回的是受影响的行数;
//3、编写删,改,增,公共方法
public static int excuteUpdate(String sql,Object[] params,Connection connection,PreparedStatement pstmt) throws SQLException {
int result = 0;
pstmt = connection.prepareStatement(sql);
for (int i=1;i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
}
result = pstmt.executeUpdate();
return result;
}
5、关闭连接、回收资源的方法;
按照先创建后关闭的原则关闭资源;
//4、关闭连接,回收资源。
public static boolean close(ResultSet rs,PreparedStatement pstmt,Connection conn){
boolean flag = true;
if (rs!=null){
try {
rs.close();
rs = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (pstmt!=null){
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn!=null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return flag;
}
6、测试
测试的过程就是,将查询的方法构造出来,然后输出。
@Test
public void test() throws SQLException {
//获取数据库连接
Connection connection = BaseDao.getconnection();
String sql = "select * from smbms_user where id=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
Object[] params = new Object[]{1};
try {
pstmt = connection.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
ResultSet resultSet = BaseDao.excuteQuery(sql, params, connection, pstmt, rs);
while(resultSet.next()){
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("userCode"));
}
}