</configuration>
三 BaseDao的编写和测试 1、 BaseDao的编写package com.hx.basedao;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class BaseDao {
private static final String USER_PWD = "userPwd";
private static final String USER_NAME = "userName";
private static final String CONN_STR = "connStr";
private static final String DRIVER = "driver";
private static String driver = "";
private static String connStr = "";
private static String userName = "";
private static String userPwd = "";
static {
try {
getDBConfigByXML();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getDBConfigPro() {
// 创建属性文件对象
Properties p = new Properties();
// 获取要读取的属性文件的输入流
// 注意属性文件的路径
// 路径只能以/开头,表示根目录
// 不能以./或../开头
// 或者其他目录开头
// 参考网址:https://blog.csdn.net/cyxinda/article/details/78254057
InputStream is = BaseDao.class.getResourceAsStream("/DBConfig.properties");
// 获取BaseDao的路径
// System.out.println(BaseDao.class.getClassLoader().getResource("").getPath());
// 输出此代码,获取到路径,对比配置文件
try {
// 把流加载到属性文件,读取对象
p.load(is);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
driver = p.getProperty(DRIVER);
connStr = p.getProperty(CONN_STR);
userName = p.getProperty(USER_NAME);
userPwd = p.getProperty(USER_PWD);
}