This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes user registration via Servlet/JSP/JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "user registration via Servlet/JSP/JDBC" Watch "user registration via Servlet/JSP/JDBC" New topic
Author

user registration via Servlet/JSP/JDBC

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Hi, I have created a user registration JSP page accepting username, password & address. On Clicking submit flow navigates to the servlet.

Servlet code




Guys I want if the table update is successful, user is navigated to a new jsp page with this message " Welcome 'Username'". Please help me out, how do I do that ??? THANKS
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

Hey Shivang,

first of all use executeUpdate instead of executeQuery.
executeUpdate returns no. of rows affected by that query in DB. So you can check if it is 1 and navigate accordingly.
Hope your query is resolved.
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Thanks for the reply Aniket I've edited the code




But this doesn't seem to work. After servlet processing the flow is not moving to the page.

I created a table in the Oracle 10g database

* Table in the database ' users ' with user details
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10

CREATE TABLE users (userid int NOT NULL PRIMARY KEY, username varchar(10), password varchar(10),
address blob, image blob);

insert into users (userid) values (seq_person.nextval);


On checking in the database by Select * from users. I am getting the error. "ORA-00932: inconsistent datatypes: expected NUMBER got BLOB". Can you figure out what the issue is ??
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

String address = request.getParameter("address");


String queryString="insert into users ( USERNAME, PASSWORD, ADDRESS) values (" + "'" + username + "'" + ","
+ "'" + passw + "'" + "," + "'" + address + "'" + ");";

CREATE TABLE users (userid int NOT NULL PRIMARY KEY, username varchar(10), password varchar(10),
address blob, image blob);

change the datatype of address field in table to varchar
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I changed the datatype to varchar, removed the image BLOB column. Now there is no error but it's still not working . The table in the database is not getting updated.

Is my query OK ??

String queryString="insert into users ( USERNAME, PASSWORD, ADDRESS) values (" + "'" + username + "'" + ","
+ "'" + passw + "'" + "," + "'" + address + "'" + ");";
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

shivang sarawagi wrote:I changed the datatype to varchar, removed the image BLOB column. Now there is no error but it's still not working . The table in the database is not getting updated.

Is my query OK ??

String queryString="insert into users ( USERNAME, PASSWORD, ADDRESS) values (" + "'" + username + "'" + ","
+ "'" + passw + "'" + "," + "'" + address + "'" + ");";


actually no... query should be:


"insert into users ( USERNAME, PASSWORD, ADDRESS) values ('"+username+"','"+passw+"','"address"')"

sql requires strings in single quots...
navalk joshi
Greenhorn

Joined: May 02, 2012
Posts: 12
hey shivang you can use the alternate way........i.e.


sql="insert into Registration values(?,?,?)";

PreparedStatement pst=con.prepareStatement(sql);


pst.setString(1,username);
pst.setString(2,passw);
pst.setString(3,address);
int x=pst.executeUpdate();

see if this works
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
How do I check that the connection url used by me is correct ???
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

Shivang change your connection url to
jdbcracle:thin:@localhost:1521:XE
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Thanks for the replies ....

First things first even if the db connection is not working the servlet navigation is not working fine. On clicking submit on jsp the control moves to the servlet & then there is just a blank page. Can you figure out any bug in the code ??

This is my latest code




Soumya Ranjan Padhi
Greenhorn

Joined: Jun 27, 2012
Posts: 10
you are doing good job but few modifications needed....
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I have done some changes (RequestDispatcher & redirect kept out of the try/catch block ) the navigation is now working fine. Its just the connection with the database which is troubling

Well this is the url which the database in my browser is displaying "http://127.0.0.1:8081/apex/f?p=4500:1003:996174241691289::NO:::"

do I have to change my connection url given in the code ???
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

shivang sarawagi wrote:I have done some changes (RequestDispatcher & redirect kept out of the try/catch block ) the navigation is now working fine. Its just the connection with the database which is troubling


what is the problem with DB connection?
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
There is no db connection error its just nothing is happening. The database is not getting updated by the query.
I implemented the query given by you but that is giving a syntax error.


This is the url which the database in my browser is displaying "http://127.0.0.1:8081/apex/f?p=4500:1003:996174241691289::NO:::"

do I need to change my connection url given in the code ??? Also Is there any jdbc jar file or something which I should download, not that I know of ??
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

shivang sarawagi wrote:There is no db connection error its just nothing is happening. The database is not getting updated by the query.
I implemented the query given by you but that is giving a syntax error.


This is the url which the database in my browser is displaying "http://127.0.0.1:8081/apex/f?p=4500:1003:996174241691289::NO:::"

do I need to change my connection url given in the code ??? Also Is there any jdbc jar file or something which I should download, not that I know of ??


yes, you have to add a jar to your build path
go to oracle's home dir and go app/oracle/product/10.2.0/server/jdbc
select the one not ending with _14(I forgot the name) and add it to build path of project, I guess you will be required to add it in lib folder as well
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Hi Aniket, I've added the ojdbc14.jar file in the lib folder of the project.

I right clicked on the project name in netbeans navigated to properties, ojdbc14.jar is present in the compile time libraries path. Will you please tell me how to add the jar in the build
path ???

Thanks
navalk joshi
Greenhorn

Joined: May 02, 2012
Posts: 12
well shivang you can try out the code below after adding jar to the library of your project......and there's usually no need to add jar to the build in Netbeans......
one more thing......modify the indexes in pst.setString according to your database....

hope this will do the needfull.................
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class SSignUP extends HttpServlet {



protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");

PrintWriter out=response.getWriter();

String name=request.getParameter("usern");
String pass=request.getParameter("pass");
String address=request.getParameter("address");
Connection con=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

try
{
con=DriverManager.getConnection("jdbcracle:thin:@localhost:1521:xe", "SYSTEM", "system");

String sql=null;
sql="insert into USERS values(?,?,?)";
PreparedStatement pst=con.prepareStatement(sql);


pst.setString(1,name);
pst.setString(2,pass);
pst.setString(3,address);
int x=pst.executeUpdate();
System.out.println("row Successfully inserted");
pst.close();
if(x>0)
{
RequestDispatcher rd=request.getRequestDispatcher("UserHomePage.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("SignUp.jsp");
rd.forward(request, response);
}
}catch(SQLException e)
{
System.out.println(e);
}
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Thanks a lot Navalk & Aniket for your help. The database is finally getting updated. The connection is working fine. I executed a basic query & it worked but the multiple insert values are not working.

Navalk can you please tell what might be the issue.

My code is

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I fixed the issue, now everything is working Thanks
navalk joshi
Greenhorn

Joined: May 02, 2012
Posts: 12
hey shivang......the code looks fine.....then where was the problem
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I had used auto increment in the table which was creating the problem. There was a column mismatch while updating the data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: user registration via Servlet/JSP/JDBC