public class DBConnection {
private static String driverName = "";
private static String url = "";
private static String userName = "";
private static String userPass = "";
private DBConnection() {
}
public static Connection getConnection(String _userName, String _userPass)
throws ClassNotFoundException, SQLException {
// mysql
driverName = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://127.0.0.1:3306/db_user?characterEncoding=gbk";
// sql server 2005
// driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// url = "jdbc:sqlserver://127.0.0.1:1433;database=mytest";
// oracle
// driverName = "oracle.jdbc.driver.OracleDriver";
// url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
userName = _userName;
userPass = _userPass;
Connection conn = null;
Class.forName(driverName);
conn = DriverManager.getConnection(url, userName, userPass);
return conn;
}
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
return getConnection("root","19880629");
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static List executeQuery(String sql) {
List list = new ArrayList();
// jdbc...
return list;
}
public static int executeUpdate(String sql) {
int result = -1;
// jdbc...
return result;
}
public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection("root", "19880629");
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
closeConnection(conn);
}
}