您的位置:首页 > 数据库 > Oracle

万能数据库连接类-Oracle、DB2 、Access 、Sql Server

2017-02-17 15:22 555 查看
package cc.apps.report;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.order.cc.sys.dao.FoJdbcDaoSupport;

public class connectData extends FoJdbcDaoSupport{
/* 连接Oracle */
String strIVRUrl = "jdbc:oracle:thin:@127.0.0.1:1521:ccdb";
String strIVRUser = "";
String strIVRPASS = "";
/* 连接DB2 */
String strCTIUrl = "jdbc:db2://**:6789/CTIDB";
String strCTIUser = "";
String strCTIPASS = "";
/* 连接Access */
String strAccUrl = "jdbc:odbc:220acc97";
String strAccUser = "";
String strAccPASS = "";

/* 连接Sql Server */
String strSqlUrl = "jdbc:sqlserver://**:1433;DatabaseName=**";
String strSqlUser = "**";
String strSqlPASS = "**";

Connection connI = null;
Connection connC = null;
Connection connA = null;

public Connection connectIVR() throws SQLException {

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connI = DriverManager.getConnection(strIVRUrl, strIVRUser, strIVRPASS);
}

catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return connI;
}

public Connection connectCTI() throws SQLException {
try {
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
connC = DriverManager.getConnection(strCTIUrl, strCTIUser, strCTIPASS);
}

catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return connC;
}

public Connection connectACC() throws SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connA = DriverManager.getConnection(strAccUrl, strAccUser, strAccPASS);
}

catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return connA;
}
public Connection connectSQL() throws SQLException {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connA = DriverManager.getConnection(strSqlUrl, strSqlUser, strSqlPASS);
}

catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return connA;
}

}

-------------------------调用方法-------------------------------------

public class ReportDAO extends connectData implements IReportDAO {
public ResultEntity queryComCheackCountRes(String year, String month, String monthEnd, String groupName) {
ResultEntity res = new ResultEntity();
List dateList = new ArrayList();
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
conn = connectSQL();
conn = this.getConnection();
stmt = conn.createStatement();
String sql = getSql(year, month, monthEnd, groupName);
rs = stmt.executeQuery(sql);
Map avgMap = new HashMap<String, Double>();
while (rs.next()) {
getRsDateList(dateList, rs, avgMap);
}
res.setList(dateList);
res.setAvgMap(avgMap);
} catch (SQLException sqle) {
sqle.printStackTrace();

} catch (Exception ex) {
ex.printStackTrace();

} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqe) {
sqe.printStackTrace();
}
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐