Hello Everyone,
Happy New Year.
I am a beginner in
jsp . I have written code for 3 jsp pages and I have used mysql database. My aim was to give login page on first page, then to give a form on 2nd page for submitting the details of a person. My first page is working, as I have added the login of only one person, but my problem is, my second page is not working. I would be thankful if someone can correct my code.I am pasting the code below. I want that when the user click on submit button on second page the values filled by him should be stored in the database.
PAGE 1 code:
<html>
<body>
<head><title> My Website </title></head>
<body>
<h1> Welcome to my homepage </h1>
<table border = "1" >
<tr >
<td>
<form method = "post" action = "regform.jsp">
<pre>
username: <input type = "text" name = "user" >
password: <input type = "password" name = "pass" >
<input type = "submit" value = "login">
</pre>
</form>
</td>
</tr>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2nd Page code:
<%@ page import = "java.sql.*" %>
<%@ page import = "java.net.*" %>
<html>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "germany";
String url = "
jdbc:mysql://localhost:3306/example?" + "user=" + username + "&password=" + password;
String s = request.getParameter("fname");
String a = request.getParameter("lname");
//String g = request.getParameter("gender");
String b = request.getParameter("country");
String c = request.getParameter("address");
String e = request.getParameter("zip");
String g = request.getParameter("gender");
try
{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
Statement stmt = conn.createStatement();
//String query = "insert into registration1 values('"+s+"', '"+g+"')";
String query = "insert into registration2 values('"+s+"', '"+a+"','"+b+"', '"+c+"','"+e+"', '"+g+"')";
int i = stmt.executeUpdate(query);
if(i > 0)
out.println("Data entered succesfully");
else
out.println("some issue");
conn.close();
}
catch(ClassNotFoundException ex)
{
System.out.println("exception occured:" + ex);
}
catch (SQLException e) {
e.printStackTrace();
}
%>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PAGE 3 code:
<html>
<head><title> Registration Page </title> </head>
<body>
<h1 align = "center"> Registration </h1>
<form method = "post" action = "registration.jsp">
<pre>
First Name : <input type = "text" name ="fname">
Last Name : <input type = "text" name ="lname">
Country : <input type = "text" name ="country">
Address : <input type = "text" name ="address">
ZIP : <input type = "text" name ="zip">
Gender:
<input type = "radio" name ="gen" value = "male"> Male
<input type = "radio" name ="gen" value = "female"> Female
<input type = "submit" value = "register">
</pre>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MySql database query code:
mysql> create table registration2
-> (
-> fname varchar(20),
-> lname varchar(20),
-> gender varchar(20),
-> country varchar(20),
-> address varchar(30),
-> zip varchar(20)
-> );
Query OK, 0 rows affected (0.16 sec)