private static String url = null
private static String user = null
private static String password = null
private static BasicDataSource ds
static{
//读取程序外的.properties 文件
//需要.properties文件的包路径
Properties props = new Properties()
try {
String path ="db.properties"
props.load(
DBUtils.class.getResourceAsStream(path)
)
//properties对象.getProperty("字符串")
driver=props.getProperty("driver")
url=props.getProperty("url")
user=props.getProperty("user")
password=props.getProperty("password")
ds = new BasicDataSource()
ds.setDriverClassName(driver)
ds.setUrl(url)
ds.setUsername(user)
ds.setPassword(password)
Class.forName(driver)
} catch (Exception e) {
e.printStackTrace()
}
}
这是一个JDBC读取配置文件连接数据库的示例代码,供参考!
Map<String, String>map = new HashMap<String, String>()map.put("key1", "value1")
map.put("key2", "value2")
map.put("key3", "value3")
Set<String>keySet = map.keySet()
int i = map.size()-1
Map<Integer, String>mapKey = new HashMap<Integer, String>()
Map<Integer, String>mapValue = new HashMap<Integer, String>()
for(java.util.Map.Entry<String, String>entry : map.entrySet()) {
mapKey.put(i, entry.getValue())
//将原来MAP的KEY放入新的MAP的VALUE 里面
mapValue.put(i, entry.getKey())
i--
}
//打印KEY值
System.out.println(mapKey.get(2))
//打印VALUE值
System.out.println(mapValue.get(0))
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)