[英]I am getting error when trying to connect to MySql using JSP page
I am unable to connect to MySql using JSP page. 我无法使用JSP页面连接到MySql。 This is my connection query -
这是我的连接查询-
<%
String driver = "com.mysql.jdbc.Driver";
Class.forName("Driver").newInstance();
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
con=DriverManager.getConnection("jdbc:mysql://10.1.11.103:3306/jiradb40","opsrptusr", "opsrptusr");
stmt=con.createStatement();
String query = "(select * from jiraissue limit 2)";
%>
I have placed jar file in lib folder still getting the error. 我将jar文件放在lib文件夹中仍然出现错误。
Any help is well appreciated. 任何帮助都将不胜感激。
Exception report 异常报告
message An exception occurred processing JSP page /Worklog.jsp at line 5
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /Worklog.jsp at line 5
2: <%@page import="java.text.DecimalFormat" %>
3: <%
4: String driver = "com.mysql.jdbc.Driver";
5: Class.forName("Driver").newInstance();
6: Connection con=null;
7: ResultSet rst=null;
8: Statement stmt=null;
This is incorrect: 这是不正确的:
Class.forName("Driver").newInstance();
That's trying to find a class called Driver
in the default package. 试图在默认包中找到一个名为
Driver
的类。
You potentially want: 您可能想要:
Class.forName(driver).newInstance();
... but even that shouldn't be required: ...但是,甚至不应该这样:
Class.forName()
should initialize the class, which would then register itself with DriverManager
Class.forName()
初始化该类,然后将其自身注册到DriverManager
You don't need to use Class.forName
anyway now, if you're working with a fairly modern JRE. 如果您使用的是相当现代的JRE,则现在无论如何都不需要使用
Class.forName
。 From the docs for DriverManager
: 从
DriverManager
的文档中:
Applications no longer need to explictly load JDBC drivers using Class.forName().
应用程序不再需要使用Class.forName()显式加载JDBC驱动程序。 Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
当前使用Class.forName()加载JDBC驱动程序的现有程序将继续运行,而无需进行修改。
Note that as a matter of style, there's no point in declaring variables and giving them an initial value of null
, only to assign a new value immediately. 请注意,就样式而言,声明变量并为其赋予初始值
null
只是为了立即分配一个新值,这没有意义。 Just use: 只需使用:
Connection con = DriverManager.getConnection(...);
Statement stmt = con.createStatement();
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.