//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);
//创建数据库链接
Connection connect ion=DriverManager . getConnection(url, username , password);
//第三步:创建SQL操作对象
Statement statement-connection . createStatement() ;
//第四步;执行SQL语句(查询SQL语 句有结果集)用的是excuteQuery()。
ResultSet resultSet=statement . executeQuery(find id_ sql);
//第五步:取出查询出来的结果集中的表记录,用next()方法
while (resultSet . next())f //next()5tÆîtåoÆßf, іяпіВж, f iжitrue, QTiitfalse //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_ sqU);
//第五步:取出查询出来的结果集中的表记录,用next()方法
while (resultSet . next()){ //next(方法是指针指向表时,看有没有记录,有记录就true,没有记录就false //getobject() , getInt(),getString(),getDouble(),getDate(........
int ids=resultSet 。getInt( columnLabel: "id");I
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() ;
}