要求:请用JDBC做此题,不能任何工具类,可以自己封装工具类。
现在有一个Student表,有ID,age,name,sex,score这些列(IP为127.0.0.1数据库名为stdb,连接数据库的用户名和密码为admin 和 123456)
① 查询分数在80分以上的所有学生信息。
② 把张三学生的分数改成100
③ 请查询出年龄大于20岁的女生信息,,并存放到集合中,打印到控制台上。
public class JdbcDemo {
@Test
public void fun() throws ClassNotFoundException, SQLException{
String drivername = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/stdb";
String name = "admin";
String password="123456";
Class.forName(drivername);
Connection con = DriverManager.getConnection(url,name,password);
Statement state = con.createStatement();
String sql = "select * from Student where score>80";
String sql2 = "update Student set score='100'where name='张三'";
String sql3 = "select * from Student where sex='女'and age>20";
String[] arr = {sql,sql2,sql3};
ResultSet rs = state.executeQuery(sql3);
for (int i = 0; i < arr.length; i++) {
}
//ResultSet rs2 = state.executeUpdate(sql2);
ArrayList list = new ArrayList();
while (rs.next()) {
int id = rs.getInt(1);
int age = rs.getInt(2);
String usename = rs.getString(3);
String sex = rs.getString(4);
int score = rs.getInt(5);
list.add(id);
list.add(age);
list.add(usename);
list.add(sex);
list.add(score);
System.out.println(id+"……"+age+usename+sex+score);
}
con.close();
state.close();
rs.close();
}
}
没写出来,写成这样了,怎么改才符合题意
|
|