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 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 map = new HashMap();
		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


你可能感兴趣的:(JAVA编程)