has private access in 'java.net.URLDecoder' 这是为什么呢
来源:8-2 Druid连接池的配置与使用(一)
我看到你没学习1453139929
2022-09-30 18:40:49


package com.imooc.jdbc.sample;
import com.imooc.jdbc.common.DbUtils;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
/**
* Druid连接池配置与使用
*/
public class DruidSample {
public static void main(String[] args) {
//1. 加载属性文件
Properties properties = new Properties();
String propertyFile = DruidSample.class.getResource("/druid-config.properties").getPath();
//空格->%20 | c:\java code\druid-config.properties
//c:\java%20code\druid-config.properties
try {
propertyFile = new URLDecoder().decode(propertyFile, "UTF-8");
properties.load(new FileInputStream(propertyFile));
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//2. 获取DataSource数据源对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//3. 创建数据库连接
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("select * from employee limit 0,10");
rs = pstmt.executeQuery();
while (rs.next()) {
Integer empId = rs.getInt(1);
String ename = rs.getString("ename");
String dname = rs.getString("dname");
Float salary = rs.getFloat("salary");
System.out.println(dname + "-" + empId + "-" + ename + "-" + salary);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
/**
* 不使用连接池:conn.close()关闭连接
* 使用连接池:conn.close()将连接回收至连接池
*/
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}2回答
好帮手慕小脸
2022-10-05
同学你好,这里的代码并不是编译错误,而是IDEA的警告。由于decode方法是静态方法,推荐使用类名去调用,所以这行代码可以更换为:
propertyFile = URLDecoder.decode(propertyFile, "UTF-8");
祝学习愉快~
月是天上月阿
2022-10-02
用静态方法的方式去访问就可以了
相似问题
回答 1
回答 1