This document discusses connecting a Java servlet application to a MySQL database. It includes code for a registration servlet that takes user input from a registration form, connects to the database, and inserts the user details. It also includes code for a login servlet that authenticates user credentials from the database and redirects to a success page if valid.
5. 5
String n = request.getParameter("userName");
String p = request.getParameter("password");
String e = request.getParameter("email");
String c = request.getParameter("language");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/servlet", "root", "mukesh");
PreparedStatement ps = con
.prepareStatement("insert into USERDETAILS values(?,?,?,?)");
ps.setString(1, n);
ps.setString(2, p);
ps.setString(3, e);
ps.setString(4, c);
RegisterServlet.java
6. 6
int i = ps.executeUpdate(); // returns the binary value 0/1
if (i > 0)
out.print("You are successfully registered...");
} catch (Exception e2) {
System.out.println(e2);
}
out.close();
}
}
RegisterServlet.java