zoukankan      html  css  js  c++  java
  • 单例模式和JDBC

    配置文件:
    
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/blog
    user=root
    user=1234
    
    properties文件里面通常存放的是Map,也就是键值对。

    单例模式,是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例。即一个类只有一个对象实例

    话说JDBC
    
    Java通过JDBC连接数据库分为以下四步:
    1.加载驱动
    2.建立连接
    3.使用Preparedstatement进行预编译或者使用statement对象
    4.释放资源
    
    
    Preparedstatement和Statement的比较:
    a.首先Preparedstatement是预编译的,而Statement反之。
    b.Preparedstatement支持批量处理,而Statement不行。
    c.Preparedstatement相对于Statement可读性上良好
    d.无论从哪个方面看Preparedstatement都比Statement要好(不相信的话可以自己写一个
    小的图书管理系统或则其它的小项目都可以,一个用Preparedstatement一个用Statement进行对比
    就知道了)

    /**
    单例模式
    */
    package
    com.utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ConfigManager { /**之所以定义为静态的是因为静态修饰可直接调用,不需要实例化,直接类名.方法名即可调用 */ private static ConfigManager config; private static Properties props; private ConfigManager(){ props=new Properties(); } public static ConfigManager getInstance(){ if(config==null){ config=new ConfigManager(); } return config; } public static String getPropertis(String key){ try { FileInputStream fis=new FileInputStream("src/db.properties"); props.load(fis); return props.getProperty(key); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }

    通过JDBC和数据库打交道

    /**
     * JDBC连接数据库
     */
    
    
    package com.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.utils.ConfigManager;
    
    public class BaseDao {
        
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        //与数据库建立连接
        public boolean getConnection(){
            String driver=ConfigManager.getInstance().getPropertis("driver");
            String url=ConfigManager.getInstance().getPropertis("url");
            String user=ConfigManager.getInstance().getPropertis("user");
            String password=ConfigManager.getInstance().getPropertis("password");
            try {
           //加载驱动 Class.forName(driver);
           //建立连接 conn
    =DriverManager.getConnection(url,user,password); return true; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } //所有查询方法的模板 public ResultSet select(String sql,Object[]obj){ getConnection(); try {
    //使用Preparedstatement对象预编译sql ps
    =conn.prepareStatement(sql); for (int i = 0; i < obj.length; i++) { ps.setObject(i+1,obj[i]); } rs=ps.executeQuery(); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } //所有增删改方法的模板 public int update(String sql,Object[]obj){ getConnection(); try { ps=conn.prepareStatement(sql); for (int i = 0; i < obj.length; i++) { ps.setObject(i+1,obj[i]); } int lines=ps.executeUpdate(); return lines; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } //释放资源 public boolean close(){ try { if(!rs.isClosed()){ rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } try { if(!ps.isClosed()){ ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } try { if(!conn.isClosed()){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } }




  • 相关阅读:
    行为模式
    行为模式
    行为模式
    行为模式
    行为模式
    结构模式
    kafka 学习整理
    Hive文件格式,以及ORC创建使用
    GBDT 介绍
    机器学习中的特征工程 —— 七月在线总结
  • 原文地址:https://www.cnblogs.com/youcong/p/7841994.html
Copyright © 2011-2022 走看看