package com.quant.comm;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBhepler {
	InputStream is = DBhepler.class.getClassLoader().getResourceAsStream("db.properties"); 
	Properties properties = new Properties();
	Connection con = null;
	ResultSet res = null;
	PreparedStatement pst=null;
	public void DataBase() {
			try {
				properties.load(is);
				Class.forName(properties.getProperty("driver"));
			    
				con = DriverManager.getConnection(properties.getProperty("url"),
													properties.getProperty("user"),
														properties.getProperty("pwd"));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				  System.err.println("装载 JDBC/ODBC 驱动程序失败。" );  
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.err.println("无法连接数据库" ); 
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}

	public ResultSet Search(String sql) {
		DataBase();
		try {
			PreparedStatement pst =con.prepareStatement(sql);

			res = pst.executeQuery();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public int AddU(String sql, String str) {
		int a = 0;
		DataBase();
		try {
		 pst = con.prepareStatement(sql+str);
			a = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(pst!=null){
				try {
					pst.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return a;
	}

}
