I am unable to connect to MySql using JSP page. 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.
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.
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
You don't need to use Class.forName
anyway now, if you're working with a fairly modern JRE. From the docs for DriverManager
:
Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
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. Just use:
Connection con = DriverManager.getConnection(...);
Statement stmt = con.createStatement();
// ...
See more on this question at Stackoverflow