mybatis学习笔记(一):原生态jdbc程序问题总结

原生态jdbc程序问题总结

jdbc程序

使用jdbc查询mysql数据库中用户表的记录

代码示例:

import java.sql.*;

/**
 * <p>Title: NaturalJdbcTest</p>
 * <p>Description: 原生JDBC测试程序</p>
 * <p>Company: www.igeeker.com</p>
 *
 * @author kay
 * @version v1.0
 * @date 2018/7/19下午2:58
 */
public class NaturalJdbcTest {

    public static void main(String[] args) {

        //数据库连接
        Connection connection = null;
        //预编译的Statement,使用预编译的Statement可以提高数据库性能
        PreparedStatement preparedStatement = null;
        //结果集
        ResultSet resultSet = null;

        try {

            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            //通过驱动管理类获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8", "账号", "密码");

            //定义sql语句 ?表示占位符
            String sql = "select * from user where username=?";
            //获取预处理statment
            preparedStatement = connection.prepareStatement(sql);

            //设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
            preparedStatement.setString(1, "张三丰");

            // 向数据库发出sql执行查询,查出结果集
            resultSet = preparedStatement.executeQuery();

            //遍历查询结果集
            while (resultSet.next()) {
                System.out.println(resultSet.getString("id") + " " + resultSet.getString("username"));
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            //释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}

问题总结

问题1:数据库连接问题

看以下代码:

//通过驱动管理类获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8", "root", "Mysqltest@123098");
if (connection != null) {
    try {
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

可以看出问题:数据库连接,使用时就创建,不使用时立即释放,对数据库进行频繁连接开启和关闭,会造成数据库资源浪费,影响数据库性能

解决方案:使用数据库连接池管理数据库连接

问题2:sql语句硬编码

看以下代码:

//定义sql语句 ?表示占位符
String sql = "select * from user where username=?";

可以看出问题:将sql语句硬编码到java代码中,如果sql语句修改了,那么需要重新编译java代码,不利于系统维护

解决方案:将sql语句配置在xml文件中, 即使sql语句变化了,也不需要对java代码进行重新编译

问题3:占位符位置和参数值硬编码

看以下代码:

//设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
preparedStatement.setString(1, "张三丰");

可以看出问题:向preparedStatement中设置参数,将占位符位置和设置参数值硬编码到java代码中,不利于系统维护

解决方案:将sql语句及占位符和参数都配置在xml中

问题4:遍历结果集数据硬编码

看一下代码:

//遍历查询结果集
while (resultSet.next()) {
    System.out.println(resultSet.getString("id") + " " + resultSet.getString("username"));
}

可以看出问题:从resultSet中遍历结果集数据时,存在硬编码,将获取的表字段硬编码了,不利于系统维护

解决方案:将查询的结果集,自动映射成java对象

总结

在实际开发中,一般不推荐直接使用原生jdbc方式进行数据库相关操作。

笔者将开始mybatis的学习之路,寻找对应的解决方案。

猜你喜欢

转载自blog.csdn.net/kuangay/article/details/81117331
今日推荐