利用Properties文件解析以及利用Java建立数据库连接
- 在下边先给一个properties解析出来 用户名、密码
public class PropertiesParser {
/*
* properties 配置文件处理工具
* 将所有键值对 存到Map里面
* 所以 propertiesMap定义为 static 和 final类型的
* 负责从外部读取键值对,再存到Map里面
* 同时对外要提供 可以有给出键 获取值的方法
* 到时需要 用户在不同配置文件里面 的键要统一命名
*/
private static final Map<String,String> propertyMap;
static {
propertyMap=new HashMap<>();
}
//此类主要提供静态方法供用户使用
public PropertiesParser() {
}
public static void loadProperties(String path) throws IOException {
if(path==null) {
return ;
}
InputStream is=PropertiesParser.class.
getResourceAsStream(path);
if(is==null) {
}
Properties property=new Properties();
property.load(is);
//获取 property 的键集合
Enumeration<Object> keys =property.keys();
while(keys.hasMoreElements()) {
String key=(String) keys.nextElement();
String value =property.getProperty(key);
String hasValue=propertyMap.get(key);
if(hasValue !=null) {
throw new KeyAlreadyExistsException
("键["+key+"]"+"已经存在!");
}
//写入propertyMap
propertyMap.put(key, value);
}
}
public static String getvalue(String key) {
return propertyMap.get(key);
}
public static Map<String,String> getBeginWith(String beginner){
Map<String ,String> result=new HashMap<>();
for (String key:propertyMap.keySet()) {
if(key.startsWith(beginner)) {
String value=propertyMap.get(key);
result.put(key, value);
}
}
return result;
}
- 接下来给出一个建立连接的类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Database {
private static Connection connection;
public Database() {
}
public static void loadDatabaseConfig(String path) {
PropertiesParser.loadProperties(path);
}
private static Connection getConnection() {
if (connection == null) {
try {
Class.forName(PropertiesParser.value("driver"));
connection = DriverManager.getConnection(
PropertiesParser.value("url"),
PropertiesParser.value("user"),
PropertiesParser.value("password"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
@SuppressWarnings("unchecked")
public <T> List<T> list(Class<?> klass) {
ClassTable ct = ClassTableFactory.getClassTable(klass);
if (ct == null) {
return null;
}
String sql = "SELECT " + ct.getColumnString() + " FROM " + ct.getTable();
List<T> result = new ArrayList<>();
try {
PreparedStatement state = getConnection().prepareStatement(sql);
ResultSet rs = state.executeQuery();
while(rs.next()) {
Object object = klass.newInstance();
ct.setFieldFromResultSet(rs, object);
result.add((T) object);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
public <T> T get(Class<?> klass, Object id) {
ClassTable ct = ClassTableFactory.getClassTable(klass);
if (ct == null) {
return null;
}
String sql = "SELECT " + ct.getColumnString() + " FROM " + ct.getTable()
+ " WHERE " + ct.getIdString() + "=?";
Connection connection = getConnection();
try {
PreparedStatement state = connection.prepareStatement(sql);
state.setObject(1, id);
ResultSet rs = state.executeQuery();
if (rs.next()) {
Object object = klass.newInstance();
ct.setFieldFromResultSet(rs, object);
return (T) object;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public int executeUpdate(String SQLString) {
Connection connection = getConnection();
try {
PreparedStatement state = connection.prepareStatement(SQLString);
return state.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public ResultSet executeQuery(String SQLString) {
ResultSet rs = null;
Connection connection = getConnection();
try {
PreparedStatement state = connection.prepareStatement(SQLString);
return state.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
这样就实现了,数据库的连接,用户只需要提供,数据库名,用户名,密码,就可以利用这两个类来进行数据库连接。