MyBatis 回顾 JDBC(一)
引言
学过 Java 的童鞋都知道,在 Java 中只有 JDBC 可以访问数据库,但是只要使用过 JDBC 的同学肯定也感受到 JDBC 访问数据库的繁琐, 需要编写大量的代码,经历一系列的步骤。
以下是一个简单的 JDBC 操作例子,大家感受一下吧。
创建数据库表
#创建表
DROP TABLE IF EXISTS tb_user;
CREATE TABLE tb_user (
id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
userName varchar(32) DEFAULT NULL,
password varchar(32) DEFAULT NULL,
name varchar(32) DEFAULT NULL,
age int(10) DEFAULT NULL,
sex int(2) DEFAULT NULL,
birthday date DEFAULT NULL,
created datetime DEFAULT NULL,
updated datetime DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据
#插入数据
INSERT INTO tb_user ( userName, password, name, age, sex, birthday, created, updated) VALUES ( 'zs', '123456', '张三', '22', '1', '1990-09-02', sysdate(), sysdate());
INSERT INTO tb_user ( userName, password, name, age, sex, birthday, created, updated) VALUES ( 'ls', '123456', '李四', '24', '1', '1993-09-05', sysdate(), sysdate());
Navicat显示结果如下:

User实体类代码
Java 工程 entity包下创建 UserEntity.java 实体类文件
import java.util.Date;
/**
* @author benjamin.xu
* @desc User实体类
* @date 2020/6/17 下午1:12
*/
public class UserEntity {
private int id;
private String userName;
private String password;
private String name;
private int age;
private int sex;
private Date birthday;
private String created;
private String updated;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getUpdated() {
return updated;
}
public void setUpdated(String updated) {
this.updated = updated;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birthday=" + birthday +
", created='" + created + '\'' +
", updated='" + updated + '\'' +
'}';
}
}
JDBC测试代码
Java 工程下创建 JDBCDemo.java 文件,如下:
import entity.UserEntity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @desc JDBC
* @date 2020/6/17 上午9:18
*/
public class JDBCDemo {
public static void main(String[] args) throws Exception{
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://localhost:3306/mybatis"; //数据库URL地址
String user = "root"; //数据库账号
String pwd = ""; //数据库密码
//获取数据库连接
connection = DriverManager.getConnection(url, user, pwd);
//获取preparedStatement
String sql = "select * from tb_user where age>?";
preparedStatement = connection.prepareStatement(sql);
//设置参数
preparedStatement.setLong(1,20);
rs = preparedStatement.executeQuery();
//处理结果集
List<UserEntity> userEntities = new ArrayList<UserEntity>();
while (rs.next()) {
int id = rs.getInt("id");
String userName = rs.getString("userName");
String password = rs.getString("password");
String name = rs.getString("name");
int age = rs.getInt("age");
int sex = rs.getInt("sex");
Date birthday = rs.getDate("birthday");
String created = rs.getString("created");
String updated = rs.getString("updated");
UserEntity userEntity = new UserEntity();
userEntity.setId(id);
userEntity.setUserName(userName);
userEntity.setPassword(password);
userEntity.setName(name);
userEntity.setAge(age);
userEntity.setSex(sex);
userEntity.setBirthday(birthday);
userEntity.setCreated(created);
userEntity.setUpdated(updated);
userEntities.add(userEntity);
}
System.out.println(userEntities);
} finally {
//关闭连接,释放资源
if (rs != null) {
rs.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
}
}
}
运行结果如下:
[UserEntity{id=1, userName='zs', password='123456', name='张三', age=22, sex=1, birthday=1990-09-02, created='2020-06-17 09:30:58.0', updated='2020-06-17 09:30:58.0'}, UserEntity{id=2, userName='ls', password='123456', name='李四', age=24, sex=1, birthday=1993-09-05, created='2020-06-17 09:30:58.0', updated='2020-06-17 09:30:58.0'}]
Process finished with exit code 0
JDBC缺点分析
-
访问数据库每次都需要加载数据库驱动(驱动名称硬编码)
-
访问数据库每次都需要获取数据库连接(连接信息硬编码)
-
设置参数需要判断参数类型、参数对应的下标
-
结果集的数据类型需要手动判断、列名需要手动填写(重点)
-
获取的字段值需要逐个手动设置到实体类中(重点)
-
访问数据库结束后每次都要手动关闭连接

浙公网安备 33010602011771号