为什么会报错呢,找不到原因了。。。。
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | package com.joey.jdbc; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.joey.jdbc.common.DbUtils; import com.joey.jdbc.entity.News; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import javax.sql.DataSource; import java.io.FileInputStream; import java.net.URLDecoder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; import java.util.Properties; import java.util.Scanner; public class ProcessNews { public static void menu() { System.out.println( " 欢迎来到新闻管理系统 " ); System.out.println( "========================" ); System.out.println( " 1-添加新闻 " ); System.out.println( " 2-查看新闻 " ); System.out.println( " 3-编辑新闻 " ); System.out.println( " 4-删除新闻 " ); System.out.println( " 5-退出系统 " ); System.out.println( " 请输入1-5之间的数字 " ); } public static void query() { Connection conn = null ; PreparedStatement pstmt = null ; Properties properties = new Properties(); String propertyFile = ProcessNews. class .getResource( "/druid-config.properties" ).getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "utf-8" ); properties.load( new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); QueryRunner qr = new QueryRunner(dataSource); String sql = "select * from news" ; List<News> list = qr.query(sql, new BeanListHandler<>(News. class )); for ( int i = 0 ; i <list.size() ; i++) { System.out.println(list.get(i).toString()); } } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeConnection( null ,pstmt,conn); } } public static void add() { Connection conn = null ; PreparedStatement pstmt = null ; Scanner sc = new Scanner(System.in); System.out.println( "请输入新闻标题" ); String title = sc.next(); System.out.println( "请输入新闻内容" ); String content = sc.next(); System.out.println( "请输入新闻日期:格式为yyyy-MM-dd" ); String strtime = sc.next(); java.util.Date udtime = null ; SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); try { udtime = sdf.parse(strtime); } catch (ParseException e) { e.printStackTrace(); } long time = udtime.getTime(); java.sql.Date sdtime = new java.sql.Date(time); Properties properties = new Properties(); String propertyFile = ProcessNews. class .getResource( "/druid-config.properties" ).getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "utf-8" ); properties.load( new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); try { conn.setAutoCommit( false ); } catch (SQLException e) { e.printStackTrace(); System.out.println( "getConnection出错" ); } QueryRunner qr = new QueryRunner(dataSource); String sql = "insert into news (title,content,creat_time) values" + "(?,?,?)" ; qr.update(conn, sql, new Object[]{title, content, sdtime}); conn.commit(); } catch (Exception e) { e.printStackTrace(); try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } } finally { DbUtils.closeConnection( null ,pstmt,conn); } } public static void update() { Connection conn = null ; PreparedStatement pstmt = null ; System.out.println( "请输入你要查询的新闻id" ); Scanner sc = new Scanner(System.in); Integer id = sc.nextInt(); Properties properties = new Properties(); String propertyFile = ProcessNews. class .getResource( "/druid-config.properties" ).getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "utf-8" ); properties.load( new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); try { conn.setAutoCommit( false ); } catch (SQLException e) { e.printStackTrace(); } QueryRunner qr = new QueryRunner(dataSource); String sql = "select * from news" ; List<News> list = qr.query(sql, new BeanListHandler<>(News. class )); for (News n : list) { if (n.getId() == id) { System.out.println( "请输入新的新闻标题:" ); String title = sc.next(); System.out.println( "请输入新的新闻内容" ); String content = sc.next(); String sql2 = "update news set title = ? ,content = ? where id = ?" ; qr.update(conn, sql2, new Object[]{title, content, n.getId()}); } } conn.commit(); } catch (Exception e) { e.printStackTrace(); try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } } finally { DbUtils.closeConnection( null ,pstmt,conn); } } public static void delete() { Scanner sc = new Scanner(System.in); System.out.println( "请输入要删除的新闻编号:" ); Integer id = sc.nextInt(); Connection conn = null ; PreparedStatement pstmt = null ; Properties properties = new Properties(); String propertyFile = ProcessNews. class .getResource( "/druid-config.properties" ).getPath(); try { propertyFile = new URLDecoder().decode(propertyFile, "utf-8" ); properties.load( new FileInputStream(propertyFile)); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); try { conn.setAutoCommit( false ); } catch (SQLException e) { e.printStackTrace(); } QueryRunner qr = new QueryRunner(dataSource); String sql = "select * from news" ; List<News> list = qr.query(sql, new BeanListHandler<>(News. class )); for (News n : list) { if (n.getId() == id) { String sql2 = "delete from news where id = ?" ; qr.update(conn, sql2, new Object[]{id}); } } conn.commit(); } catch (Exception e) { e.printStackTrace(); try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } } finally { DbUtils.closeConnection( null ,pstmt,conn); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer input = 0 ; while ( true ) { ProcessNews.menu(); try { input= sc.nextInt(); } catch (Exception e) { e.printStackTrace(); System.out.println( "请输入1-5之间的整数" ); sc.next(); continue ; } if (input == 5 ) { System.out.println( "已退出" ); break ; } switch (input) { case 1 : ProcessNews.add(); break ; case 2 : ProcessNews.query(); break ; case 3 : ProcessNews.update(); break ; case 4 : ProcessNews.delete(); break ; default : System.out.println( "输入有误请重新输入(1-5)" ); break ; } } } } |
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 | package com.joey.jdbc.entity; public class News { private Integer id; private String title; private String content; private String creat_time; public News() { } public News(Integer id, String title, String content, String creat_time) { this .id = id; this .title = title; this .content = content; this .creat_time = creat_time; } @Override public String toString() { return "News{" + "id=" + id + ", title='" + title + '\ '' + ", content='" + content + '\ '' + ", creat_time='" + creat_time + '\ '' + '}' ; } public Integer getId() { return id; } public void setId(Integer 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 String getCreat_time() { return creat_time; } public void setCreat_time(String creat_time) { this .creat_time = creat_time; } } |
36
收起
正在回答
4回答
同学你好,这不是错误信息,这是日志信息。jdk的info级别的日志默认是红色的。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
java工程师2020版
- 参与学习 人
- 提交作业 9409 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧