引言

学过 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缺点分析

  1. 访问数据库每次都需要加载数据库驱动(驱动名称硬编码)

  2. 访问数据库每次都需要获取数据库连接(连接信息硬编码)

  3. 设置参数需要判断参数类型、参数对应的下标

  4. 结果集的数据类型需要手动判断、列名需要手动填写(重点)

  5. 获取的字段值需要逐个手动设置到实体类中(重点)

  6. 访问数据库结束后每次都要手动关闭连接

MyBatis 回顾 JDBC(一)的更多相关文章

  1. Mybatis解决jdbc编程的问题

    1.1.1  Mybatis解决jdbc编程的问题 1.  数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig.xml中配置 ...

  2. Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案

    转自http://www.cnblogs.com/fnz0/p/5713102.html 不知道自己什么时候才有这种钻研精神- -. 1      背景 系统中需要批量生成单据数据到数据库表,所以采用 ...

  3. JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  4. MyBatis、JDBC相关知识

    引言 在mybatis的基础知识中我们已经可以对mybatis的工作方式窥斑见豹.但是,为什么还要要学习mybatis的工作原理?因为,随着mybatis框架的不断发展,如今已经越来越趋于自动化,从代 ...

  5. 为什么使用Mybatis对JDBC进行包装探究

    一.原生JDBC在实际生产中使用存在的影响性能的问题 首先分析使用JDBC的代码: Connection connection = null; PreparedStatement preparedSt ...

  6. (七) SpringBoot起飞之路-整合SpringSecurity(Mybatis、JDBC、内存)

    兴趣的朋友可以去了解一下前五篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Sprin ...

  7. MyBatis通过JDBC生成的执行语句问题

    我们编程的过程中大部分使用了很出色的ORM框架,例如:MyBatis,Hibernate,SpringJDBC,但是这些都离不开数据驱动JDBC的支持.虽然使用起来很方便,但是碰到一些问题确实很棘手, ...

  8. MyBatis知多少(17)MyBatis和JDBC

    有了MyBatis,就不再需要编写JDBC代码了.像JDBCT这样的API的确非常强大,但使用起来总不免觉得太过繁琐.代码清单给出了一个使用JDBC的示例. 从这个例子中很容易看出,JDBC API会 ...

  9. MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程

    虽然MySQL的存储过程,一般情况下,是不会使用到的,但是在一些特殊场景中,还是有需求的.最近遇到一个sql server向mysql迁移的项目,有一些sql server的存储过程需要向mysql迁 ...

随机推荐

  1. 「新特性」Spring Boot 全局懒加载机制了解一下

    关于延迟加载 在 Spring 中,默认情况下所有定的 bean 及其依赖项目都是在应用启动时创建容器上下文是被初始化的.测试代码如下: @Slf4j @Configuration public cl ...

  2. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作centos6.5-14

    自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像制作centos6.5-14 欢迎加QQ群:1026880196 进行交流学习 制作OpenSta ...

  3. Day13_69_yield()

    yield() 方法 - Thread.yield() 该方法是一个静态方法, 作用是给同一个优先级的线程让位,阻塞当前线程,但是阻塞时间不可以指定,是随机的. - Thread.yield() 和 ...

  4. 痛!痛!痛!我们的好兄弟Git,一路走好!

    文章是正经文章,标题不要在意,哈哈 Git作为现在主流的版本控制工具,但是如何在软件开发过程中进行合理的分支管理是一个见仁见智的问题. 接下来我会对比下现有的几种比较普遍的分支管理方式和之前在阿里时候 ...

  5. 1.7.9- HTML合并单元格实例

  6. 01- Java概述

    一 Java简介 java语言发展史 发展史简单了解:如下: https://www.jianshu.com/p/a78fcb3ccf63 java语言平台 JavaSE(标准版):可以用户开发普通桌 ...

  7. 总结:composer的install和require和update指令。到底什么时候用什么指令

    https://packagist.org  相当于是应用商店

  8. php的call_user_func_array()使用场景

    1..动态调用普通函数时,比如参数和调用方法名称不确定的时候很好用 function sayEnglish($fName, $content) { echo 'I am ' . $content; } ...

  9. MySql 按日,按周,按月 分组 统计数据

    知识关键词:DATE_FORMAT 按天统计: SELECT DATE_FORMAT(create_time,'%Y%m%d') days, COUNT(caseid) count FROM tc_c ...

  10. 在Android的App中动态的加载Java类

    原文的地址:http://yenliangl.blogspot.com/2009/11/dynamic-loading-of-classes-in-your.html 我正在编写一个应用程序能够加载别 ...