//查询
//1,通过ID查询
@Test
public void findById() throws ClassNotFoundException, SQLException{
int id=2;
String find_id_sql="select * from user where id="+id;
//第一步:加载驱动
Class.forName(driver);
//第二步:创建数据库连接
Connerction connection=DriverManager,getConnection(url,username,password);
//第三步:创建SQL操作对象
Statement statement=connection.createStatement();
//第四步:执行SQL语句(查询SQL语句有结果集)用的是excuteQuery()。
ResultSet resultSet=statement.executeQuery()。
//第五步:取出查询出来的结果集中的表记录,用next方法
while (resultSet . next()){ //next()方法是指南针指向表时,看有没有记录,有记录就true,没有记录就false
//get0bject() ,getInt() , getString(), getDouble() , getDate()........
int ids=resultSet. getInt( columnLabel: "id");
String username=resultSet . getString( columnLabel: "username");
String password=resultSet . getString( columnLabel: "password");
string nickname=resultSet . getString( columnLabel: "nickname" ) ;
System. out . println(ids+":"+username+" : "+password+": "+nickname) ; }
//第六步:释放资源
resultSet. close();
statement . close() ;
connection. close();
}
//2.查询所有记录
@Test
pubLic void findALL( throws CLassNotFoundException, SQLException {
String find_ all_sql="seLect * from user" ;
//第一步:加载驱动
Class . forName(driver);
//第二步:创建数据库连接
Connection connection=DriverManager . ge tConnection(url, username,password);
//第三步:创建SQL操作对象
Statement statement=connection . createStatement();
//第四步:执行SQL语句(查询SQL语句有结果集)用的是excuteQuery()。
ResuLtSet resultSet=statement . executeQuery(find_ all_ sql);
//第五步:取出查询出来的结果集中的表记录,用next()方法
while (resultSet . next()){ //next(方法是指针指向表时,看有没有记录,有记录就true,没有记录就false //getobject() , getInt(),getString(),getDouble(),getDate()........
int ids=resultSet 。getInt( columnLabel: "id");
String username=resultSet . getstring( columnLabel: "username ");
String password=resultSet . getString( columnLabel: "password") ;
String nickname=resultSet. getString( columnLabel: "nickname" ) ;
System. out. printLn(ids+":" +username+" :"+password+" :" +nickname);
}
//第六步:释放资源
resultSet . close();
statement . cLose();
connection. close() ;
}