单例实例化的问题,求指教,求帮助!
下面是我写的代码,标红色的localhost是我要改的地方,我想在第一次实例化JDBCUtil的时候就获取到一个ip地址然后改掉那个localhost,有什么办法吗?求指教!!!
package exam.client.util;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class JDBCUtil
{
private static BasicDataSource dataSource = null;
private static JDBCUtil pool=null;
private JDBCUtil()
{
try {
Properties p = new Properties();
p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
p.setProperty("url", "jdbc:mysql://localhost:3306/exam?characterEncoding=gb2312");
p.setProperty("password", "root");
p.setProperty("username", "root");
//p.setProperty("driverClassName", "sun.jdbc.odbc.JdbcOdbcDriver");access
//p.setProperty("url", "jdbc:odbc:Myexam");access
p.setProperty("maxActive", "30");
p.setProperty("maxIdle", "10");
p.setProperty("maxWait", "1000");
p.setProperty("removeAbandoned", "true");
p.setProperty("removeAbandonedTimeout", "120");
p.setProperty("testOnBorrow", "true");
p.setProperty("logAbandoned", "true");
p.setProperty("initialSize", "20");
dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static JDBCUtil getPool()
{
if (pool == null) {
pool=new JDBCUtil();
}
return pool;
}
public synchronized Connection getConnection() throws SQLException {
Connection conn = null;
if (dataSource != null) {
conn = dataSource.getConnection();
}
return conn;
}
}