public class jdbcdemo2 { //0.创建工程,导入jar驱动包 public static void main(String[] args) throws Exception { //1.注册驱动( DriverManager 1.注册驱动 2.获取数据库连接) //Class.forName("com.mysql.jdbc.Driver"); //2.创建连接(如果连接的是本机的mysql并且端口号为3306,可以简化书写) //String url = "jdbc:mysql://127.0.0.1:3306/db1"; String url = "jdbc:mysql:///db1?useSSL = false"; String uesrname = "root"; String password = "1234"; Connection conn = DriverManager.getConnection(url, uesrname, password); //3.定义sql String sql = "select * from account"; //4.获取statement对象 Statement stmt = conn.createStatement(); //5.执行sql ResultSet resultSet = stmt.executeQuery(sql); //6.处理结果:遍历resultset中所有数据 //6.1光标向下移动一行,并且判断当前行是否有数据 //创建集合 List<Account> list = new ArrayList<>(); while(resultSet.next()){ //6.2获取数据 getxxx(); Account account = new Account(); int id = resultSet.getInt("id"); String name = resultSet.getString("name"); double money = resultSet.getDouble("money"); //赋值 account.setId(id); account.setName(name); account.setMoney(money); //存入集合 list.add(account); } System.out.println(list); resultSet.close(); stmt.close(); conn.close(); } }
resultset案例
最新推荐文章于 2022-07-11 08:45:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)