简体   繁体   English

尝试使用JSP页面连接到MySql时出现错误

[英]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: ...但是,甚至不应该这样:

  • Just using 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.

相关问题 当我尝试使用锚标记从我的jsp页面调用servlet页面时,显示错误 - when i am trying to call a servlet page from my jsp page using anchor tag it showing an error 当我尝试使用JPA连接时是否需要mysql连接器? - Is the mysql connector needed when I am trying to connect using JPA? 我正在尝试使用休眠xml映射连接到mysql数据库,但出现此错误 - I am trying to connect to a mysql database with hibernate xml mapping but I am getting this error 尝试 setOnClickListener 时出现错误 - I am getting error when trying to setOnClickListener 尝试使用servlet和jsp插入mysql时出错 - getting error while trying to insert in mysql using servlet and jsp 尝试连接到本地主机phpmyadmin时出现错误 - I am getting error while trying to connect to localhost phpmyadmin 我正在尝试在tomcat 6.0中运行一个jsp项目。 但是我收到以下错误 - I am trying to run a jsp project in tomcat 6.0. But I am getting the following error 我正在尝试从jsp运行shell脚本,但出现Ljava.lang.string错误 - i am trying to run a shell script from jsp but i am getting Ljava.lang.string error 为什么在jsp中使用if语句会出现此错误? - Why am I getting this error using if statement in jsp? 我正在尝试使用JSON格式显示来自MySQL数据库的数据,但我一直收到以下错误 - I am trying to display data from MySQL database using JSON Format but i keep getting the following error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM