MyBatis系列一:JDBC编程
来源:互联网 发布:kmeans聚类算法数据集 编辑:程序博客网 时间:2024/06/06 04:15
看一个JDBC访问数据库的例子:
import java.io.Serializable;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;class User implements Serializable { /** * */ private static final long serialVersionUID = -2270076124239939932L; private Long id; private String name; private int age; public User() { super(); } public User(Long id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; }}public class JdbcExample { private static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; private static final String DB_MYSQL_URL = "jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"; private static final String DB_MYSQL_USER = "root"; private static final String DB_MYSQL_PASSWORD = "123456"; private static final String GET_USER_SQL = "select id, name, age from user where id = ?"; public User getUser(Long id) { Connection connection = getConnection(); PreparedStatement preparedStatement = null; ResultSet resultSet = null; User user = null; try { preparedStatement = connection.prepareStatement(GET_USER_SQL); preparedStatement.setLong(1, id); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { String name = resultSet.getString("name"); int age = resultSet.getInt("age"); user = new User(id, name, age); } } catch (SQLException e) { e.printStackTrace(); } finally { closeConnection(resultSet, preparedStatement, connection); } return user; } private static Connection getConnection() { Connection connection = null; try { Class.forName(DRIVER_CLASS_NAME); connection = DriverManager.getConnection(DB_MYSQL_URL, DB_MYSQL_USER, DB_MYSQL_PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } private static void closeConnection(ResultSet resultSet, Statement statement, Connection connection) { try { if (resultSet != null && !resultSet.isClosed()) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (statement != null && !statement.isClosed()) { statement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { JdbcExample jdbcExample = new JdbcExample(); User user = jdbcExample.getUser(1L); System.out.println(user); }}
这里因为使用了6.0.5版本的MySQL连接驱动,URL增加了serverTimezone=UTC参数
JDBC编程的步骤:
1. 加载JDBC驱动,注册连接信息
2. 操作Connection,并获取数据库连接
3. 创建JDBC Statement对象
4. 设置SQL并传递参数
5. 通过Statement执行SQL,返回结果到ResultSet对象
6. 使用ResultSet读取数据,然后通过代码转化为具体的POJO对象
7. 关闭数据库相关资源
这里需要转发一个链接:
从JDBC到MyBatis:http://chenjc-it.iteye.com/blog/1455688
分析的非常好
0 0
- MyBatis系列一:JDBC编程
- Mybatis学习(一)原生态的JDBC编程总结
- JDBC系列一:初识JDBC
- JDBC系列一
- JDBC编程(一)
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- 深入浅出Mybatis系列(一)---Mybatis入门
- MyBatis Generator系列(一)----MyBatis Generator简介
- Mybatis系列(一)入门
- Mybatis系列(一)入门
- mybatis入门系列(一)
- mybatis系列一:入门篇
- 【MyBatis】1:JDBC编程存在的问题
- springmvc spring hibernate maven pom.xml
- 【Android】安卓开发之将上一个Activity的数据传递给下一个Activity
- java中volatile关键字的含义
- 解决 error C1083: 无法打开包括文件:“jni.h”: No such file or directory
- 第54篇一对多实现之学生端代码合成iframe研究及array_rand及array_product及array_pop
- MyBatis系列一:JDBC编程
- Log4j使用教程
- Tomcat无法正常工作
- Android 获取浏览器当前分享页面的截屏
- C++学习笔记 c++引用(&)与指针(*)的区别
- Android实现横竖屏、全屏的几种方式
- 使用原生jdbc查询数据库时存在的问题及对策
- ubuntu 创建Django工程出现:Cannot find installed version of python-django or python3-django.
- 四大湖(C程序设计进阶 第2周)