JDBC连接oracle10g数据库-韩顺平SQLHelper类
注意:使用时,大家一定要注意加入oracle的驱动包
package oracle;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class SQLHelper {
private static Connection conn;
private static PreparedStatement ps;
private static ResultSet rs;
private static String driver;
private static String url;
private static String username;
private static String password;
public static void main(String[] args) {
// 测试
String sql = "select * from emp";
ResultSet rst = SQLHelper.executeQuery(sql, null);
try {
while(rst.next()){
System.out.println(rst.getString("ename"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
SQLHelper.close(SQLHelper.getConn(),SQLHelper.getPs(),rst);
}
}
public static Connection getConn() {
return conn;
}
public static PreparedStatement getPs() {
return ps;
}
static{
try {
Map<String,String> map = get();
driver = map.get("driver");
url = map.get("url");
username = map.get("username");
password = map.get("password");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//初始化变量
public static Map get(){
Properties pp = new Properties();
FileInputStream fis = null;
Map<String,String> map = new HashMap<String, String>();
try {
fis = new FileInputStream("dbinfor.properties");
pp.load(fis);
driver = pp.getProperty("driver");
url = pp.getProperty("url");
username = pp.getProperty("username");
password = pp.getProperty("password");
map.put("driver", driver);
map.put("url", url);
map.put("username", username);
map.put("password", password);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
//增删改方法
public static void executeUpdate(String sql,String []parameters){
try {
conn = DriverManager.getConnection(url, username, password);
ps = conn.prepareStatement(sql);
if(parameters!=null){
for(int i=0;i<parameters.length;i++){
ps.setString(i+1, parameters[i]);
}
}
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
SQLHelper.close(conn, ps, rs);
}
}
//查询方法
public static ResultSet executeQuery(String sql,String []parameters){
try {
conn = DriverManager.getConnection(url, username, password);
ps = conn.prepareStatement(sql);
if(parameters !=null){
for(int i=0;i<parameters.length;i++){
ps.setString(i+1, parameters[i]);
}
}
//执行查询
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
//关闭资源
//SQLHelper.close(conn, ps, rs);
}
return rs;
}
//关闭资源
public static void close(Connection conn,Statement stmt,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn=null;
}
}
}