要想利用代码读取数据库中的数据,首先要把驱动复制到eclipse的项目中,驱动可以从官网下载:
1--在项目中新建一个文件夹lib
2--把下载好的驱动拖入文件夹中。
3--进行下面的操作,应用并关闭即可。
4.然后就会出现:
驱动就安装完成了。
1.定义一个学生类javabean:
package lession220721;
import java.util.Date;
public class Student {
private Integer id;
private String stuId;
private String name;
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
private Date birthday;
private Float weight;
private Integer height;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.实现数据库的连接和读取数据:
package lession220721;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class Ybw {
public static void main(String[] args) {
Connection conn = null;
try {
// 第1步:加载数据库驱动,参数为驱动类名称
// 如果项目没有此类,则会进入异常
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/students?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
// 第2步:获取数据库连接
// 第1个参数是数据库连接字符串
// 第2个参数是数据库用户
// 第3个参数是数据库密码
conn = DriverManager.getConnection(url, "root", "123456");
// 第3步:创建传输sql的载体
Statement stmt = conn.createStatement();
// 第4步:向数据库发送sql指令
String sql = "select id,stu_id,`name`,sex,birthday,weight,height,email from t_student where id > 3 and id<15";
// 返回一个结果集
ResultSet rs = stmt.executeQuery(sql);
System.out.println("id\t" + "stuId\t" + "name \t" + "sex" + "\t" + "birthday\t" + "weight\t" + "height\t"
+ "email");
// 第5步:从结果集中取出想要的数据
while (rs.next()) {
// 取出一行的数据
Integer id = rs.getInt("id");// 列名或别名
String stuId = rs.getString("stu_id");
String name = rs.getString("name");
String sex = rs.getString("sex");
Date birthday = rs.getDate("birthday");
Float weight = rs.getFloat("weight");
Integer height = rs.getInt("height");
String email = rs.getString("email");
// ORM:对象关系映射。mybatis。
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setStuId(stuId);
stu.setSex(sex);
stu.setBirthday(birthday);
stu.setWeight(weight);
stu.setHeight(height);
stu.setEmail(email);
System.out.println(stu.getId() + "\t" + stu.getStuId() + "\t" + stu.getName() + " \t" + stu.getSex()
+ "\t" + stu.getBirthday() + "\t" + stu.getWeight() + "\t" + stu.getHeight() + "\t"
+ stu.getEmail());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
// 第6步:关闭数据库
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
输出结果:
String url = "jdbc:mysql://127.0.0.1:3306/students?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
127.0.0.1是IP地址,连接的是本地数据库。
3306是端口号。
students是数据库表名。