ConnectionSingle.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:3k
源码类别:
OA系统
开发平台:
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;
- }
- }