resultset-->executeQuery
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
-
- public class Demo3 {
- public static void main(String[] args) throws Exception {
- //1.注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- //2.获取连接对象//url数据库
- Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","root");
- //3.获取执行SQL语句的对象
- Statement statement=connection.createStatement();
- //4.执行SQL语句,
- //resultset以表结构进行临时结果存储,要通过jdbc api 将其依次获取
- ResultSet resultSet= statement.executeQuery("select *from user");
- //5.处理结果
- while(resultSet.next()){//判断下一行是否有数据;
- //对当前列编号数据进行获取,根据列的编号
- int id= resultSet.getInt(1);
- String username= resultSet.getString(2);
- String password= resultSet.getString(3);
- System.out.println(id+"\t"+username+"\t"+password);
- }
- //6.释放资源
- resultSet.close();
- statement.close();
- connection.close();
- }
- }