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
First of all I will say writing database code in jsp is not good way to code.All the database code and business logic must be written in servlet or plain java class.Jsp is view, after processing from servlet , to show output we can use jsp.
This is just a warning, you can ignore it if you want.
The warning is raised because you probably have a leading slash (i.e. /) in your context-root in glassfish-web.xml (should be in the WEB-INF folder of the WAR).
You may get rid of the warning if you remove the leading slash so your glassfish-web.xml looks similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>SalutationApp-war</context-root>
</glassfish-web-app>
I hope I helped u.
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);
}
User contributions licensed under CC BY-SA 3.0