1. 程式人生 > >JavaWeb中jdbcproperties配置文件

JavaWeb中jdbcproperties配置文件

package dbcp string test stack 中文 put 支持中文 tor

開發中使用properties配置文件,方便後期維護。

  1. 文件位置: 任意,建議src下
  2. 文件名稱:任意,擴展名為properties
  3. 文件內容:一行一組數據,格式“key=value”  
    1. key 命名自定義,如果是多單詞,習慣使用點分割,例如jdbc.driver
    2. value 值不支持中文,如果有需要使用非英文字符,將進行Unicode轉化

  配置文件只需要加載一次,提供靜態代碼,當前類被加載到內存執行

  1 package com.jdbc.dao;
  2 
  3 import java.sql.Connection;
4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.ResourceBundle; 9 10 11 public class JdbcUtils { 12 13 private static String user; 14 public static String password;
15 public static String className; 16 public static String url; 17 Connection con=null; 18 PreparedStatement pstm=null; 19 ResultSet rs=null; 20 static{ 21 ResourceBundle bundle=ResourceBundle.getBundle("jdbc"); 22 user=bundle.getString("jdbc.user");
23 password=bundle.getString("jdbc.password"); 24 className=bundle.getString("jdbc.className"); 25 url=bundle.getString("jdbc.url"); 26 27 } 28 public JdbcUtils() { 29 try{ 30 Class.forName(className); 31 } catch (ClassNotFoundException e) { 32 e.printStackTrace(); 33 } 34 35 } 36 37 38 39 40 41 public Connection getConnection() { 42 try { 43 con=(Connection) DriverManager.getConnection(url, user, password); 44 } catch (SQLException e) { 45 con=null; 46 e.printStackTrace(); 47 48 } 49 50 return con; 51 } 52 53 54 public ResultSet excuteQuery(String sql, Object[] obj) { 55 if (sql!=null ){ 56 con=getConnection(); 57 if(con!=null){ 58 try { 59 pstm=(PreparedStatement) con.prepareStatement(sql); 60 if (obj!=null) { 61 for(int i=0;i<obj.length;i++){ 62 pstm.setObject(i+1,obj[i]); 63 } 64 } 65 66 rs=pstm.executeQuery(); 67 68 } catch (SQLException e) { 69 e.printStackTrace(); 70 } 71 } 72 } 73 74 return rs; 75 } 76 77 78 public int excuteUpdate(String sql, Object[] obj) { 79 // TODO Auto-generated method stub 80 int flag=-1; 81 if(sql!=null && obj!=null){ 82 con=getConnection(); 83 84 if (con!=null) { 85 try { 86 pstm=(PreparedStatement) con.prepareStatement(sql); 87 for(int i=0;i<obj.length;i++){ 88 pstm.setObject(i+1, obj[i]); 89 } 90 flag=pstm.executeUpdate(); 91 92 } catch (SQLException e) { 93 e.printStackTrace(); 94 } 95 } 96 } 97 return flag; 98 } 99 100 public ResultSet queryAll(String sql) { 101 102 con=getConnection(); 103 if(con!=null){ 104 try { 105 pstm=(PreparedStatement) con.prepareStatement(sql); 106 rs=pstm.executeQuery(); 107 } catch (SQLException e) { 108 e.printStackTrace(); 109 } 110 } 111 112 113 return rs; 114 115 } 116 117 118 public void closeAll() { 119 // TODO Auto-generated method stub 120 if (rs!=null) { 121 try { 122 rs.close(); 123 } catch (SQLException e) { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 } 128 if (pstm!=null) { 129 try { 130 pstm.close(); 131 } catch (SQLException e) { 132 // TODO Auto-generated catch block 133 e.printStackTrace(); 134 } 135 136 } 137 if (con!=null) { 138 try { 139 con.close(); 140 } catch (SQLException e) { 141 // TODO Auto-generated catch block 142 e.printStackTrace(); 143 } 144 } 145 } 146 147 148 }

配置文件 jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore

加載配置文件:properties對象

開發中會使用Properties對象進行, 我們可以采用加載properties 文件獲得流,然後使用Properties對象進行處理

1.  加載properties文件獲取inputStream
1.1   方式1.使用類加載ClassLoader加載src的資源(固定寫法) 獲得ClassLoader固定寫法:當前類.class.getClassLoader();

        InputStream  is=   jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");

1.2 方式2 加載當前類同包下的資源,如果需要從src開始必須填寫 ‘’/‘’

        InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");

加載src下的資源

        InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties");

加載完成後:使用Properties處理流

Properties props=new Properties();

使用load() 方法加載指定的流

props.load(is); / props.load(is3); / props.load(is2);

Properties props=new Properties();

            props.load(is);
            //使用getProperty(key),獲取需要的值
            className=props.getProperty("jdbc.className");
            url=props.getProperty("jdbc.url");
            user=props.getProperty("jdbc.user");
            password=props.getProperty("jdbc.password");
        

jdbc加載項目

package com.jdec.util_v3;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class jdbcUtil {
    private static  String user;
    public  static String password;
    public  static String className;
    public  static String url;
    Connection con=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;
    static{
        //1.加載properties文件獲取inputStream
        /*1.1 方式1.使用類加載ClassLoader加載src的資源(固定寫法)
         * 獲得ClassLoader固定寫法:當前類.class.getClassLoader();
         */
        InputStream  is=   jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //加載當前類同包下的資源,如果需要從src開始必須填寫/
        InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");
        //加載src下的資源
        InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties");
        
        //使用Properties處理流
        // 使用load() 方法加載指定的流
        Properties props=new Properties();
        try {
            props.load(is);
            //使用getProperty(key),獲取需要的值
            className=props.getProperty("jdbc.className");
            url=props.getProperty("jdbc.url");
            user=props.getProperty("jdbc.user");
            password=props.getProperty("jdbc.password");
                    
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
        public jdbcUtil() {
            try{
                Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }


            


            public Connection getConnection() {
                try {
                    con=(Connection) DriverManager.getConnection(url, user, password);
                } catch (SQLException e) {
                    con=null;
                    e.printStackTrace();
                    
                }
                
                return con;
            }

    
            public ResultSet excuteQuery(String sql, Object[] obj) {
                if (sql!=null ){
                    con=getConnection();
                    if(con!=null){
                        try {
                            pstm=(PreparedStatement) con.prepareStatement(sql);
                                if (obj!=null) {
                                    for(int i=0;i<obj.length;i++){
                                        pstm.setObject(i+1,obj[i]);
                                    }
                                }
                                
                             rs=pstm.executeQuery();
                
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
                return rs;
            }


            public int excuteUpdate(String sql, Object[] obj) {
                // TODO Auto-generated method stub
                int flag=-1;
                if(sql!=null && obj!=null){
                    con=getConnection();
                    
                    if (con!=null) {
                        try {
                            pstm=(PreparedStatement) con.prepareStatement(sql);
                            for(int i=0;i<obj.length;i++){
                                pstm.setObject(i+1, obj[i]);
                            }
                            flag=pstm.executeUpdate();
                            
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return flag;
            }

            public ResultSet queryAll(String sql) {
                
                    con=getConnection();
                    if(con!=null){
                        try {
                            pstm=(PreparedStatement) con.prepareStatement(sql);
                            rs=pstm.executeQuery();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                
                
                return rs;
                
            }


        public void closeAll() {
            // TODO Auto-generated method stub
            if (rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
                        if (pstm!=null) {
                            try {
                                pstm.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            
                        }
                        if (con!=null) {
                            try {
                                con.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }

    
}

測試類

package com.jdbc.util;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.jdbc.dao.JdbcUtils;
import com.jdec.util_v3.jdbcUtil;



    public static void main(String[] args) throws SQLException{
        jdbcUtil jd=new jdbcUtil();
        String sql="select * from book";
        Object[] obj=null;
        ResultSet rs=jd.excuteQuery(sql, obj);
        while(rs.next()){
            System.out.println(rs.getObject("bookId")+"   "+rs.getObject("bookName")+"     "+rs.getObject("bookAuthor"));

        }
    }
}

jdbc.properties

jdbc.user=root
jdbc.password=root
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore

JavaWeb中jdbcproperties配置文件