JavaWeb:常见异常合集----初学者的乐园
- 零、相关阅读
- 一、运行时异常
-
- 1、类找不到异常 ---- java.lang. ClassNotFoundException: com.mysql.jdbc.Driver
- 2、通信异常,通信连接失败 ---- com. mysql. jdbc. exceptions. jdbc4. CommunicationsException: Communications link failure
- 3、MySQL非暂时连接异常 ---- connection不允许在关闭之后操作MySQLNonTransientConnectionException: No operations allowed after connection closed.
- 4、SQL异常
-
- (1)没有找到合适的驱动 ---- java.sql. SQLException: No suitable driver found for jdbc.mysql://localhost:3306/week3
- (2)访问拒绝 ---- java. sql. SQLException: Access denied for user 'root'@'localhost' (using password: YES)
- (3)PreparedStatement设置参数的下标超过了占位符的数量 ---- java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
- (4)PreparedStatement设置参数的下标小于占位符的数量 ---- java.sql.SQLException: No value specified for parameter 2
- (5)executeQuery不能执行DML语句 ---- SQLException: Can not issue data manipulation statements with executeQuery()
- (6)executeUpdate不能执行select语句 ---- SQLException: Can not issue executeUpdate() or executeLargeUpdate() for SELECTs
- (7)ResultSet不允许在关闭之后操作 ---- SQLException: Operation not allowed after ResultSet closed
- (8)statement不允许在关闭之后操作 ---- SQLException: No operations allowed after statement closed.
- 5、MySQL语法错误异常
- 6、不合法状态异常,不能再相应提交后在进行转发 ---- IllegalStateException: Cannot forward after response has been committed
- 二、编译错误
-
- 1、类型不匹配:不能从java.sql.Connection转换到com.mysql.jdbc.Connection ---- Type mismatch: cannot convert from java.sql.Connection to com.mysql.jdbc.Connection
- 2、不能使用类名调用非静态方法 ---- Cannot make a static reference to the non-static method getConn() from the type DBUtil
- 3、方法未定义 ---- The method getConn() is undefined for the type DBUtil
- 4、构造方法未定义 ---- The constructor Student(int, String, String, String, String, String, String) is undefined
- 5、不能被识别为一个变量 ---- stuList cannot be resolved to a variable
- 6、tomcat 启动报错 ---- Caused by: java. lang. IllegalArgumentException: The servlets named [login] and [com.baway.servlet.MgrStuServlet] are both mapped to the url-pattern [/login.do] which is not permitted
- 7、表单提交请求参数乱码
- 8、jsp页面乱码:常见现象,登录失败
- 9、访问页面404
零、相关阅读
不常见异常 ---- javax.el.PropertyNotFoundException:Property not found on type java.lang.String
参见:JavaWeb: 论强迫症, javax.el.PropertyNotFoundException:Property not found on type java.lang.String
https://blog.csdn.net/weixin_43473435/article/details/84066675
一、运行时异常
1、类找不到异常 ---- java.lang. ClassNotFoundException: com.mysql.jdbc.Driver
排查步骤-解决方案
①、检查驱动包名类名是否拼写错误————修正拼写错误
②、检查驱动jar包是否引入————引入jar包(选中项目,右键build path=》configure build path=》上面四个菜单选择 libraries=》选择右侧第二个add external jars=>找到jar包路径打开)
③、对应web项目,检查tomcat=>webapps=》当前项目文件夹=>WEB-INF=>lib文件夹下是否存在驱动jar包————如果在部署后才引入jar包,需要停止服务,重新部署下
2、通信异常,通信连接失败 ---- com. mysql. jdbc. exceptions. jdbc4. CommunicationsException: Communications link failure
排查步骤-解决方案
①检查驱动URL中域名和端口是否拼写错误
3、MySQL非暂时连接异常 ---- connection不允许在关闭之后操作MySQLNonTransientConnectionException: No operations allowed after connection closed.
排查步骤-解决方案
connection不允许在关闭之后操作
4、SQL异常
(1)没有找到合适的驱动 ---- java.sql. SQLException: No suitable driver found for jdbc.mysql://localhost:3306/week3
排查步骤-解决方案:驱动URL是否拼写错误
①检查是否注册驱动
②检查是否有中文标点
③注意jdbc和mysql的拼写及顺序
④注意中间拼接都是用冒号而不是小数点
(2)访问拒绝 ---- java. sql. SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)
排查步骤-解决方案
①密码输入错误————修改连接密码
(3)PreparedStatement设置参数的下标超过了占位符的数量 ---- java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
排查步骤-解决方案
检查参数的下标超过了占位符的数量
(4)PreparedStatement设置参数的下标小于占位符的数量 ---- java.sql.SQLException: No value specified for parameter 2
排查步骤-解决方案
PreparedStatement设置参数的下标小于占位符的数量,增加设置参数个数