⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnection.java

📁 数据库封装包
💻 JAVA
字号:
/*
 * Created on 2007-7-4
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 *  The class function is creating a Database connection;
 *  该类封装了第三方数据库连接池的组件,创建一个数据库连接对象,并提供基本数据库操作方法;
 */
package database;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.devdaily.opensource.database.DDConnectionBroker;
public class DBConnection {
	
	Connection connection = null;
	private static DDConnectionBroker broker = null;
	//参数fileName为数据库配置文件,文件格式为Properties;
	public DBConnection (){
		String fileName="";
		try{
		String filepath=getFullPathRelateClass("",  DBConnection.class);	
		fileName=filepath+"\\dboperation.properties";
		System.out.println(fileName);
	    File file=new File(fileName);
		FileInputStream input=new FileInputStream(file);
		Properties p=new Properties();
		p.load(input);
		String userName=p.getProperty("userName");
		String password=p.getProperty("password");
		String connectURL=p.getProperty("connectURL");
		String driverClassName=p.getProperty("driverClassName");
		int minConnections=Integer.parseInt(p.getProperty("minConnections"));
		int maxConnections=Integer.parseInt(p.getProperty("maxConnections"));
		long timeout=Long.parseLong(p.getProperty("timeout"));
		long leaseTime=Long.parseLong(p.getProperty("leaseTime"));
		String logFile=p.getProperty("logFile");
		//String conURL = "jdbc:microsoft:sqlserver://"+dbIP+":1433;databasename="+dbName;
		 broker=new DDConnectionBroker(driverClassName,connectURL,userName,password,
				                         minConnections,maxConnections,timeout,leaseTime,logFile);
		 	
		}catch(SQLException exc){
			System.out.println("数据库连接出错!请检查配置文件!");
			exc.printStackTrace();
		}catch(Exception  e){
			e.printStackTrace();
		}
}
	


/* 根据查询语句,返回记录集*/
  public ResultSet executeQuery(String sql){
  	 ResultSet rs=null;
  	try{ 
  	 connection=broker.getConnection();	
  	 if(connection==null)System.out.print("no connection");
  	 Statement s = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
	  rs= s.executeQuery(sql);	
    }catch(SQLException e){
    	e.printStackTrace();
    }finally{
    	try{
    		broker.freeConnection(connection);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    return rs;
  }	
  /* 根据执行sql语句,完成数据库记录更新及删除操作*/
  public void executeUpdateOrDelete(String sql){
  	try{ 
  		connection=broker.getConnection();
  	  	Statement s = connection.createStatement();
  		s.executeUpdate(sql);	
  	    }
  	catch(SQLException e){
  	    	e.printStackTrace();
  	    }
    finally{
    	try{
    		broker.freeConnection(connection);
    	    }
    	catch(Exception e){
    		e.printStackTrace();
    	}
  	       }
  }

  
  /*得到参数类的路径*/
  private URL getClassLocationURL(final Class cls) {
	  if (cls == null)
	   throw new IllegalArgumentException("null input: cls");
	  URL result = null;
	  final String clsAsResource = cls.getName().replace('.', '/').concat(
	    ".class");
	  final ProtectionDomain pd = cls.getProtectionDomain();
	  if (pd != null) {
	     final CodeSource cs = pd.getCodeSource();
	     if (cs != null)
	      result = cs.getLocation();
        if (result != null) {
	    if ("file".equals(result.getProtocol())) {
	     try {
	      if (result.toExternalForm().endsWith(".jar")
	        || result.toExternalForm().endsWith(".zip"))
	       result = new URL("jar:".concat(
	         result.toExternalForm()).concat("!/")
	         .concat(clsAsResource));
	      else if (new File(result.getFile()).isDirectory())
	       result = new URL(result, clsAsResource);
	     } catch (MalformedURLException ignore) {
	     }
	    }
	   }
	  }

	  if (result == null) {
	     final ClassLoader clsLoader = cls.getClassLoader();
	      result = clsLoader != null ? clsLoader.getResource(clsAsResource)
	     : ClassLoader.getSystemResource(clsAsResource);
	  }
	  return result;
	 }
  
  /*得到当前类的相对路径*/
 public String getFullPathRelateClass(String relatedPath, Class cls)
  throws IOException {
 String path = null;
 if (relatedPath == null) {
  throw new NullPointerException();
 }
 String clsPath = getPathFromClass(cls);
 File clsFile = new File(clsPath);
 String tempPath = clsFile.getParent() + File.separator + relatedPath;
 File file = new File(tempPath);
 path = file.getCanonicalPath();
 return path;
}
  /*得到类的绝对*/
 public  String getPathFromClass(Class cls) throws IOException {
	  String path = null;
	  if (cls == null) {
	   throw new NullPointerException();
	  }
	  URL url = getClassLocationURL(cls);
	  if (url != null) {
	   path = url.getPath();
	   if ("jar".equalsIgnoreCase(url.getProtocol())) {
	    try {
	     path = new URL(path).getPath();
	    } catch (MalformedURLException e) {
	    }
	    int location = path.indexOf("!/");
	    if (location != -1) {
	     path = path.substring(0, location);
	    }
	   }
	   File file = new File(path);
	   path = file.getCanonicalPath();
	  }
	  return path;
	 } 
 
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -