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]   
  为什么??   
  怎么解决???   
  在线等待   

解决方案 »

  1.   

    驱动加到classpath里了吗,已经提示你找不到类了
      

  2.   

    db_index.txt的内容是driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/jspcndb?user=root&password=aaa&useUnicode=true&characterEncoding=GB2312
      

  3.   

    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);
        }  
      

  4.   

    接着上面的
    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);
        }  
      

  5.   

    那为高手帮我解决下了!是不是要啊db.txt改成db.properties
      

  6.   

    你不是报了 ClassNotFoundException 吗?写一个简单的测试类测试一下希望lz能养成多写测试代码的习惯,给你的DBConnectionManager一个main方法测试下ClassNotFoundException  的话, 看看是不是 Class.forName("...")这里抛出来的
    看一看你的类路径中有没有加入 mysql的驱动
      

  7.   

    你可以用一个简单的工具去测一下。首先确定驱动的jar包放到了lib中