JDBC工具类
package com.itheima.jdbc.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class JDBCUitel1 {
private static DataSource dataSource=null;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src/druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
}catch(Exception e) {
e.printStackTrace();
}
}
public static Connection getconnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
public static DataSource getdatasource() {
return dataSource;
}
public static void release(Statement stmt,Connection conn) {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn) {
if(stmt!=null) {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
conn=null;
}
if(rs!=null) {
try {
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
rs=null;
}
}
}
配置文件
url:jdbc:mysql://localhost/web_test4?serverTimezone=UTC
driverClassName:com.mysql.cj.jdbc.Driver
username:root
password:123456
用户类
package com.itheima.DBuitls.daouser;
public class user {
private Integer id;
private String username;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "user [id=" + id + ", username=" + username + ", money=" + money + "]";
}
}
测试类
package com.itheima.DButils;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import com.itheima.DBuitls.daouser.user;
import com.itheima.jdbc.utils.JDBCUitel1;
public class DBuitils {
@Test
public void dome1() throws SQLException {
QueryRunner queryrunner = new QueryRunner(JDBCUitel1.getdatasource());
queryrunner.update("insert into user values (null,?,?);", "阿黄","20000");
}
@Test
public void dome2() throws SQLException {
QueryRunner queryrunner = new QueryRunner(JDBCUitel1.getdatasource());
queryrunner.update("delete from user where id=?", 6);
}
@Test
public void dome3() throws SQLException {
QueryRunner queryrunner = new QueryRunner(JDBCUitel1.getdatasource());
queryrunner.update("update user set username=?,money=? where id = ?", "旺财","100000",3);
}
@Test
public void demo4() throws SQLException {
QueryRunner queryrunner = new QueryRunner(JDBCUitel1.getdatasource());
user rs = queryrunner.query("select * from user where id=?", new BeanHandler<user>(user.class), 2);
System.out.println(rs);
}
@Test
public void demo5() throws SQLException {
QueryRunner queryrunner = new QueryRunner(JDBCUitel1.getdatasource());
List<user> list= queryrunner.query("select * from user", new BeanListHandler<user>(user.class));
for(user a:list) {
System.out.println(a);
}
}
}