日期:2014-05-17 浏览次数:21009 次
package zmy.db;
import java.sql.*;
public class DBUtil {
    public static Connection getConnection() {
        Connection conn = null;
        try {
            String driver = "oracle.jdbc.driver.OracleDriver";
            String dbName = "dbname";
            String dbURL = "jdbc:oracle:thin:@localhost:1521:";
            String username = "scott";
            String password = "triger";
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(dbURL + dbName, username,
                    password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static boolean closeConnection(Connection conn) {
        try {
            conn.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
------解决方案--------------------
package zmy.db;
import java.sql.*;
public class DBUtil {
    public static Connection getConnection() {
        Connection conn = null;
        try {
            String driver = "oracle.jdbc.driver.OracleDriver";
            String dbName = "dbname";
            String dbURL = "jdbc:oracle:thin:@localhost:1521:";
            String username = "scott";
            String password = "triger";
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(dbURL + dbName, username,
                    password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static boolean closeConnection(Connection conn) {
        try {
            conn.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}