import jdbcutile.ResultsetHandler;
import jdbcutile.mysqlutil;
import jdbcutile.sqlcaozuo;
import jdbcutile.sqlcaozuoimpl;
import javax.naming.Name;
import javax.sound.midi.Soundbank;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;
public class c3p0test { //创建 HandleSql 类
static Connection con; //声明 Connection 对象
static PreparedStatement pStmt;//声明预处理 PreparedStatement 对象
static ResultSet res;//声明结果 ResultSet 对象
static String url = "jdbc:mysql://localhost:3306/stus";
static String user = "root";
static String password = "jiangshao888";
// public Connection getConnection() {//建立返回值为 Connection 的方法
//
// //代码块(1):加载数据库驱动类
// try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// System.out.println("数据库驱动加载成功");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
//
// //代码块(2):通过访问数据库的URL获取数据库连接对象
// try {
// con = DriverManager.getConnection(url,user,password);
// System.out.println("数据库连接成功");
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// return con;
// }
public static void main(String[] args) throws Exception {//主方法
sqlcaozuo sqlcaozuo=new sqlcaozuoimpl();
String sql="select * from stu";
List<people> sname = sqlcaozuo.query(sql, new ResultsetHandler<List<people>>() {
@Override
public List<people> handle(ResultSet re) throws Exception {
List<people> strings = new ArrayList<>();
while (re.next()) {
// strings.add(re.getString("sname"));
people peo = new people(re.getInt("sid"),
re.getString("sname") ,
re.getString("gender") ,
re.getString("phone"),
re.getString("birthday"),
re.getString("hobby"),
re.getString("info"));
strings.add(peo);
}
return strings;
}
});
for (int i = 0; i < sname.size(); i++) {
System.out.println(
"id:"+sname.get(i).getSid()+
" 姓名:"+sname.get(i).getSname()+
" 性别:"+sname.get(i).getGender()+
" 电话:"+sname.get(i).getPhone()+
" 生日:"+sname.get(i).getBrithday()+
" 爱好:"+sname.get(i).getHobby()+
" 简介:"+sname.get(i).getInfo()
);
}
System.out.println("删除第二条-------");
sql="delete from stu where sid=?";
sqlcaozuo.updexe(sql,2);
System.out.println("再次查询-------");
sql="select * from stu";
sname = sqlcaozuo.query(sql, new ResultsetHandler<List<people>>() {
@Override
public List<people> handle(ResultSet re) throws Exception {
List<people> strings = new ArrayList<>();
while (re.next()) {
// strings.add(re.getString("sname"));
people peo = new people(re.getInt("sid"),
re.getString("sname") ,
re.getString("gender") ,
re.getString("phone"),
re.getString("birthday"),
re.getString("hobby"),
re.getString("info"));
strings.add(peo);
}
return strings;
}
});
for (int i = 0; i < sname.size(); i++) {
System.out.println(
"id:"+sname.get(i).getSid()+
" 姓名:"+sname.get(i).getSname()+
" 性别:"+sname.get(i).getGender()+
" 电话:"+sname.get(i).getPhone()+
" 生日:"+sname.get(i).getBrithday()+
" 爱好:"+sname.get(i).getHobby()+
" 简介:"+sname.get(i).getInfo()
);
}
// System.out.println("-----------------增加15条记录---------------");
// // INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing')
// for(int i=0;i<15;i++){
// sql="INSERT INTO stu VALUES (null, ?,?,00,222,22,11)";
// sqlcaozuo.updexe(sql,"夜枫","hello");
// }
System.out.println("再次查询-------");
sql="select * from stu";
sname = sqlcaozuo.query(sql, new ResultsetHandler<List<people>>() {
@Override
public List<people> handle(ResultSet re) throws Exception {
List<people> strings = new ArrayList<>();
while (re.next()) {
// strings.add(re.getString("sname"));
people peo = new people(re.getInt("sid"),
re.getString("sname") ,
re.getString("gender") ,
re.getString("phone"),
re.getString("birthday"),
re.getString("hobby"),
re.getString("info"));
strings.add(peo);
}
return strings;
}
});
for (int i = 0; i < sname.size(); i++) {
System.out.println(
"id:"+sname.get(i).getSid()+
" 姓名:"+sname.get(i).getSname()+
" 性别:"+sname.get(i).getGender()+
" 电话:"+sname.get(i).getPhone()+
" 生日:"+sname.get(i).getBrithday()+
" 爱好:"+sname.get(i).getHobby()+
" 简介:"+sname.get(i).getInfo()
);
}
System.out.println("修改-----------------------");
sql="update stu set sname='紫色珊瑚' where sid>3";
sqlcaozuo.updexe(sql);
System.out.println("再次查询-------");
sql="select * from stu";
sname = sqlcaozuo.query(sql, new ResultsetHandler<List<people>>() {
@Override
public List<people> handle(ResultSet re) throws Exception {
List<people> strings = new ArrayList<>();
while (re.next()) {
// strings.add(re.getString("sname"));
people peo = new people(re.getInt("sid"),
re.getString("sname") ,
re.getString("gender") ,
re.getString("phone"),
re.getString("birthday"),
re.getString("hobby"),
re.getString("info"));
strings.add(peo);
}
return strings;
}
});
for (int i = 0; i < sname.size(); i++) {
System.out.println(
"id:"+sname.get(i).getSid()+
" 姓名:"+sname.get(i).getSname()+
" 性别:"+sname.get(i).getGender()+
" 电话:"+sname.get(i).getPhone()+
" 生日:"+sname.get(i).getBrithday()+
" 爱好:"+sname.get(i).getHobby()+
" 简介:"+sname.get(i).getInfo()
);
}
// try {
// // Class.forN