zoukankan      html  css  js  c++  java
  • mybatis学习(二)

      1.了解原生jdbc程序问题

    代码:

    package cn.itcast.mybatis.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    /**
     * 
     * <p>Title: JdbcTest</p>
     * <p>Description:通过单独的jdbc程序,总结其中的问题 </p>
     * <p>Company: www.itcast.com</p> 
     * @author    传智.燕青
     * @date    2015-4-22上午9:16:05
     * @version 1.0
     */
    public class JdbcTest {
        
        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=utf-8", "root", "root");
                //定义sql语句 ?表示占位符
                String sql = "select * from user where username = ?";
                //获取预处理statement
                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) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(preparedStatement!=null){
                    try {
                        preparedStatement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
    
        }
    }
    jdbc代码

    由上面代码可以引申出几个问题:

    1.数据库连接,使用时就创建,不使用就立即释放,对数据库连接的频繁开启和关闭,造成数据库资源的浪费,影响数据库性能(connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "root"); ---- conn.close())

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

    2.将sql语句硬编码到java代码程序中,如果sql语句修改,则需要重新编译java代码。不利于系统的维护。
    解决方案:将sql语句配置到xml文件中,即使sql有变化。也不需要对java重新编译。

    3.在向preparedStatement设置参数的时候,对占位符位置和设置参数值,硬编码在java代码中。
    解决方案:将sql语句以及占位符和参数全部配置在xml中

    4.在resultset中遍历结果集数据时,存在硬编码,将获取表的字段进行硬编码,不利于维护。
    解决方案:将查询的结果集,自动的映射成java对象。

  • 相关阅读:
    字符串的基本操作
    PHP & Delphi 語法
    Delphi项目构成之单元文件PAS
    Delphi项目构成之项目文件DPR
    Delphi项目的构成
    關於那我的編程歷史..
    點擊Button,在Label1顯示HelloWorld!。
    開博客了, 因為搞Delphi 開發的關於Delphi學習
    Java 基础知识(一)
    关于多线程对于全局变量的资源竞争问题
  • 原文地址:https://www.cnblogs.com/feiguo/p/8467597.html
Copyright © 2011-2022 走看看