Class> c =Class.forName("android.os.SystemProperties
时间: 2024-05-19 09:14:48 浏览: 15
");This code will attempt to load the class "android.os.SystemProperties" dynamically at runtime using the Class.forName() method. If the class is found, it will return a reference to the Class object representing that class, which can then be used to access the members of that class. If the class is not found, a ClassNotFoundException will be thrown.
相关问题
Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'prefix'; <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <list>
这个错误是因为在 Spring 的属性注入过程中,发现无法将类型为 'java.util.ArrayList' 的属性值转换为类型为 'java.lang.String' 的属性值,因为二者类型不匹配。根据你提供的代码片段,出错的地方是在配置 InternalResourceViewResolver 的 prefix 属性时。
原因是 prefix 属性应该是一个字符串类型,但是你使用了一个列表类型来设置这个属性的值。你需要将列表中的元素拼接成一个字符串作为 prefix 属性的值。例如:
```
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
```
在这个例子中,我们使用了字符串类型的值 "/WEB-INF/views/" 来设置 prefix 属性的值。如果你有多个值需要拼接,可以使用 Spring EL 表达式来实现。例如:
```
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="#{'/WEB-INF/views/'.concat(systemProperties['user.dir'])}" />
<property name="suffix" value=".jsp" />
</bean>
```
这个例子中,我们使用了 Spring EL 表达式来将 "/WEB-INF/views/" 和系统属性 user.dir 拼接成一个字符串作为 prefix 属性的值。
package db; import java.sql.*; public class Shujuku { private static final String URL = "jdbc:mysql://localhost:3306/?db_student"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; public void kk(){ try{ Class.forName(DRIVER_CLASS); Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement=conn.createStatement(); System.out.println("已经成功连接到数据库!"); } catch (Exception e) { throw new RuntimeException(e); } }}优化
优化点:
1. 数据库连接信息应该封装到配置文件中,以便于修改和维护。
2. 数据库连接应该使用连接池进行管理,避免频繁创建和销毁连接对象。
3. 应该使用 PreparedStatement 对象执行 SQL 语句,避免 SQL 注入攻击。
4. 避免在方法中抛出 RuntimeException 异常,应该捕获异常并进行处理。
下面是优化后的代码:
```
package db;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DatabaseUtil {
private static DataSource ds;
static {
try {
InputStream is = DatabaseUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(is);
ds = DruidDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
```
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Shujuku {
public void kk() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
ps = conn.prepareStatement("select * from student");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(conn, ps, rs);
}
}
}
```
这里使用了阿里巴巴的 Druid 数据库连接池,可以在 Maven 中添加以下依赖项来获取:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
```
需要在类路径中添加 `db.properties` 配置文件,内容如下:
```
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_student
username=root
password=123456
initialSize=5
minIdle=5
maxActive=20
```
其中 `initialSize` 表示初始化连接数,`minIdle` 表示最小空闲连接数,`maxActive` 表示最大连接数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![properties](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)