java读取配置文件的几种方式

web工程一般都会连接数据库,然而加载数据库配置文件主要有几种。

1.读取properties文件,连接数据库   //这是最简单的方式,

注:config.properties在src目录下

java读取配置文件的几种方式_第1张图片

java读取配置文件的几种方式_第2张图片

java读取配置文件的几种方式_第3张图片

#DB Properties
Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
DBurl=jdbc:sqlserver://127.0.0.1\\:1433;DatabaseName=PCMgmt
DBUser=sa
DBPwd=123

DBUtils.java

package com.lee.demo.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtils {

	public static Connection getConnection() {
		Connection conn = null;
		InputStream inputStream = DBUtils.class.getResourceAsStream("/config.properties");
		String driver = "";
		String dburl = "";
		String user = "";
		String pwd = "";
		Properties p = new Properties();
		try {
			p.load(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		driver = p.getProperty("Driver");
		dburl = p.getProperty("DBurl");
		user = p.getProperty("DBUser");
		pwd = p.getProperty("DBPwd");
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(dburl, user, pwd);
			System.out.println("Database connected successful...");
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

		return conn;

	}

	public static void release(Connection conn) {
		try {
			if(conn!=null) {
				conn.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			System.out.println("release successful...");
		}
		
	}

	public static void release(PreparedStatement prep, Connection conn) {

		try {
			if(conn!=null) {
				conn.close();
			}
			if(prep!=null) {
				prep.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			prep = null;
			System.out.println("release successful...");
		}
	}

	public static void release(PreparedStatement prep, Connection conn, ResultSet rs) {

		try {
			if(conn!=null) {
				conn.close();
			}
			if(prep!=null) {
				prep.close();
			}
			if(rs!=null) {
				rs.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			prep = null;
			rs = null;
			System.out.println("release successful...");
		}
		
	}

	public static void main(String[] args) {
		DBUtils.release(DBUtils.getConnection());
	
	}

}


2.读取xml配置文件,连接数据库  //我这里涉及解析xml,需要用到dom4j.jar,自行下载

java读取配置文件的几种方式_第4张图片

java读取配置文件的几种方式_第5张图片

java读取配置文件的几种方式_第6张图片




com.microsoft.sqlserver.jdbc.SQLServerDriver
127.0.0.1
1433
PCMgmt
sa
123


DBUtils_2.java

package com.lee.demo.util;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DBUtils_2 {
	
	public static void main(String[] args) {
		DBUtils_2.release(getConnection());
	}
	
	
	public static Connection getConnection() {
		Connection conn = null;
		Map map = getPropertiey();
		String Driver = map.get("Driver");
		String Server = map.get("Server");
		String DBName = map.get("DBName");
		String DBPort = map.get("DBPort");
		String DBUser = map.get("DBUser");
		String DBPsWd = map.get("DBPsWd");
		String DB_Url = "jdbc:sqlserver://"+Server+"\\:"+DBPort+";DatabaseName="+DBName;
		try {
			Class.forName(Driver).newInstance();
			conn = DriverManager.getConnection(DB_Url, DBUser, DBPsWd);
			System.out.println("Database connected successful...");
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public static Map getPropertiey() {
		Map map = new HashMap();
		File file = new File(DBUtils_2.class.getResource("database.xml").getFile());
		SAXReader saxReader = new SAXReader();
		Document document;
		try {
			document = saxReader.read(file);
			Element element = document.getRootElement();
			Iterator iterator = element.elementIterator();
			while (iterator.hasNext()) {
				org.dom4j.Element e = (org.dom4j.Element) iterator.next();
				//System.out.println(e.getName() + " ");
				List attributes = e.attributes();
				Iterator it = e.elementIterator();
				while (it.hasNext()) {
					/*
					 * org.dom4j.Element object = (org.dom4j.Element) it.next();
					 * System.out.println(object.getName() + ":" + object.getText() + "\t");
					 */
					Element object = (org.dom4j.Element) it.next();
					try {
						map.put(object.getName(), object.getText());
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return map;
	}
	
	
	
	
	public static void release(Connection conn) {
		try {
			if(conn!=null) {
				conn.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			System.out.println("release successful...");
		}
		
	}

	public static void release(PreparedStatement prep, Connection conn) {

		try {
			if(conn!=null) {
				conn.close();
			}
			if(prep!=null) {
				prep.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			prep = null;
			System.out.println("release successful...");
		}
	}

	public static void release(PreparedStatement prep, Connection conn, ResultSet rs) {

		try {
			if(conn!=null) {
				conn.close();
			}
			if(prep!=null) {
				prep.close();
			}
			if(rs!=null) {
				rs.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			conn = null;
			prep = null;
			rs = null;
			System.out.println("release successful...");
		}
		
	}
	

}

3.web.xml配置连接池。这个不想写了



你可能感兴趣的:(java)