//连接池
package day38;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtils {
private static BasicDataSource datas=new BasicDataSource();
static{
datas.setDriverClassName("com.mysql.jdbc.Driver");
datas.setUrl("jdbc:mysql://localhost:3306/day38");
datas.setUsername("root");
datas.setPassword("root");
datas.setInitialSize(20);
datas.setMaxActive(30);
datas.setMaxIdle(5);
datas.setMinIdle(1);
}
public static DataSource getDataSourse(){
return datas;
}
}
//八种基本类型
package day38;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sound.midi.Synthesizer;
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;
public class QueryRunnerDemo {
private static QueryRunner qr =new QueryRunner(JDBCUtils.getDataSourse());
public static void main(String[] args) {
//insert();
//delete();
//arrayHandler();
//arrayListHandler();
//beanHandler();
//beanListHandler();
//columnListHandler();
//scalarHandler();
//mapHandler();
//mapListHandler();
//select();
//update1();
avgs();
}
public static void avgs(){
String sql="SELECT avg(age) FROM student WHERE sex='男'";
try {
List<Object[]> query = qr.query(sql ,new ArrayListHandler());
for(Object[] o:query){
for(Object obj:o){
System.out.println(obj);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void update1(){
String sql="UPDATE student SET age=22 WHERE NAME='qiang'";
try {
qr.update(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void select(){
String sql="SELECT * FROM student WHERE (sex='女'AND age>5)";
try {
List<Object[]> query = qr.query(sql,new ArrayListHandler());
for(Object[] obj:query){
for(Object p:obj){
System.out.print(p);
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void mapListHandler(){
String sql="SELECT*FROM student";
try {
List<Map<String, Object>> query = qr.query(sql, new MapListHandler());
for(Map<String,Object> map:query){
for(String key:map.keySet()){
System.out.print(key+" "+map.get(key));
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void mapHandler(){
String sql="SELECT * FROM student";
try {
Map<String, Object> query = qr.query(sql,new MapHandler());
for(String key:query.keySet()){
System.out.println(key+" "+query.get(key));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void scalarHandler(){
String sql="SELECT COUNT(*) FROM student";
try {
Object query = qr.query(sql, new ScalarHandler<Object>());
System.out.println(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void columnListHandler(){
String sql="SELECT *FROM student";
try {
List<Object> query = qr.query(sql, new ColumnListHandler<Object>("name"));
for(Object obj:query){
System.out.println(obj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void beanListHandler(){
String sql="SELECT*FROM student";
try {
List<student> query = qr.query(sql, new BeanListHandler<>(student.class));
for(student stu:query){
System.out.println(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void beanHandler(){
String sql="SELECT*FROM student";
try {
student query = qr.query(sql, new BeanHandler<>(student.class));
System.out.println(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void arrayListHandler(){
String sql ="SELECT*FROM student";
try {
List<Object[]> query = qr.query(sql, new ArrayListHandler());
for(Object[] obj:query){
for(Object o:obj){
System.out.print(o);
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void arrayHandler(){
String sql="SELECT * FROM student";
try {
Object[] q = qr.query(sql,new ArrayHandler());
for(Object obj:q){
System.out.println(obj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delete(){
String sql="DELETE FROM student WHERE id=?";
try {
qr.update(sql,7);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("删除失败");
}
}
public static void insert(){
String sql="INSERT INTO student(name,sex,age)VALUES(?,?,?)";
Object[] param={"王力宏","男",36};
try {
qr.update(sql,param);
} catch (SQLException e) {
System.out.println("添加数据失败");
}
}
}