JDBC连接oracle10g数据库-韩顺平SQLHelper类

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;
		}
	}
	

	
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值