Mumbai GT Mumbai GT - 1 year ago 55
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 page

package com.jwt.servlet;

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 {

PrintWriter out = response.getWriter();

String n = request.getParameter("userName");
String p = request.getParameter("password");
String e = request.getParameter("email");
String c = request.getParameter("language");

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) {



Here is the register.jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
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;
<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>
document.getElementById("tt").innerHTML = Date();
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">
</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>

This is the web.xml page

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" version="3.0">



Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download