URLDecoder()问题
package com.imooc.jdbc.sample;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.imooc.jdbc.common.DbUtils;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
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-confid.properties").getPath();
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 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);
}
}
}相关截图:

9
收起
正在回答 回答被采纳积分+1
2回答
本人纯属虚构5444055
2023-05-15 15:55:06
老师,还是这样
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星