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

📄 connectionsingle.java

📁 一个完整的
💻 JAVA
字号:
package com.gforce.currency.database;

/**
 * <p>Title: 吉力科技办公自动化系统</p>
 * <p>Description: 吉力科技办公自动化系统</p>
 * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司  Copyright (c) 2003 GForce Sceince & Technology</p>
 * <p>Company: 西安吉力科技发展有限公司 (GForce Sceince & Technology)</p>
 * @author 马登军
 * @version 1.0
 */
import java.sql.*;
import com.gforce.currency.*;

public class ConnectionSingle {

  private static String strDatabaseParaFileName = "/config.properties"; //设置系统参数属性文件路径

  /**
   * 创建Connection对象
   * @param strDriverName    数据库连接的驱动程序名称
   * @param strConnectionURL   数据库连接的JDBC URL
   * @param strConnectionUser  数据库连接的用户名
   * @param strConnectionPassword   数据库连接的密码
   * @return   数据库连接
   */
  public static Connection getConnectionSingle(String strDriverName,
                                               String strConnectionURL,
                                               String strConnectionUser,
                                               String strConnectionPassword) {
    RegisterDriver(strDriverName);
    Connection conn = null;
    try {
      if (strConnectionUser.trim().length() < 1) {
        conn = DriverManager.getConnection(strConnectionURL);
      }
      else {
        conn = DriverManager.getConnection(strConnectionURL, strConnectionUser,
                                           strConnectionPassword);
      }
      SystemOut.OutPrintLine("成功创建了URL为" + strConnectionURL + "的连接");
    }
    catch (Exception err) {
      SystemOut.ErrPrintLine("错误:不能创建URL为" + strConnectionURL + "的连接,错误类型是 " + err.getMessage());
    }
    return conn;
  }

  /**
   * 注册驱动程序
   * @param strDriverName   驱动程序名称
   */
  private static void RegisterDriver(String strDriverName) {
    try {
      Driver driver = (Driver) Class.forName(strDriverName).newInstance();
      DriverManager.registerDriver(driver);
    }
    catch (Exception err) {
      SystemOut.ErrPrintLine("错误:无法注册“" + strDriverName + "”JDBC驱动程序!");
    }
  }

  /**
   * 创建单个指定连接名称的Connection对象
   * @param strConnectionName  连接名称
   * @return  创建好的Connection对象
   */
  public static Connection getConnectionSingle(String strConnectionName) {
    Connection conn = null;
    String strConnectionDriver = new GetParament().GetStringParamentFromFile(strDatabaseParaFileName,strConnectionName +".driver");
    String strConnectionURL = new GetParament().GetStringParamentFromFile(strDatabaseParaFileName,strConnectionName +".url");
    String strConnectionUser = new GetParament().GetStringParamentFromFile(strDatabaseParaFileName,strConnectionName +".user");
    String strConnectionPassword = new GetParament().GetStringParamentFromFile(strDatabaseParaFileName,strConnectionName +".password");
    if(strConnectionDriver.trim().length()>1&&strConnectionURL.trim().length()>1)
    {
      conn = getConnectionSingle(strConnectionDriver,strConnectionURL,strConnectionUser,strConnectionPassword);
      SystemOut.OutPrintLine("成功为“" + strConnectionName + "”创建了URL为" + strConnectionURL + "的连接");
    }
    if(conn==null)
    {
      SystemOut.ErrPrintLine("错误:不能为“" + strConnectionName + "”创建URL为" + strConnectionURL + "的连接");
    }
    return conn;
  }
}

⌨️ 快捷键说明

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