| 方法名 | 说明 |
| Object setProperty(String key, String value) | 设置集合的键和值,都是String类型,底层调用 Hashtable方法 |
| put String getProperty(String key) | 使用此属性列表中指定的键搜索属性 |
| Set stringPropertyNames() | 从该属性列表中返回一个不可修改的键集,其中键及其对应的 值是字符串 |
示例代码:
public class PropertiesDemo {
public static void main(String[] args) {
//创建集合对象
Properties prop = new Properties();
//Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
prop.setProperty("test1", "孙悟空");
prop.setProperty("test2", "猪八戒");
prop.setProperty("test3", "唐三藏");
//Set stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
Set names = prop.stringPropertyNames();
for (String key : names) {
// System.out.println(key);
String value = prop.getProperty(key);
System.out.println(key + "," + value);
}
}
}
1.2 Properties和IO流相结合的方法
| 方法名 | 说明 |
| void load(InputStream inStream) | 从输入字节流读取属性列表(键和元素对) |
| void load(Reader reader) | 从输入字符流读取属性列表(键和元素对) |
| void store(OutputStream out, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流 |
| void store(Writer writer, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 |
示例代码:
public class PropertiesDemo01 {
public static void main(String[] args) throws IOException {
//把集合中的数据保存到文件
myStore();
//把文件中的数据加载到集合
myLoad();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
//void load(Reader reader):
FileReader fr = new FileReader("myfile\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("test1","孙悟空");
prop.setProperty("test2","猪八戒");
prop.setProperty("test3","唐三藏");
//void store(Writer writer, String comments):
FileWriter fw = new FileWriter("myfile\fw.txt");
prop.store(fw,null);
fw.close();
}
}
2.Jdbc工具类
2.1 简单工具类
示例代码:
public class JDBCUtils {
static final String driver = "com.mysql.jdbc.Driver";
static final String url = "jdbc:mysql:///yun?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false";
static final String user = "root";
static final String password = "123456";
//默认加载块,只加载一次
static{
try{
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接
public Connection getConnection() {
Connection conn=null;
try{
conn= DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public void closeAll(ResultSet rs, PreparedStatement psmt,Connection conn){
if(rs!=null){
try{
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(psmt!=null){
try{
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//设置参数的方法
public PreparedStatement setParam(PreparedStatement psmt,Object[]objects){
if(objects!=null){
for(int i=0;i0){
System.out.println("操作成功");
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll(null,psmt,conn);
}
System.out.println("操作失败");
return false;
}
//通用的查询方法
public List
2.2 使用properties集合创建工具类
示例代码:
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
//静态加载块,默认执行
static {
try {
//创建properties集合
Properties pro = new Properties();
//获取src路径下文件的方法ClassLoader
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
pro.load(new FileReader(path));
//3.获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(Statement stmt,Connection conn){
if( stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement stmt, Connection conn){
if( rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
jdbc.properties文件
#如果是locaclhost,可以省略不写,如下url url=jdbc:mysql:///yun user=root password=123456 driver=com.mysql.jdbc.Driver
以上就是Jdbc的简单工具类和Properties集合的结合使用,欢迎收藏~



