 |
| JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 23rd, 2005, 05:01 PM
|
|
Registered User
|
|
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
inserting data into database using a jsp
Hello all
I am trying to insert data into a mysql table using a jsp. I have a simple page called newuser.jsp (shown below), which collects data from the user. It calls the page validate_new_user.jsp also shown below. Now it complies fine, when I enter data into the form and hit submit I am taken to the success.htm page. But when I go into my database and use the command âselect * from user_detailsâ the data I entered in from the newuser.jsp page is not there. I am using tomcat 5.5. Please help as this is really wrecking my head, it may be something simple that I have left out. Thanks in advance.
Here is the code for newuser.jsp
<html>
<head>
<title>new user</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Please enter your details</h1>
<form action="validate_new_user.jsp" method="post">
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Name:</td>
<td><input type="text" name="NewName"></td>
</tr>
<tr>
<td align="right">Email Address:</td>
<td><input type="text" name="EmailAddress"></td>
</tr>
<tr>
<td align="right">Nationality:</td>
<td><input type="text" name="Nationality"></td>
</tr>
<tr>
<td align="right">User Name:</td>
<td><input type="text" name="UserName"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="Password"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here is the code for validate_new_user.jsp
<html>
<head>
<title>Validate New User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>
<%
Connection con = null;
String names = request.getParameter("NewName");
String eaddress = request.getParameter("EmailAddress");
String nation = request.getParameter("Nationality");
String userid = request.getParameter("UserName");
String pw = request.getParameter("Password");
String queryText = "insert into user_details (\"fullName\", \"emailAddress\",
\"nationality\", \"username\", \"password\")
values('"+names+"','"+eaddress+"','"+nation+"','"+ userid+"','"+pw+"')";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance ();
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","password");
Statement stat = con.createStatement();
ResultSet rst = stat.executeQuery(queryText);
rst.close();
stat.close();
con.close();
} catch (Exception e) { }
response.sendRedirect("success.htm");
%>
</body>
</html>
|
|

March 30th, 2005, 07:21 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
|
|
|
|

March 31st, 2005, 05:57 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You need to use executeUpdate, NOT executeQuery
st.executeUpdate();
st.clearParameters(); // makes sure your clear for next time!!
The rest of your code/sql looks a bit odd. I use prepared statement like this:
PreparedStatement st;
st = conn.prepareStatement("INSERT INTO gone (bland, missing, user, company, test, hide, stuff, junk, info) VALUES (?,?,?,?,?,?,?,?,?)");
st.setString(1,variable);
st.setString(2,var2);
st.setString(3,"string");
st.setString(4,company);
st.setString(5,"string2");
st.setInt(6,variable3);
st.setString(7,"str3");
st.setString(8,"str4");
st.setString(9,"str5");
st.executeUpdate();
st.clearParameters();
|
|

March 2nd, 2014, 12:09 AM
|
|
Registered User
|
|
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
angrycat i tried a lot its not working
i'm trying to insert values from html form to mysql table using jsp. i'm tring it since 3days it's not working. i'm getting to a blank white. i'm new to jsp. can anybody provide complete program plssss... thanks in advance. im posting my code here
form.htm:
<html>
<head>
<title>new user</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Please enter your details</h1>
<form action="reg.jsp" method="post">
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">userid:</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td align="right">password:</td>
<td><input type="text" name="pwd"></td>
</tr>
<tr>
<td align="right">firstname:</td>
<td><input type="text" name="fname"></td>
</tr>
<tr>
<td align="right">lastname:</td>
<td><input type="text" name="lname"></td>
</tr>
<tr>
<td align="right">email:</td>
<td><input type="password" name="email"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
reg.jsp:
<%@ page import ="java.sql.*" %>
<%@ page import ="java.sql.Connection" %>
<%@ page import ="java.sql.DriverManager" %>
<%@ page import ="java.sql.PreparedStatement" %>
<%
String Database="jdbc:mysql://localhost:3306/test";
String UserName="root";
String Password="1234";
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
ResultSet rs = null;
Statement st = null;
PreparedStatement pstmt = null;
try
{
String query = "insert into users(user,pwd,fname,lname,email) values(?, ?, ? , ? )";
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setString(1, "user"); // set input parameter 1
pstmt.setString(2, "pwd"); // set input parameter 2
pstmt.setString(3,"fname");
pstmt.setString(4, "lname");// set input parameter 3
pstmt.setString(5, "email");
pstmt.executeUpdate();
int i = pstmt.executeUpdate();
if(i!=0)
{
System.out.println("Record has been inserted");
}
else
{
System.out.println("failed to insert the data");
}
}
catch (Exception e)
{
System.out.println(e);
}
%>
|
|

January 15th, 2015, 11:35 AM
|
|
Registered User
|
|
Join Date: Jan 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
jsp programs with db
Hi please help me in solving some basic problems in jsp.
I am new to jsp & studying through correspondance so no lab experience .I have tried learning from tutorials yet in problem
I know jsp prog. to display contents from database but how to handle questions like :
to display name after accepting id as input?
I tried following but not working.pleeese help.
my table name is tab which has foll fields
id,name,city,state,country
Code:
<%@ page language="java" contentType="text/html" import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<html>
<head>
<title>db table</title>
</head>
<body>
<%
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection dbcon=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","root","root");
Statement stmt=dbcon.createStatement();
ResultSet rs=stmt.executeQuery("select name from tab where id=?");
int id;
String name="xxxx";
String city="y";
String state="x";
String country="xx";
%>
<center><table border=1 width=100%>
<tr bgcolor="orange"><th>id</th><th>Name</th><th>city</th><th>state</th><th>country</th></tr>
<% while(rs.next())
{
id=rs.getInt("id");name=rs.getString("name");city=rs.getString("city");state=rs.getString("state");country=rs.getString("country"); %>
<tr> <td> <%= id %> </td><td> <%= name %> </td><td> <%= city %> </td><td> <%= state %> </td><td> <%= country %> </td> </tr>
<% } %>
</table></center>
<% rs.close();
stmt.close();
dbcon.close(); %>
</body>
</html>
|
|
 |