package JDBC;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Test005 {
public static void main(String[] args) {
//第一种方法:创建一个Map集合,把数据库信息添加进去,然后把Map交给DruidDataSourceFactory工厂类
//创建Map集合
Map<String, String> map = new HashMap<>();
//往Map集合中添加数据库信息
map.put("driver", "com.mysql.cj.jdbc.Driver");
map.put("url", "jdbc:mysql://localhost:3306/test002?serverTimezone=PRC");
map.put("username", "dbone");
map.put("password", "dbone");
try {
//创建数据源读取Map
DataSource d = DruidDataSourceFactory.createDataSource(map);
//阿帕奇jdbcutils更容易操作数据库,读取数据池信息
QueryRunner run = new QueryRunner(d);
//简单实现一个查询语句
// List<Map<String, Object>> sel = run.query("select * from huiyuan", new MapListHandler());
//遍历输出
// sel.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
//第一种方法,配置好加载文件,用properties类读取出来交给DruidDataSourceFactory这个工厂类
//创建Properties类,用来加载配置文件
Properties p = new Properties();
try {
//加载配置文件
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties"));
//创建数据源读取加载p
DataSource ds = DruidDataSourceFactory.createDataSource(p);
//阿帕奇jdbcutils更容易操作数据库,读取数据池信息
QueryRunner runner = new QueryRunner(ds);
//简单实现一个条件查询
List<Map<String, Object>> qe1 = runner.query("select * from huiyuan where id =?", new MapListHandler(), 3);
qe1.forEach(System.out::println);
List<String> qe = runner.query("select id from ssx", new ColumnListHandler<>());
System.out.println(qe);
Student be = runner.query("select * from huiyuan where id =?", new BeanHandler<>(Student.class), 2);
System.out.println(be);
List<String> qe2 = runner.query("select name from huiyuan", new ColumnListHandler<>());
qe2.forEach(System.out::println);
// int up1 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?", "王丽", 1400, "女", 21,1);
// int up2 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?","赵四",1000,"男",19,2);
// int up3 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?","张三",1200,"男",18,3);
// int up4 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?","王五",1300,"男",20,4);
// int up5 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?","莉莉",1100,"女",20,8);
// int up6 = runner.update("update huiyuan set name =?,money=?,gender=?,age=? where id =?","james",1500,"男",20,6);
// System.out.println(up1);
// System.out.println(up2);
// System.out.println(up3);
// System.out.println(up4);
// System.out.println(up5);
// System.out.println(up6);
// int ins = runner.update("insert into huiyuan values (null,?,?,?,?,?,?,?)", "jack", "男", 21, 1600, "1999-02-02", "2000-05-18", "很优秀!");
// System.out.println(ins);
// int ins2 = runner.execute("delete from huiyuan where id =?", 7);
// System.out.println(ins2);
String ver = runner.query("select version()", new ScalarHandler<>());
System.out.println(ver);
runner.execute("select id,name,money,gender,birth from huiyuan", new MapListHandler()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}