wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.vci.server.base.dbutil;
 
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.Properties;
 
import javax.sql.DataSource;
 
import org.apache.commons.dbcp.BasicDataSource;
 
import com.vci.common.log.ServerWithLog4j;
 
 
public final class DBPoolInit {
 
    private String dbURL; // Database URL
    private Properties dbProperties; // Properties read from properties file
    private String driverClass; // Driver class name
    
    private String userName = "";
    private String password = "";
    private String fileName = "properties/sys.properties";
    
    public DBPoolInit()
    {
        
    }
    public void InitProperties(String strProsFileName)
    {
        dbProperties = new Properties();
        if ( !strProsFileName.equalsIgnoreCase("") )
            fileName = strProsFileName;
        try
        {
            FileInputStream propertiesFileStream = new FileInputStream(fileName);
            dbProperties.load(propertiesFileStream);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public DataSource getDBPool() throws ClassNotFoundException, SQLException, Exception
    {
        BasicDataSource ds = new BasicDataSource();
        driverClass = (String)dbProperties.get("DB.DRIVER");
        dbURL   = (String)dbProperties.get("DB.URL");
        userName  = ((String)dbProperties.get("DB.USERNAME")).trim();
        
        password = ((String)dbProperties.get("DB.PASSWORD")).trim();;
        String otherProperties = (String)dbProperties.get("DB.SUB_PROPERTIES");
        
        if(driverClass == null || dbURL == null){
          throw new Exception("Driver Class or Driver URL should not be null");
        }
        ServerWithLog4j.logger.info("Registering driver : "+driverClass);
        
        if( (userName.length() == 0) && (password.length() == 0) && (otherProperties.length() == 0) )
        {
          ServerWithLog4j.logger.info("UserName is null or password is null!");
        }
        else
        {
          ds.setDriverClassName (driverClass);
          ds.setUsername (userName);
          ds.setPassword (password);
          ds.setUrl (dbURL);
          int maxactive = Integer.parseInt((String)dbProperties.get("PT.MAXACTIVE"));
          int maxidle = Integer.parseInt((String)dbProperties.get("PT.MAXIDLE"));
          int minidle = Integer.parseInt((String)dbProperties.get("PT.MINIDLE"));
          int maxwait = Integer.parseInt((String)dbProperties.get("PT.MAXWAIT"));
 
          ds.setMaxActive(maxactive);
          ds.setMaxIdle(maxidle);
          ds.setMinIdle(minidle);
          ds.setMaxWait(maxwait);
          ds.setDefaultAutoCommit(false);
        }
        ServerWithLog4j.logger.info(dbURL);
        ServerWithLog4j.logger.info("Database connect successful!");
        return ds;
    }
}