JavaWeb 踩坑记录

采用 JSP + Servlet + Mysql 进行 JavaWeb 开发过程中遇的一些问题

主要的学习参考网站:http://how2j.cn/k/jdbc/jdbc-statement/387.html

在连接数据库的过程中出现了诸多问题,总而言之就是,教程是死的而mysql的版本是不断变化的,我下载的是最新版的mysql所以出现了不兼容的情况。

如果想快速解决,可以下载老版的mysql(打开http://how2j.cn/k/mysql/mysql-install/377.html,右侧即可下载)。但技术本身是不断迭代更新的,一味地使用旧版本去逃避,终归也不是个办法,那就只有直面这些问题了。

步骤 1 : 为项目导入mysql-jdbc的jar包

步骤 2 : 初始化驱动

这两步按照教程操作下来没有什么问题,右键->Run as->Java application ,控制台也输出了成功的信息

步骤 3 : 建立与数据库的连接

进行到这一步,就开始报错了

首先要修改

1
2
3
4
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
"root", "admin");

这里的数据库名、用户名、密码名修改为本机实际对应的参数

运行,控制台报错

1
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client

百度到的信息基本上都无关,但在这个参考网站的留言板上看到了一个人说

”已找到答案,站长所示MySQL的jdbc的jar包需要更新,大家可以去官网下载。“

好吧那就更新去吧

怎么在官网上下载java连接mysql的驱动jar包?

下载后复制目前最新的mysql-connector-java-8.0.12.jar到WebContent\WEB-INF\lib 文件夹下,再右键project->property->java build path->libaries->add external jars

再次运行,依旧报错,但报错信息已经发生了变化,说明更新jar包是有用的

1
2
3
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Fri Oct 05 14:14:12 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

(仔细读一下其实可以发现以上有三个报错)

查了一下,发现因为jar包更新了,所以原来用于初始化驱动的代码也要变化:

1
2
Class.forName("com.mysql.jdbc.Driver");//删去
Class.forName("com.mysql.cj.jdbc.Driver");//添加

运行,依旧报错

1
2
Fri Oct 05 14:17:11 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

(报错变成了两个)

经查,原因同上,版本更新所致 https://blog.csdn.net/lvchengbo/article/details/54429709

1
2
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8  更新为
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false

运行,依旧报错

1
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

(报错变成了一个)

经查,原因同上,版本更新所致 https://blog.csdn.net/weixin_37577564/article/details/80329775

1
2
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false 更新为
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT

运行,连接成功

第二天,运行突然又报错了

1
Public Key Retrieval is not allowed

经查,原因同上,版本更新所致 https://blog.csdn.net/u013360850/article/details/80373604

1
2
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT 更新为
jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true

运行,连接成功

完整代码如下:

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
package com.JDBC;
import java.sql.*;


public class DateBase {
public static void main(String[] args) {

try {
Class.forName("com.mysql.cj.jdbc.Driver");

// 建立与数据库的Connection连接
// 这里需要提供:
// 数据库所处于的ip:127.0.0.1 (本机)
// 数据库的端口号: 3306 (mysql专用端口号)
// 数据库名称 logistics
// 编码方式 UTF-8
// 账号 root
// 密码 root

Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/logistics?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true",
"root", "root");

System.out.println("连接成功,获取连接对象: " + c);

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

创建一个新filter有两种方式

  1. New->Class 然后点击interfaces右侧的Add,第一次添加时,弹出的界面内容都是空白的,接着在 Choose interfaces 中输入 javax.servlet.filter 选中第一个即可
  2. New->Filter (建议选用该方式)

action=”/login.do” 404报错

action=”login.do” 运行成功


启动tomcat却一直报错404找不到jsp文件

查了半天 https://blog.csdn.net/lynn_wgr/article/details/7751228

成功解决


莫名其妙一到保存web.xml文件时eclipse就要卡死

网上查了查 https://blog.csdn.net/opnmzxcvb/article/details/6959585

都是五六年前的博文,说是xml版本不对应,但我明显是对应的啊

中途换了idea 没这个问题了,但idea还有其他的一些问题,最终弃用

折腾了一下午,最后自己琢磨了一下,重新创建了一个web工程,创建时把 dynamic web module version 从4.0改成了3.0

成功解决!!!!


当tomcat运行有误时一定要看控制台的出错信息!!!!


insert into order(userId, carId, total, content, status, startAddress, endAddress, createDate, userRealName) values (?, ?, ?, ?, ?, ?, ?, ?, ?)

一直报错在eclipse中调试了半天都不知道是怎么回事,直到黄大师表示”我们在mysql客户端里直接运行一下吧“

复制粘贴,把问号改为相应的参数,果然运行失败提示语句错误,但还是没什么头绪

突然我发现order和status这两个单词的颜色和它们周围的不一样!!

我恍然大悟——这两个单词是sql语句中的关键字,不能用作名称!!

然后改了改这两处,运行成功!


1.js中使用el表达式要加双引号或单引号:’${list}’

2.js变量获取el表达式中的对象:不能直接获取,直接获取得到的是该对象的toString值。

有两种方法:

一:el中直接写对象的属性var abc = ‘${list[0].index}’;——–如用js数组变量装el表达式对象的每一个属性

二:先把对象转换成json再把json转换成对象赋值给js变量。


文章目录
|