样例
database.properties
diverClass = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/steam?serverTimezone=UTC&characterEncoding=utf-8
username = root
password = 000323624
JDBCUtilsConfig
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/*
* 编写数据库连接的工具类,JDBC工具类
* 获取连接对象采用读取配置文件方式
* 读取文件获取连接,执行一次,static{}
*/
public class JDBCUtilsConfig {
private static Connection con;
private static String diverClass;
private static String url;
private static String username;
private static String password;
static {
try {
readConfig();
Class.forName(diverClass);
con = DriverManager.getConnection(url, username, password);
}catch(Exception ex) {
throw new RuntimeException(ex+"数据库连接失败");
}
}
private static void readConfig()throws Exception{
InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro = new Properties();
pro.load(in);
diverClass = pro.getProperty("diverClass");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
}
public static Connection getConnection() {
return con;
}
}
Sort.java
package domain;
public class Sort {
private int id;
private String name;
private String password;
private String nicheng;
private String imgurl;
private String backurl;
public Sort(int id, String name, String password, String nicheng, String imgurl, String backurl) {
super();
this.id = id;
this.name = name;
this.password = password;
this.nicheng = nicheng;
this.imgurl = imgurl;
this.backurl = backurl;
}
public Sort() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNicheng() {
return nicheng;
}
public void setNicheng(String nicheng) {
this.nicheng = nicheng;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getBackurl() {
return backurl;
}
public void setBackurl(String backurl) {
this.backurl = backurl;
}
@Override
public String toString() {
return "Sort [id=" + id + ", name=" + name + ", password=" + password + ", nicheng=" + nicheng + ", imgurl="
+ imgurl + ", backurl=" + backurl + "]";
};
}
Test.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import domain.Sort;
import jdbcutil.JDBCUtils;
import jdbcutil.JDBCUtilsConfig;
public class Test {
public static void main(String[] args) throws Exception{
Connection con = JDBCUtilsConfig.getConnection();
System.out.println(con);
PreparedStatement pst = con.prepareStatement("SELECT * FROM user");
ResultSet rs = pst.executeQuery();
List<Sort> list = new ArrayList<Sort>();
while(rs.next()) {
Sort s = new Sort(rs.getInt("id"),rs.getString("name"),rs.getString("password"),rs.getString("nicheng"),rs.getString("imgurl"),rs.getString("backurl"));
list.add(s);
}
for(Sort s : list) {
System.out.println(s);
}
JDBCUtils.close(con, pst, rs);
}
}
DBUtils工具类
增删改操作
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import jdbcutil.JDBCUtilsConfig;
/*
* 使用QueryRunner类,实现对数据表的
* insert delete update
* 调用QueryRunner类的方法 update (Connection con,String sql,Object...param)
* Object...param 可变参数,Object类型,SQL语句会出现?占位符
* 数据库连接对象,自定义的工具类传递
*/
public class DBUtilsTest {
private static Connection con = JDBCUtilsConfig.getConnection();
public static void main(String[] args)throws SQLException
{
delete();
}
/*
* 定义方法,使用QueryRunner类的方法delete将数据表的数据删除
*/
public static void delete()throws SQLException{
//创建QueryRunner类对象
QueryRunner qr = new QueryRunner();
//写删除的SQL语句
String sql = "DELETE FROM user WHERE id=?";
//调用QueryRunner方法update
int row = qr.update(con, sql, 61);
System.out.println(row);
/*
* 判断insert,update,delete执行是否成功
* 对返回值row判断
* if(row>0) 执行成功
*/
DbUtils.closeQuietly(con);
}
/*
* 定义方法,使用QueryRunner类的方法update将数据表的数据修改
*/
public static void update()throws SQLException{
//创建QueryRunner类对象
QueryRunner qr = new QueryRunner();
//写修改数据的SQL语句
String sql = "UPDATE user SET name=?,password=?,nicheng=? WHERE id=?";
//定义Object数组,存储?中的参数
Object[] params = {"ssssss","ddddddd","dddddddd",61};
//调用QueryRunner方法update
int row = qr.update(con, sql, params);
System.out.println(row);
DbUtils.closeQuietly(con);
}
/*
* 定义方法,使用QueryRunner类的方法update向数据表中,添加数据
*/
public static void insert()throws SQLException{
//创建QueryRunner类对象
QueryRunner qr = new QueryRunner();
String sql = "INSERT INTO user (name,password,nicheng)VALUES(?,?,?)";
//将三个?占位符的实际参数,写在数组中
Object[] params = {"xd","xxx","xxxx"};
//调用QueryRunner类的方法update执行SQL语句
int row = qr.update(con, sql, params);
System.out.println(row);
DbUtils.closeQuietly(con);
}
}
结果处理集
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import cn.itcast.domain.Sort;
import cn.itcast.jdbcutil.JDBCUtilsConfig;
/*
* QueryRunner数据查询操作:
* 调用QueryRunner类方法query(Connection con,String sql,ResultSetHandler r, Object..params)
* ResultSetHandler r 结果集的处理方式,传递ResultSetHandler接口实现类
* Object..params SQL语句中的?占位符
*
* 注意: query方法返回值,返回的是T 泛型, 具体返回值类型,跟随结果集处理方式变化
*/
public class QueryRunnerDemo1 {
private static Connection con = JDBCUtilsConfig.getConnection();
public static void main(String[] args) throws SQLException{
// arrayHandler();
// arrayListHandler();
// beanHandler();
// beanListHander();
// columnListHandler();
// scalarHandler();
// mapHandler();
// mapListHandler();
}
/*
* 结果集第八种处理方法,MapListHandler
* 将结果集每一行存储到Map集合,键:列名,值:数据
* Map集合过多,存储到List集合
*/
public static void mapListHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法query,传递结果集实现类MapListHandler
//返回值List集合, 存储的是Map集合
List<Map<String,Object>> list = qr.query(con, sql, new MapListHandler());
//遍历集合list
for( Map<String,Object> map : list ){
for(String key : map.keySet()){
System.out.print(key+"..."+map.get(key));
}
System.out.println();
}
}
/*
* 结果集第七种处理方法,MapHandler
* 将结果集第一行数据,封装到Map集合中
* Map<键,值> 键:列名 值:这列的数据
*/
public static void mapHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法query,传递结果集实现类MapHandler
//返回值: Map集合,Map接口实现类, 泛型
Map<String,Object> map = qr.query(con, sql, new MapHandler());
//遍历Map集合
for(String key : map.keySet()){
System.out.println(key+".."+map.get(key));
}
}
/*
* 结果集第六种处理方法,ScalarHandler
* 对于查询后,只有1个结果
*/
public static void scalarHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT COUNT(*) FROM sort";
//调用方法query,传递结果集处理实现类ScalarHandler
long count = qr.query(con, sql, new ScalarHandler<Long>());
System.out.println(count);
}
/*
* 结果集第五种处理方法,ColumnListHandler
* 结果集,指定列的数据,存储到List集合
* List<Object> 每个列数据类型不同
*/
public static void columnListHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort ";
//调用方法 query,传递结果集实现类ColumnListHandler
//实现类构造方法中,使用字符串的列名
List<Object> list = qr.query(con, sql, new ColumnListHandler<Object>("sname"));
for(Object obj : list){
System.out.println(obj);
}
}
/*
* 结果集第四种处理方法, BeanListHandler
* 结果集每一行数据,封装JavaBean对象
* 多个JavaBean对象,存储到List集合
*/
public static void beanListHander()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort ";
//调用方法query,传递结果集处理实现类BeanListHandler
List<Sort> list = qr.query(con, sql, new BeanListHandler<Sort>(Sort.class));
for(Sort s : list){
System.out.println(s);
}
}
/*
* 结果集第三种处理方法,BeanHandler
* 将结果集的第一行数据,封装成JavaBean对象
* 注意: 被封装成数据到JavaBean对象, Sort类必须有空参数构造
*/
public static void beanHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort ";
//调用方法,传递结果集实现类BeanHandler
//BeanHandler(Class<T> type)
Sort s = qr.query(con, sql, new BeanHandler<Sort>(Sort.class));
System.out.println(s);
}
/*
* 结果集第二种处理方法,ArrayListHandler
* 将结果集的每一行,封装到对象数组中, 出现很多对象数组
* 对象数组存储到List集合
*/
public static void arrayListHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用query方法,结果集处理的参数上,传递实现类ArrayListHandler
//方法返回值 每行是一个对象数组,存储到List
List<Object[]> result= qr.query(con, sql, new ArrayListHandler());
//集合的遍历
for( Object[] objs : result){
//遍历对象数组
for(Object obj : objs){
System.out.print(obj+" ");
}
System.out.println();
}
}
/*
* 结果集第一种处理方法, ArrayHandler
* 将结果集的第一行存储到对象数组中 Object[]
*/
public static void arrayHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法query执行查询,传递连接对象,SQL语句,结果集处理方式的实现类
//返回对象数组
Object[] result = qr.query(con, sql, new ArrayHandler());
for(Object obj : result){
System.out.print(obj);
}
}
}