为什么会报错呢,找不到原因了。。。。

为什么会报错呢,找不到原因了。。。。

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

http://img1.sycdn.imooc.com//climg/5f089b750913229518971022.jpg

http://img1.sycdn.imooc.com//climg/5f089b8d09b8727d19290983.jpghttp://img1.sycdn.imooc.com//climg/5f089bb20927495410511021.jpg

正在回答

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

4回答

同学你好,这不是错误信息,这是日志信息。jdk的info级别的日志默认是红色的。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

Joey_yxy 提问者 2020-07-11 14:53:33

为什么会有这样红色的提示,是错误信息吗,还是jdk自带的提示呢,很奇怪

http://img1.sycdn.imooc.com//climg/5f0961ea0979a69c15271015.jpg

好帮手慕阿慧 2020-07-11 11:41:14

同学你好,driverClassName值中jc写错了,应该是cj

如下:

http://img1.sycdn.imooc.com//climg/5f0934b70920583504060062.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 Joey_yxy #1
    老师,改成cj之后还是提示同样的错误。。还有哪里有问题么老师
    2020-07-11 14:46:17
  • 提问者 Joey_yxy #2
    老师,我找到了。。少引入了一个jar包,但还有一个问题,粘贴到下面的回复中了,您看一下
    2020-07-11 14:54:37
Joey_yxy 提问者 2020-07-11 00:59:14
1
2
3
4
5
6
driverClassName = com.mysql.jc.jdbc.Driver
url = jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username = root
password = root
initialSize = 10
maxActive = 20


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

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

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

0 星
请稍等 ...
微信客服

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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