连接池中有报错无法解决


package com.imooc.jdbc.sample;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import common.DbUtils;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
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 (UnsupportedEncodingException 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 eno = rs.getInt(1); //JDBC中字段索引从1开始,而非0
String ename = rs.getString("ename");
Float salary = rs.getFloat("salary");
String dname = rs.getString("dname");
System.out.println(dname + "-" + eno + "-" + ename + "-" + salary);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}11
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕小蓝
2023-10-05 09:16:34
同学你好,建议同学点开URLDecoder类的源码查看一下:
1.decode方法是否是静态方法;
2.URLDecoder构造方法是否为私有方法。
由于高版本的URLDecoder类中,已经不再允许创建URLDecoder对象,并且将decode方法修改为了静态方法。如果同学使用的版本与课程不一致,确实会出现这样的问题,此处只需要将
new URLDecoder().decode(propertyFile, "UTF-8");
修改为
URLDecoder.decode(propertyFile, "UTF-8");
这样的静态调用方式即可。
祝学习愉快~
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星