Search code examples
mysqldatabasejspdatabase-connectionhttp-status-code-500

how to fix an error on " DriverManager.getConnection(connectionURL, UN, PW); "?


I'm new to MYSQL and JSP. I am trying check the connectivity with between mysql databse and jsp. When I run the project on server it throws an exception. I cann't find the error on code.

I have created oop database on mysql and UN, PW are also correct.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>Connection status </h1>
<% 

/* Create string of connection url within specified format with machine name, 
port number and database name. Here machine name id localhost and 
database name is oop. */ 
String connectionURL = "jdbc:mysql://localhost:3306/oop"; 

// declare a connection by using Connection interface 
Connection connection = null; 

// Load JBBC driver "com.mysql.jdbc.Driver" 
Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructors(); 

/* Create a connection by using getConnection() method that takes parameters of 
string type connection url, user name and password to connect to database. */ 
connection = DriverManager.getConnection(connectionURL, "root", "raviya");

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed()){

%>
<font size="+3" color="green">
<% 
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}

else{

%>
</font>
<font size="+3" color="red">

<%
out.println("Unable to connect to database.");
}
%>
</font>
</body>
</html>

Error: enter image description here


Solution

  • If you are not import mysql connector jar file these type of error can occur. So you can download jar file related to the your version and you can paste it in your own project /WEB-INF/lib folder.