URLDecoder()问题

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);
        }

    }
}

相关截图:

https://img1.sycdn.imooc.com//climg/645dd5a708af56f310000464.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
本人纯属虚构5444055 提问者 2023-05-15 15:55:06
6461e54c0001293710000464.jpg老师,还是这样
好帮手慕小蓝 2023-05-15 09:51:49

同学们好,在更新之后的URLDecoder中,decode方法已经更新为静态方法了,所以可能会出现同学遇到的问题。同学可以将这行代码更换为静态调用方式来尝试一下,如:

propertyFile = URLDecoder.decode(propertyFile, "UTF-8");

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师