系统找不到指定得文件呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 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积分~
来为老师/同学的回答评分吧