Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2005, 05:01 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>


Reply With Quote
  #2 (permalink)  
Old March 30th, 2005, 07:21 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , India.
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr
Default

You read about the JSP & Servlet
http://java.sun.com/webservices/docs...ets.html#69957

Gokulan Ethiraj
Reply With Quote
  #3 (permalink)  
Old March 31st, 2005, 05:57 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();



Reply With Quote
  #4 (permalink)  
Old March 2nd, 2014, 12:09 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy 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);
}


%>
Reply With Quote
  #5 (permalink)  
Old January 15th, 2015, 11:35 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question 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>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with inserting data into access database ITladybug ADO.NET 1 January 1st, 2006 01:50 PM
Inserting data into Access database OldCoder ASP.NET 2.0 Basics 1 December 31st, 2005 06:26 PM
inserting data into Sql Database CodeMonkeys C# 6 August 31st, 2004 04:21 PM
failure in inserting data into database ckmun81 Access ASP 3 December 21st, 2003 08:09 PM



All times are GMT -4. The time now is 02:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.