-
-
-
import java.sql.*;
public class jdbcdome_ResultSet {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/homework?useSSl=false";
String user="root";
String password="1234";
Connection connection = DriverManager.getConnection(url, user, password);
String sql="select *from lyj";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
int id = resultSet.getInt(1);
System.out.println(id);
}
statement.close();
connection.close();
resultSet.close();
}
} - 练习:
- 代码实现:
public class ResultSet练习 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/homework?useSSL=false";
String user="root";
String password="1234";
Connection connection = DriverManager.getConnection(url, user, password);
String sql="select * from lyj";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
List<account> list=new ArrayList<>();
while (resultSet.next()){
account account=new account();
int id = resultSet.getInt(1);
String sid = resultSet.getString(2);
account.setId(id);
account.setSid(sid);
list.add(account);
}
System.out.println(list);
}
}
class account{
private int id;
private String sid;
public account() {
}
public account(int id, String sid) {
this.id = id;
this.sid = sid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
@Override
public String toString() {
return "account{" +
"id=" + id +
", sid='" + sid + '\'' +
'}';
}
}