Oracle数据库连接

package p5.com.byd.mes.dev.tool;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Savepoint;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OracleConnActivity {
   private static final Log log=LogFactory.getLog(OracleConnActivity.class);
   public Connection conn=null;
   public String SQLString="";
   public PreparedStatement psm=null;
   public ResultSet rs=null;
   public Savepoint sp=null;
   public String url="";
   public String user="";
   public String password="";
   public String ActivityName="";
   public String paraStr="";
   
   public OracleConnActivity(){
	   url="";
	   user="";
	   password="";
   }
  public OracleConnActivity(String parURL,String parUSER,String parPASSWORD){
	  url=parURL;
	  user=parUSER;
	  password=parPASSWORD;
  } 
  public boolean getConnect(){
	  try{
		  DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		  conn=DriverManager.getConnection(url,user,password);
		  conn.setAutoCommit(true);
	  }catch(SQLException ex){
		  log.debug(url+"建立数据库连接时发生异常");
		  log.debug("Error code="+ex.getErrorCode());
		  log.debug("Error message="+ex.getMessage());
		  log.debug("sql state=" +ex.getSQLState());
		  return false;
	  }
	  return true;
  }
public void DisConnect(){
	try{
        //关闭ResultSet对象
        if (rs != null) {
            rs.close();
        }
        //关闭PreparedStatement对象
        if (psm != null) {
            psm.close();
        }
        //关闭Connection对象
        if (conn != null) {
            conn.close();
        }
	}catch(SQLException ex){
		log.debug(url+"关闭数据库连接发生异常");
		log.debug("Error code = " + ex.getErrorCode());
		log.debug("Error message = " + ex.getMessage());
		log.debug("SQL state = " + ex.getSQLState());
	}
}
//	public static void main(String[] args){
//		OracleConnActivity con=new OracleConnActivity();
//		con.url="jdbc:oracle:thin:@10.6.68.242:1521:MESSS";
//		con.user="PS_MES_PSN";
//		con.password="psndb";
//		if(con.getConnect()){
//			try{
//				con.SQLString="select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')notime from dual";
//				con.psm=con.conn.prepareStatement(con.SQLString);
//				con.rs=con.psm.executeQuery();
//				while(con.rs.next()){
//					log.debug(con.rs.getString(1));
//				}
//			}catch(SQLException ex){
//				log.debug(ex.getMessage());
//			}
//			finally{
//				con.DisConnect();
//			}
//		}
//		con=null;
//	}
}

 

posted @ 2011-07-22 08:34  上善¤若水  阅读(125)  评论(0编辑  收藏  举报