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
原创粉丝点击