import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import com.wer.factories.PrintFactory;
/**
* ORACLE数据库操作工具类
*
*/
public class OracleHandler {
private String ipAddr = "127.0.0.1";
private String port = "1521";
private String usrname = "sa";
private String password = "sa";
private String sid = "sss";
private int StartColIndex = 1; // 1 为起始索引号
private Connection conn = null;
private Statement state = null;
public OracleHandler() {
// initial
}
public OracleHandler(String ipAddr, String port, String usrname,
String password, String sid) {
this.ipAddr = ipAddr;
this.port = port;
this.usrname = usrname;
this.password = password;
this.sid = sid;
}
public OracleHandler(String ipAddr, String usrname, String password,
String sid) {
this.ipAddr = ipAddr;
this.usrname = usrname;
this.password = password;
this.sid = sid;
getConn();
}
/**
* DB连接,保存连接实例至全局变量
*
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws SQLException
*/
private void getConn() {
String dbUrl = "jdbc:oracle:thin:@" + ipAddr + ":1521:" + sid;
String theUser = usrname;
String thePw = password;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(dbUrl, theUser, thePw);
state = conn.createStatement();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
}
}
/**
* 关闭数据库连接
*
* @throws SQLException
*/
public void close() {
if (state != null)
try {
state.close();
state = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
}
if (conn != null)
try {
conn.close();
conn = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
}
}
/**
* 是否已连接
*
* @return
*/
public boolean isConnected() {
if (state != null && conn != null)
return true;
else
return false;
}
/**
* 从DB中获取表列名
*/
public String[] getColumnFrmDB(String tblname) {
ResultSet rs = null;
String[] rst;
if (isConnected()) {
try {
String headSql = "select * from " + tblname;
// ArrayList IBSCONFIG = executeQuery(headSql);
rs = state.executeQuery(headSql);
ResultSetMetaData rsmd = rs.getMetaData();
int size = rsmd.getColumnCount();
rst = new String[size];
for (int i = 0; i < size; i++) {
System.out.print(rsmd.getColumnName(i + StartColIndex)
+ " ");
rst[i] = rsmd.getColumnName(i + StartColIndex);
}
return rst;
} catch (SQLException e) {
// TODO Auto-generated catch block
PrintFactory.getInstance().err(e.getMessage());
}
}
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
PrintFactory.getInstance().err(e.getMessage());
}
return null;
}
/** 获取查询SQL语句的返回表头数组 **/
public String[] QueryForHeads(String Sql) {
ResultSet rs = null;
String[] rst;
if (isConnected()) {
try {
rs = state.executeQuery(Sql);
ResultSetMetaData rsmd = rs.getMetaData();
int size = rsmd.getColumnCount();
rst = new String[size + 1];
rst[0] = "序号";
for (int i = 0; i < size; i++) {
System.out.print(rsmd.getColumnName(i + StartColIndex)
+ " ");
rst[i + 1] = rsmd.getCo
Oracle数据库操作接口
最新推荐文章于 2023-03-06 10:57:49 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)