JDBC(链表存储数据)
- 使用类来建立数据库的数据模板,进而通过对象来存储数据库实例
- 使用链表来存储数据对象
详情见注释
主类
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo3 {
public static void main(String[] args) {
String sql = null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "root");
//获取执行sql的对象
statement = connection.createStatement();
//为sql赋值
sql = "SELECT * FROM STUDENT3";
//执行sql,将结果保存在ResultSet类型的结果集里
resultSet = statement.executeQuery(sql);
//声明list引用,接受student3Findall类型的值
List<student3Findall> list = null;
//声明数据库中表对应的类在循环外部,使引用复用
student3Findall student3Findalls = null;
//为list引用创建对象
list = new ArrayList<student3Findall>();
//循环遍历结果集
while (resultSet.next()) {
//创建 student3Findall 类型的对象,并为其赋值
student3Findalls = new student3Findall();
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
int age = resultSet.getInt(3);
String sex = resultSet.getString(4);
String address = resultSet.getString(5);
int math = resultSet.getInt(6);
int english = resultSet.getInt(7);
student3Findalls.setId(id);
student3Findalls.setAge(age);
student3Findalls.setSex(sex);
student3Findalls.setEnglish(english);
student3Findalls.setMath(math);
student3Findalls.setName(name);
student3Findalls.setAddress(address);
//将对象装载进入列表
list.add(student3Findalls);
}
System.out.println(list);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//释放资源
//防止有空,造成资源未完全释放
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
数据库数据格式类
import java.sql.ResultSet;
import java.util.List;
public class student3Findall {
private int id;
private String name;
private String sex;
private int age;
private String address;
private int math;
private int english;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
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 int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return "student3Findall{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", address='" + address + '\'' +
", math=" + math +
", english=" + english +
'}';
}
}