Andy Andy - 5 months ago 59
MySQL Question

my database is not getting connected simple on jsp page build on netbeans

The following is a simple code to show wheather database is connected or

not.I am using mysql workbench and netbeans.It is showing not connected

and the glassfish server is showing some warnings which are shown after
the code.

code

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Connection conn = null;

try{
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.getConnection
("jdbc:mysql://localhost:3306/userpass?zeroDateTimeBehavior
=convertToNull","root", "root");
if(conn!=null)
{
out.print("connected to database successfully");
}

}catch(Exception e)
{
out.print("not connected to database");
}

%>
</body>
</html>

<warning and info*/
Info: Loading application __admingui done in 7,651 ms
Warning: Context path from ServletContext: differs from path from
bundle:/
Info: Redirecting to /index.jsf
Info: Admin Console: Initializing Session Attributes...
Warning: Could not open/create prefs root node Software\JavaSoft\Prefs at
root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
Warning: Cannot create update center Image for C:\Program
Files\glassfish->4.1.1; Update Center functionality will not be available
in Admin Console

Answer

Try this maybe it is a syntax error also check if you have correct jar file or not-

Connection conn = null;

try{
     Class.forName("com.mysql.jdbc.Driver");
 conn =DriverManager.getConnection
 ("jdbc:mysql://localhost:3306/userpass","root", "root"); //removed after userpass
     if(conn!=null)
     out.println("connected to database successfully");
     else
     out.println("not connected to database");

  }catch(Exception e)
  {
   out.println(e);
 }