package net.xzwz.db;import java.io.*;
import java.sql.*;
import java.util.Properties;public class DbConnect
{ Connection conn;
Statement stmt;
ResultSet rset;
DBConnectionManager connMgr; public DbConnect()
{
conn = null;
stmt = null;
rset = null;
connMgr = null;
} public void close()
throws SQLException
{
if(conn != null)
conn.close();
if(rset != null)
rset.close();
if(stmt != null)
stmt.close();
} public ResultSet executeQuery(String s)
throws SQLException
{
stmt = conn.createStatement();
rset = stmt.executeQuery(s);
return rset;
} public void executeUpdate(String s)
throws SQLException
{
stmt = conn.createStatement();
stmt.executeUpdate(s);
if(stmt != null)
stmt.close();
} protected void finalize()
throws Throwable
{
close();
} public void freeConnection(String s)
{
connMgr.freeConnection(s, conn);
} public Connection getConn()
{
return conn;
} public static void main(String args[])
{
DbConnection dbconnection = new DbConnection();
System.out.println(dbconnection.openConnection());
} public boolean openConnection()
{
Properties properties = new Properties();
try
{
InputStream inputstream = getClass().getResourceAsStream("db_index.txt");
properties.load(inputstream);
if(inputstream != null)
inputstream.close();
}
catch(IOException _ex)
{
System.out.println("[DbConnection] Open db_index.txt File, Error!");
}
String s = properties.getProperty("driver");
String s1 = properties.getProperty("url");
try
{
Class.forName(s).newInstance();
}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println("JDBC login, Error!@" + classnotfoundexception.getMessage());
return false;
}
catch(Exception exception)
{
System.err.println("Unable to load driver!");
exception.printStackTrace();
}
try
{
conn = DriverManager.getConnection(s1);
}
catch(SQLException sqlexception)
{
System.out.println("Generate Connection, Error!" + sqlexception.getMessage());
return false;
}
return true;
} public boolean openConnection(String s)
{
connMgr = DBConnectionManager.getInstance();
conn = connMgr.getConnection(s);
return conn != null;
} public boolean openConnection(String s, long l)
{
connMgr = DBConnectionManager.getInstance();
conn = connMgr.getConnection(s, l);
return conn != null;
} public void release()
{
connMgr.release();
}
}
就会出错,提示JDBC login,[email protected]
为什么??
怎么解决???
在线等待
url=jdbc:mysql://localhost:3306/jspcndb?user=root&password=aaa&useUnicode=true&characterEncoding=GB2312
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBConnectionManager
{
class DBConnectionPool
{ private int checkedOut;
private Vector freeConnections;
private int maxConn;
private String name;
private String password;
private String URL;
private String user; public synchronized void freeConnection(Connection connection)
{
freeConnections.addElement(connection);
checkedOut--;
notifyAll();
} public synchronized Connection getConnection()
{
Connection connection = null;
if(freeConnections.size() > 0)
{
connection = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if(connection.isClosed())
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
}
catch(SQLException _ex)
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
} else
if(maxConn == 0 || checkedOut < maxConn)
connection = newConnection();
if(connection != null)
checkedOut++;
return connection;
} public synchronized Connection getConnection(long l)
{
long l1 = (new Date()).getTime();
Connection connection;
while((connection = getConnection()) == null)
{
try
{
wait(l);
}
catch(InterruptedException _ex) { }
if((new Date()).getTime() - l1 >= l)
return null;
}
return connection;
} private Connection newConnection()
{
Connection connection = null;
try
{
if(user == null)
connection = DriverManager.getConnection(URL);
else
connection = DriverManager.getConnection(URL, user, password);
log("连接池" + name + "创建一个新的连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法创建下列URL的连接: " + URL);
return null;
}
return connection;
} public synchronized void release()
{
for(Enumeration enumeration = freeConnections.elements(); enumeration.hasMoreElements();)
{
Connection connection = (Connection)enumeration.nextElement();
try
{
connection.close();
log("关闭连接池" + name + "中的一个连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法关闭连接池" + name + "中的连接");
}
} freeConnections.removeAllElements();
} public DBConnectionPool(String s, String s1, String s2, String s3, int i)
{
freeConnections = new Vector();
name = s;
URL = s1;
user = s2;
password = s3;
maxConn = i;
}
}
private static DBConnectionManager instance;
private static int clients;
private Vector drivers;
private PrintWriter log;
private Hashtable pools; private DBConnectionManager()
{
drivers = new Vector();
pools = new Hashtable();
init();
} private void createPools(Properties properties)
{
for(Enumeration enumeration = properties.propertyNames(); enumeration.hasMoreElements();)
{
String s = (String)enumeration.nextElement();
if(s.endsWith(".url"))
{
String s1 = s.substring(0, s.lastIndexOf("."));
String s2 = properties.getProperty(s1 + ".url");
if(s2 == null)
{
log("没有为连接池" + s1 + "指定URL");
} else
{
String s3 = properties.getProperty(s1 + ".user");
String s4 = properties.getProperty(s1 + ".password");
String s5 = properties.getProperty(s1 + ".maxconn", "0");
int i;
try
{
i = Integer.valueOf(s5).intValue();
}
catch(NumberFormatException _ex)
{
log("错误的最大连接数限制: " + s5 + " .连接池: " + s1);
i = 0;
}
DBConnectionPool dbconnectionpool = new DBConnectionPool(s1, s2, s3, s4, i);
pools.put(s1, dbconnectionpool);
log("成功创建连接池" + s1);
}
}
} } public void freeConnection(String s, Connection connection)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
dbconnectionpool.freeConnection(connection);
} public Connection getConnection(String s)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection();
else
return null;
} public Connection getConnection(String s, long l)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection(l);
else
return null;
} public static synchronized DBConnectionManager getInstance()
{
if(instance == null)
instance = new DBConnectionManager();
clients++;
return instance;
} private void init()
{
java.io.InputStream inputstream = getClass().getResourceAsStream("/db.properties");
Properties properties = new Properties();
try
{
properties.load(inputstream);
}
catch(Exception _ex)
{
System.err.println("不能读取属性文件. 请确保db.properties在CLASSPATH指定的路径中");
return;
}
String s = properties.getProperty("logfile", "DBConnectionManager.log");
try
{
log = new PrintWriter(new FileWriter(s, true), true);
}
catch(IOException _ex)
{
System.err.println("无法打开日志文件: " + s);
log = new PrintWriter(System.err);
}
loadDrivers(properties);
createPools(properties);
}
DBConnectionManager.java的源程序如下:package net.xzwz.db;import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBConnectionManager
{
class DBConnectionPool
{ private int checkedOut;
private Vector freeConnections;
private int maxConn;
private String name;
private String password;
private String URL;
private String user; public synchronized void freeConnection(Connection connection)
{
freeConnections.addElement(connection);
checkedOut--;
notifyAll();
} public synchronized Connection getConnection()
{
Connection connection = null;
if(freeConnections.size() > 0)
{
connection = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if(connection.isClosed())
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
}
catch(SQLException _ex)
{
log("从连接池" + name + "删除一个无效连接");
connection = getConnection();
}
} else
if(maxConn == 0 || checkedOut < maxConn)
connection = newConnection();
if(connection != null)
checkedOut++;
return connection;
} public synchronized Connection getConnection(long l)
{
long l1 = (new Date()).getTime();
Connection connection;
while((connection = getConnection()) == null)
{
try
{
wait(l);
}
catch(InterruptedException _ex) { }
if((new Date()).getTime() - l1 >= l)
return null;
}
return connection;
} private Connection newConnection()
{
Connection connection = null;
try
{
if(user == null)
connection = DriverManager.getConnection(URL);
else
connection = DriverManager.getConnection(URL, user, password);
log("连接池" + name + "创建一个新的连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法创建下列URL的连接: " + URL);
return null;
}
return connection;
} public synchronized void release()
{
for(Enumeration enumeration = freeConnections.elements(); enumeration.hasMoreElements();)
{
Connection connection = (Connection)enumeration.nextElement();
try
{
connection.close();
log("关闭连接池" + name + "中的一个连接");
}
catch(SQLException sqlexception)
{
log(sqlexception, "无法关闭连接池" + name + "中的连接");
}
} freeConnections.removeAllElements();
} public DBConnectionPool(String s, String s1, String s2, String s3, int i)
{
freeConnections = new Vector();
name = s;
URL = s1;
user = s2;
password = s3;
maxConn = i;
}
}
private static DBConnectionManager instance;
private static int clients;
private Vector drivers;
private PrintWriter log;
private Hashtable pools; private DBConnectionManager()
{
drivers = new Vector();
pools = new Hashtable();
init();
} private void createPools(Properties properties)
{
for(Enumeration enumeration = properties.propertyNames(); enumeration.hasMoreElements();)
{
String s = (String)enumeration.nextElement();
if(s.endsWith(".url"))
{
String s1 = s.substring(0, s.lastIndexOf("."));
String s2 = properties.getProperty(s1 + ".url");
if(s2 == null)
{
log("没有为连接池" + s1 + "指定URL");
} else
{
String s3 = properties.getProperty(s1 + ".user");
String s4 = properties.getProperty(s1 + ".password");
String s5 = properties.getProperty(s1 + ".maxconn", "0");
int i;
try
{
i = Integer.valueOf(s5).intValue();
}
catch(NumberFormatException _ex)
{
log("错误的最大连接数限制: " + s5 + " .连接池: " + s1);
i = 0;
}
DBConnectionPool dbconnectionpool = new DBConnectionPool(s1, s2, s3, s4, i);
pools.put(s1, dbconnectionpool);
log("成功创建连接池" + s1);
}
}
} } public void freeConnection(String s, Connection connection)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
dbconnectionpool.freeConnection(connection);
} public Connection getConnection(String s)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection();
else
return null;
} public Connection getConnection(String s, long l)
{
DBConnectionPool dbconnectionpool = (DBConnectionPool)pools.get(s);
if(dbconnectionpool != null)
return dbconnectionpool.getConnection(l);
else
return null;
} public static synchronized DBConnectionManager getInstance()
{
if(instance == null)
instance = new DBConnectionManager();
clients++;
return instance;
} private void init()
{
java.io.InputStream inputstream = getClass().getResourceAsStream("/db.properties");
Properties properties = new Properties();
try
{
properties.load(inputstream);
}
catch(Exception _ex)
{
System.err.println("不能读取属性文件. 请确保db.properties在CLASSPATH指定的路径中");
return;
}
String s = properties.getProperty("logfile", "DBConnectionManager.log");
try
{
log = new PrintWriter(new FileWriter(s, true), true);
}
catch(IOException _ex)
{
System.err.println("无法打开日志文件: " + s);
log = new PrintWriter(System.err);
}
loadDrivers(properties);
createPools(properties);
}
看一看你的类路径中有没有加入 mysql的驱动