1、Mybatis 总结

发布时间 2023-07-11 15:32:45作者: lidongdongdong~

1、JDBC

public class JDBCTest {

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        PreparedStatement prepareStatement = null;
        ResultSet rs = null;

        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/ssmdemo";
            String user = "root";
            String password = "root";
            connection = DriverManager.getConnection(url, user, password);

            // 获取 statement, preparedStatement
            String sql = "select * from tb_user where id = ?";
            prepareStatement = connection.prepareStatement(sql);
            // 设置参数
            prepareStatement.setLong(1, 1L);
            // 执行查询
            rs = prepareStatement.executeQuery();

            // 处理结果集
            while (rs.next()) {
                System.out.println(rs.getString("user_name"));
                System.out.println(rs.getString("name"));
                System.out.println(rs.getInt("age"));
                System.out.println(rs.getDate("birthday"));
            }
        } finally {
            // 关闭连接, 释放资源
            if (rs != null) rs.close();
            if (prepareStatement != null) prepareStatement.close();
            if (connection != null) connection.close();
        }
    }
}

2、