dbconnect.java

来自「企业进销存源码」· Java 代码 · 共 51 行

JAVA
51
字号
package com.dao.util;

import java.sql.*;
import java.util.Properties;

/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class DbConnect {	
	private Connection conn = null; 
	private String url = "";
	private String drive = "";
	private String user = "";
	private String password = "";
	public DbConnect(){
		try {
			Properties p = new Properties();
			try {
				p.load(
					DbConnect.class.getResourceAsStream(
						"/connection.properties"));

			} catch (Exception e) {
				//e.printStackTrace();
			}
			url = p.getProperty("url");
			drive = p.getProperty("drive");	
			user = p.getProperty("user");
			password = p.getProperty("password");			

		} catch (Exception ex) {
			System.err.print("系统在装载配置文件或查找数据源时产生错误,错误信息:" + ex.getMessage());
		}
	}
	public Connection getConnection() throws Exception{
		try { 
			Class.forName(drive); 
			conn=DriverManager.getConnection(url, user, password); 
			return conn;
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("数据库连接错误");
		}
	}
}

⌨️ 快捷键说明

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