@Test

//查询

//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() ;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值