系统找不到指定得文件呢?
package Dblianljc; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.mysql.cj.QueryResult; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.List; import java.util.Properties; public class news_jdbc { public static void jdbc_mysql(){ Properties properties=new Properties(); //String typ = news_jdbc.class.getResource("/druid-config.properties").getPath(); String typ = news_jdbc.class.getResource("/druid-config.properties").getPath(); try { typ=new URLDecoder().decode("typ","UTF-8"); properties.load(new FileInputStream(typ)); } catch (Exception e) { e.printStackTrace(); } try { DataSource dataSource=DruidDataSourceFactory.createDataSource(properties); QueryRunner queryResult=new QueryRunner(dataSource); List<News> listnews=queryResult.query("SELECT * FROM `news_one` LIMIT ?,10;", new BeanListHandler<>(News.class), new Object[]{10}); for (News news:listnews){ System.out.println(news); } } catch (Exception e) { e.printStackTrace(); } } } package Dblianljc; import java.util.Date; public class News { private int id; private String title; private String content; private Date create_time; public News(){} public News(int id,String title,String content,Date create_time){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreate_time() { return create_time; } public void setCreate_time(Date create_time) { this.create_time = create_time; } @Override public String toString() { return "新闻=" + ",新闻id=" + id + ",新闻标题=" + title + '\'' + ",新闻内容=" + content + '\'' + ",新闻时间=" + create_time + '}'; } } package Dblianljc; public class news_test { public static void main(String[] args) { news_jdbc nj=new news_jdbc(); nj.jdbc_mysql(); } } driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/news?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username=root password=123456
11
收起
正在回答
1回答
同学你好,从同学截图中看,配置文件似乎没有处于src根目录下,而是存储在了package中。如果是这样的话,确实是找不到文件的,同学需要将配置文件移动到src下才可以。具体可以参考课程 https://class.imooc.com/lesson/2083#mid=49468 的3分30秒左右讲解。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星