一,封装对象
package JDBC;
public class student {
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
二,链接数据库并且查询返回List对象
package JDBC;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class my2 {
public List<student> findAll() throws ClassNotFoundException, SQLException {
List<student> list=null;
Connection connection=null;
Statement statement=null;
ResultSet re=null;
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库链接对象
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/day17","root","18388543714a");
// 定义SQL
String sql="select * from user";
//获取执行对象
statement=connection.createStatement();
// 执行SQL
re=statement.executeQuery(sql);
student student =null;
list=new ArrayList<student>();
while (re.next()){
// 获取数据
int id=re.getInt("id");
String name=re.getString("name");
String gender=re.getString("gender");
int age=re.getInt("age");
String address=re.getString("address");
String qq=re.getString("qq");
String email=re.getString("email");
String username=re.getString("username");
String password=re.getString("password");
// 创建student对象
student =new student();
student.setId(id);
student.setName(name);
student.setGender(gender);
student.setAge(age);
student.setAddress(address);
student.setQq(qq);
student.setEmail(email);
student.setUsername(username);
student.setPassword(password);
// 装载集合
list.add(student);
}
if (connection!=null){
connection.close();
}
if (statement!=null){
statement.close();
}
if (re!=null){
re.close();
}
return list;
}
}
三.测试
package JDBC;
import java.sql.SQLException;
import java.util.List;
public class ListTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
List<student> list=new my2().findAll();
for (student ss:list){
System.out.println(ss.getName()+"..."+ss.getAge());
}
}
}
?
|