Bootstrap

Java的JDBC的ResultSet的简单理解

Java的JDBC的ResultSet的简单理解

import java.sql.*;

public class JDBC06{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.导入驱动jar包
        //2.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //3.获取数据库连接对象
        //Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/hsp_db02","root","011012");
        Connection con= DriverManager.getConnection("jdbc:mysql:///hsp_db02","root","011012");
        //4.定义sql
        String sql="select * from account";
        //5.获取定义sql语句的对象 statement
        Statement stm=con.createStatement();
        //6.执行sql,接受返回结果
        ResultSet rs =stm.executeQuery(sql);
        //7.处理结果
        //7.1让游标向下移动一行
        rs.next();
        //7.2获取数据
        int id=rs.getInt(1);
        String name=rs.getString("name");
        double balance=rs.getDouble(3);
        System.out.println(id+"----"+name+"----"+balance);
        //8.释放资源
        stm.close();
        con.close();
    }
}
;