This servlet is designed to add a new user entry into a database table.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String dob = request.getParameter("dob");
String gen = request.getParameter("gen");
String mem_type = request.getParameter("mem");
String fname = request.getParameter("fname");
String addr = request.getParameter("addr");
long number = Long.parseLong(request.getParameter("c_no"));
String loginID = request.getParameter("loginID");
String username = request.getParameter("username");
String password = request.getParameter("password");
java.sql.Date sqlDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
try {
Date dobb = sdf.parse(dob);
sqlDate = new java.sql.Date(dobb.getDate());
}
catch(ParseException e) {
e.printStackTrace();
}
String name = firstname+" "+lastname;
Connection conn = null;
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/library", "root", "asdfghjkl");
PreparedStatement updatequery = (PreparedStatement)conn.prepareStatement("INSERT INTO librarian(Librarian Name,Gender,Address,Date Of Birth,Login ID,Contact Number) VALUES (?,?,?,?,?,?)");
updatequery.setString(1, name);
updatequery.setString(2, gen);
updatequery.setString(3, addr);
updatequery.setDate(4, sqlDate);
updatequery.setString(5, loginID);
updatequery.setLong(6, number);
int i = updatequery.executeUpdate();
out.println(i+"Inserted.");
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
I've implemented the above code in an attempt to insert a new record into a table, but it's not functioning as expected. Any assistance in identifying errors within my program would be greatly appreciated.
The following JSP code snippet is provided :
<html>
<head>
<style>
#d1{
position:relative;
bottom:0
}
</style>
</head>
<jsp:include page="header.html"/>
<body>
<div>
<form class="page" method="post" action="RegisterPage">
First Name : <input name="firstname">
<br><br>
Last Name : <input name="lastname">
<br><br>
Date of Birth : <input name="dob" type="date">
<br><br>
Gender :
<input type="radio" name="gen" value="Male">Male
<input type="radio" name="gen" value="Female">Female
<br><br>
Membership Type :
<input type="radio" name="mem" value="Librarian">Librarian
<input type="radio" name="mem" value="Member">Member
<br><br>
Father's Name : <input name="fname">
<br><br>
Address <input name="addr">
<br><br>
Contact Number : <input name="c_no" type="text">
<br><br>
Email ID : <input name="loginID" type="email">
<br><br>
Username : <input name="username">
<br><br>
Password : <input name="password" type="password">
<br><br>
<input type="submit" value="Register">
</form>
</div>
</body>
<div id="d1">
<jsp:include page="footer.html"/>
</div>
</html>
The table consists of 7 columns, with the first being an auto-increment primary key and the rest comprising standard columns for Name, Gender, Address, Date of Birth, Login ID, and Contact Number.
An error message is displayed in the console: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'login table(Login ID,Username,Password) VALUES ('[email protected]','asdfghjkl','asdfg' at line 1