MyBatis系列一:JDBC编程

看一个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
分析的非常好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值