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

📄 dbconnectionmanager.java

📁 基于wml的手机余额查询系统
💻 JAVA
字号:
package org.wap.util;

import java.sql.*;

/**
 * Title:       数据库连接管理器
 * Description:
 * Copyright:    
 * Company:      
 * @author:		 
 * @version 1.0
 */

public class DBConnectionManager {
	
	private Connection dbConnection = null;

    public DBConnectionManager(){
     
	String configFileName = "datasource.properties";
	String source = "sqlserver";
		  try {
				   DbConfigLoader.setDbConfigContext(configFileName,source);
				   //load JDBC
				   if (DbConfigLoader.getConnectionType().equals("JDBC")) {
				   log("TransactionContext: setup " + source + " JDBC Driver");
				   Class.forName(DbConfigLoader.getDriver());
				   log("TransactionContext: setup " + source + " JDBC loaded ok");
				   }
				   //load JDNI
				   if (DbConfigLoader.getConnectionType().equals("JNDI")) {
					log("TransactionContext: setup " + source + " JNDI Driver");
//					  servicelocator = ServiceLocator.getInstance();
					log("TransactionContext: setup  " + source + " JNDI loaded ok");
				   }
			   } catch (ClassNotFoundException e) {
				   System.out.println(source + " class not found");
				   throw new TranSysException(e.getMessage());
			   } catch (SQLException e) {
				 e.printStackTrace();  //To change body of catch statement use Options | File Templates.
			   } catch (Exception e) {
				 e.printStackTrace();  //To change body of catch statement use Options | File Templates.
				 System.out.println(source + "JNDI Driver not found");
				 throw new TranSysException(e.getMessage());  //To change body of catch statement use Options | File Templates.
			   }

      
    }
    
	public Connection getDBConnection() {
		 getConnection();
		 log("TransactionContext: in get connection");
		 if (dbConnection == null)
		 	throw new TranSysException("no database connection established yet");
		
		    return dbConnection;
	 }
	 
	public void getConnection() throws TranSysException {
		  try {
			  //get JDBC connection
			  if (DbConfigLoader.getConnectionType().equals("JDBC")) {
				  log("TransactionContext: before get connection");
				  dbConnection = DriverManager.getConnection(DbConfigLoader.getUrl(),DbConfigLoader.getUserName(),DbConfigLoader.getPassword());
				  log("TransactionContext: after get connection");
			  }
			  //get JDNI connection
//				if (DbConfigLoader.getConnectionType().equals("JNDI")) {
//					dbConnection = servicelocator.getDBConn(0);
//				}
//			} catch (ServiceLocatorException e) {
//				log("TransactionContext: SQL Exception");
//				e.printStackTrace();
		  } catch (SQLException se) {
			  log("TransactionContext: SQL Exception");
			  se.printStackTrace();
			  throw new TranSysException("SQLExcpetion while getting" + " DB Connection : \n" + se);
		  }
		  
	  }

	  private void log(String s) {
		  System.out.println(s);
	  }
	
	
	
	
	
	
	
	
	
	
//    private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //驱动程序名
//    private String url = "jdbc:microsoft:sqlserver://192.168.6.203:1433;DatabaseName=immunity";//url
//    private String user = "sa";                  //登录数据库用户名
//    private String password = "sa";              //登录数据库密码
//
//    public void setDriverName(String newDriverName) {
//        driverName = newDriverName;
//    }
//    public String getDriverName() {
//        return driverName;
//    }
//    public void setUrl(String newUrl) {
//        url = newUrl;
//    }
//    public String getUrl() {
//        return url;
//    }
//    public void setUser(String newUser) {
//        user = newUser;
//    }
//    public String getUser() {
//        return user;
//    }
//    public void setPassword(String newPassword) {
//        password = newPassword;
//    }
//    public String getPassword() {
//        return password;
//    }
//
//    public Connection getConnection() {
//        try {
//            Class.forName(driverName);
//            return DriverManager.getConnection(url, user, password);
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public static void main(String[] args) {
        DBConnectionManager DBConnectionManager1 = new DBConnectionManager();
    }
}

⌨️ 快捷键说明

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