Properties prop = PropertiesUtils.loadProp("/db.properties");
时间: 2024-04-14 15:27:13 浏览: 15
您可以使用以下代码加载名为 "db.properties" 的属性文件:
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesUtils {
public static Properties loadProp(String filePath) {
Properties prop = new Properties();
try (FileInputStream fis = new FileInputStream(filePath)) {
prop.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
}
```
请确保将 "db.properties" 文件放在正确的位置,并提供正确的文件路径作为 `filePath` 参数调用 `loadProp` 方法。这将返回一个 `Properties` 对象,您可以使用它来获取属性值。
相关问题
Properties prop = PropertiesUtils.loadProp("/db.properties"); String insertSql= "insert into system(id,name,`class`,phonenumber,address)" + "value(?, ?, ?, ?, ?)"; String selectSql = "select * from student"; Object[] datas = new Object[]{"1", "王", 101, "1234567891", "武汉1"}; String url = prop.getProperty("url"); String userName = prop.getProperty("userName"); String password = prop.getProperty("password"); String driver = prop.getProperty("driver"); insert(insertSql, datas, driver, url, userName, password);
您的代码片段中,首先加载了名为"db.properties"的属性文件,并将属性文件中的配置信息加载到`prop`对象中。
接下来,您定义了一个插入语句`insertSql`和一个查询语句`selectSql`,并创建一个包含插入数据的数组`datas`。
然后,您从`prop`对象中获取了数据库相关的配置信息,包括URL、用户名、密码和驱动程序。
最后,您调用了一个名为`insert()`的方法,传递了插入语句、数据数组以及数据库相关的配置信息作为参数。
根据代码片段中提供的信息,可能是在将数据插入到名为"system"的表中,并使用从属性文件中获取的数据库连接信息进行操作。
如果您有其他问题,请随时提问。
package db; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DBCon { Connection con = null; Statement st = null; ResultSet rs = null; String driver=null; String url = null; String username = null; String password = null; public Connection dbCon() { try { InputStream is=DBCon.class.getClassLoader().getResourceAsStream("db.properties"); Properties prop=new Properties(); try { prop.load(is); driver=prop.getProperty("driver"); url=prop.getProperty("url"); username=prop.getProperty("username"); password=prop.getProperty("password"); } catch (IOException e1) { e1.printStackTrace(); } Class.forName(driver); Class.forName("com.mysql.jdbc.Driver"); url = "jdbc:mysql:///sams?useUnicode=true&characterEncoding=utf8"; username = "root"; password = ""; try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return con; } /* * 增删改 */ public int query(String sql) { int rs = 0; con = dbCon(); try { st = con.createStatement(); rs = st.executeUpdate(sql); } catch (SQLException e) { close(); e.printStackTrace(); } return rs; } /* * 查 */ public ResultSet find(String sql) { try { con = dbCon(); st = con.createStatement(); rs = st.executeQuery(sql); } catch (SQLException e) { close(); e.printStackTrace(); } return rs; } /* * 关闭数据库 */ public void close() { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }有错误
在 `dbCon` 方法中,你已经通过 `Properties` 对象获取了 `driver`、`url`、`username` 和 `password` 的值,但是之后又重新对这些变量进行了赋值,这会导致你在连接数据库时使用的是硬编码的值而不是从 `db.properties` 文件中读取的值。因此,需要删除这些重新赋值的语句:
```
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql:///sams?useUnicode=true&characterEncoding=utf8";
username = "root";
password = "";
```
另外,你在 `find` 和 `query` 方法中都使用了 `con = dbCon();` 来获取连接,这会导致每次执行数据库操作都会重新获取一次连接,而不是复用已有的连接,这会导致性能问题。因此,应该在 `dbCon` 方法中增加一个判断,如果连接已经存在,则直接返回该连接,否则再新建连接。修改后的 `dbCon` 方法如下:
```
public Connection dbCon() {
if (con != null) {
return con;
}
try {
InputStream is=DBCon.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop=new Properties();
try {
prop.load(is);
driver=prop.getProperty("driver");
url=prop.getProperty("url");
username=prop.getProperty("username");
password=prop.getProperty("password");
} catch (IOException e1) {
e1.printStackTrace();
}
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return con;
}
```