import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 连接数据库基类
*
* @class ConnectionOracle
* @description
* @author 李智慧
* @copyRight copyright(c) 2012 广东南航易网通电子商务有限公司,Rights Reserved
* @time Mar 7, 2012 11:35:11 AM
*/
public class ConnectionOracle {
String sd = "oracle.jdbc.driver.OracleDriver";// oracle数据库驱动名称
String sc = "jdbc:oracle:thin:@192.168.1.120:1521:CDD";// oracle数据库连接地址
String userName = "test";// 连接数据库的用户名
String password = "1234";// 连接数据库的密码
// String sd="com.mysql.jdbc.Driver";//mysql数据库驱动名称
// sc="jdbc:mysql://localhost:3306/payManagerDB?useUnicode=true&characterEncoding=utf8";//mysql数据库连接地址
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
/**
* 构造函数,加载数据库驱动
*
* @throws ClassNotFoundException
*/
public ConnectionOracle() throws ClassNotFoundException {
Class.forName(sd);
}
/**
* 获得数据库connection
*
* @return
* @throws SQLException
*/
public Connection getConnection() throws SQLException {
con = DriverManager.getConnection(sc, userName, password);
return con;
}
/**
* 查询用的返回ResultSet类型数据
*
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
return rs;
}
/**
* 执行更新、新增、删除操作
*
* @param sql
* @throws SQLException
*/
public void executeUpdate(String sql) throws SQLException {
con = DriverManager.getConnection(sc, userName, password);
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
}
/**
* 关闭连接
*
* @throws SQLException
*/
public void close() throws SQLException {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
}