Mumbai GT Mumbai GT - 1 month ago 7
Javascript Question

unknown database while running servlet code

i am trying my hands on simple servlet and database connectivity program but i have been facing issue with db connectivity after running my code and filling up the form and submitting it i get unknown databaseissue.Have been looking into many sites for the plausible solution but couldn't get any..

Take a look at the servlet code i.e RegisterServlet.java page

package com.jwt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class RegisterServlet extends HttpServlet {


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n = request.getParameter("userName");
String p = request.getParameter("password");
String e = request.getParameter("email");
String c = request.getParameter("language");
try{
System.out.println("connecting...");

Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to physical database...");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/User1", "root", "root");

PreparedStatement ps = con.prepareStatement("insert into USERDETAILS values(?,?,?,?)");
System.out.println("Connection has been made");

ps.setString(1, n);
ps.setString(2, p);
ps.setString(3, e);
ps.setString(4, c);

int i = ps.executeUpdate();
if (i > 0)
out.print("You are successfully registered...");

} catch (Exception e2) {
System.out.println(e2);
}


out.close();
}

}


Here is the register.jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function validateForm() {
var x = document.forms["myForm"]["userName"].value;
var y = document.forms["myForm"]["password"].value;
var z = document.forms["myForm"]["email"].value;
var checkString = document.myForm.userName.value;


if ((x == null || x == "") || (y==null || y=="") || (z==null || z=="" ) )
{
alert("Enter the mandatory fields");
return false;
}
}
</script>
</head>
<body>
<div style="background-color:grey;color:white;padding:25px 75px 25px 50px; text-align:center; border: 1px solid red; display:inline-block; text- style:bold">
<form name="myForm" action="register" onsubmit="return validateForm()" method="post">
<p id="tt" style="margin-left"></p>
<script>
document.getElementById("tt").innerHTML = Date();
</script>
Name*:<input type="text" name="userName" placeholder="Enter Name" style="margin-left:35px; " onkeypress="return alpha(event)" /><br/>
Password*:<input type="password" name="password" style="margin- left:12px; margin-top:10px" placeholder="Enter Password" /><br/>
Email Id*:<input type="text" name="email" placeholder="Enter Email Id" style="margin-left:18px; margin-top:10px" /><br/>
Language: <select name="language" style="margin-left:80px; margin-top:10px">
<option>--Select--</option>
<option>Hindi</option>
<option>English</option>
<option>French</option>
</select> <br/>
<p id="demo" style="display:none">Saved...</p>
<input type="submit" onclick="document.getElementById('demo').style.display='block'" value="Submit" style="margin-left:150px; margin-top:10px"/>
<footer style="margin-bottom"> By </footer>
</form>
</div>
</body>
</html>


This is the web.xml page

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ServletDBExample</display-name>
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>com.jwt.servlet.RegisterServlet</servlet-class>
</servlet>



<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
</web-app>

Answer

Try removing the username & password OR try changing it to other users. It shall work. I came across the same error and it seemed to work for me.