package com.gogofun.javabean;java
import java.sql.*;mysql
public class DB {sql
private static boolean isInit = false;服務器
private static String mysqlDriver = "com.mysql.jdbc.Driver";oracle
private static String oracleDriver = "oracle.jdbc.driver.OracleDriver";ide
private static String mysqlConn = "jdbc:mysql://";this
private static String oracleConn = "jdbc:oracle:this:@";spa
private static PreparedStatement pstmt = null;字符串
private static void init() {get
try {
Class.forName(mysqlDriver);
Class.forName(oracleDriver);
isInit = true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
isInit = false;
}
}
//外部獲取Mysql驅動字符串
public static String getMysqlDriver() {
return mysqlDriver;
}
//外部獲取Oracle驅動字符串
public static String getOracleDriver() {
return oracleDriver;
}
//該方法鏈接本地服務器,默認端口
public static Connection getMysqlConnection(String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = mysqlConn + "localhost/" + dbName + "?user=" + dbUser
+ "&password=" + dbPassword;
try {
return DriverManager.getConnection(connStr);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//該方法鏈接指定IP服務器,默認端口
public static Connection getMysqlConnection(String dbIp, String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = mysqlConn + dbIp + "/" + dbName + "?user=" + dbUser
+ "&password=" + dbPassword;
try {
return DriverManager.getConnection(connStr);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//該方法鏈接本地服務器,默認端口
public static Connection getOracleConnection(String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = oracleConn + "localhost:1521:" + dbName;
try {
return DriverManager.getConnection(connStr,dbUser,dbPassword);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//該方法鏈接本地服務器,指定端口
/*
public static Connection getOracleConnection(String dbPort,String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = oracleConn +"localhost:" + dbPort +":" + dbName;
try {
return DriverManager.getConnection(connStr,dbUser,dbPassword);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}*/
//該方法鏈接指定IP服務器,默認端口
public static Connection getOracleConnection(String dbIp,String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = oracleConn + dbIp + ":1521:" + dbName;
try {
return DriverManager.getConnection(connStr,dbUser,dbPassword);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//該方法鏈接指定IP服務器,指定端口
public static Connection getOracleConnection(String dbIp,String dbPort,String dbName,
String dbUser, String dbPassword) {
if (!isInit) {
init();
}
String connStr = oracleConn + dbIp + ":" + dbPort +":" + dbName;
try {
return DriverManager.getConnection(connStr,dbUser,dbPassword);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//執行一條SQL Query語句
public static ResultSet executeQuery(Connection conn,String sqlQ) {
if((conn == null)|| (sqlQ == null)) {
return null;
}
try {
pstmt = conn.prepareStatement(sqlQ);
return pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
close(pstmt);
return null;
}
}
//執行一條SQL Update語句
public static boolean executeUpdate(Connection conn,String sqlU) {
if((conn == null)|| (sqlU == null)) {
return false;
}
try {
pstmt = conn.prepareStatement(sqlU);
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
close(pstmt);
return false;
}
}
//關閉鏈接
public static boolean close (Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
//關閉語句
public static boolean close (Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
//關閉結果集
public static boolean close (ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}