problem from registering a data in database with servlet

problem from registering a data in database with servlet

View Answers

July 3, 2009 at 12:42 PM

Hi Friend,

Try the following code:

1)addUser.jsp:

<html>
<head>
</head>
<body>
<form name="userform" method="post" action="../AddUser">
<br><br><br>
<table align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;">
<tr><td colspan=2 style="font-weight:bold;" align="center">Add User</td></tr>
<tr><td colspan=2 align="center" height="10px"></td></tr>
<tr>
<td style="font-weight:bold;">First Name</td>
<td><input type="text" name="first_name" value=""></td>
</tr>
<tr>
<td style="font-weight:bold;">Last Name</td>
<td><input type="text" name="last_name" value=""></td>
</tr>
<tr>
<td style="font-weight:bold;">UserName</td>
<td><input type="text" name="username" value=""></td>
</tr>
<tr>
<td style="font-weight:bold;">Password</td>
<td><input type="password" name="password" value=""></td>
</tr>

<tr>
<td style="font-weight:bold;">City</td>
<td><input type="text" name="city" rows=5 cols=25></td>
</tr>
<tr>
<td style="font-weight:bold;">State</td>
<td><input type="text" name="state" value=""></td>
</tr>
<tr>
<td style="font-weight:bold;">Country</td>
<td><input type="text" name="country" value=""></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="Submit" value="Save" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<tr><td colspan=2 align="center" height="10px"></td></tr>
</table>
</form>


</body>
</html>

July 3, 2009 at 12:43 PM

continue.............

2)Servlet 'AddUser.java':

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;

public class AddUser extends HttpServlet{

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";;
String dbName = "register";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";


String first_name = request.getParameter("first_name").toString();
String lastName = request.getParameter("last_name").toString();
String username = request.getParameter("username").toString();
String pass = request.getParameter("password").toString();
String city = request.getParameter("city").toString();
String state = request.getParameter("state").toString();
String country = request.getParameter("country").toString();


Statement stmt1,stmt2;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");

String query1="Select * from user";
stmt1 = conn.createStatement();
ResultSet rs=stmt1.executeQuery(query1);
String value1="",value2="",value3="",value4="",value5="",value6="",value7="";
while(rs.next()){
value1=rs.getString("first_name");
value2=rs.getString("last_name");
value3=rs.getString("username");
value4=rs.getString("password");
value5=rs.getString("city");
value6=rs.getString("state");
value7=rs.getString("country");
}
if((value1.equals(first_name))&&(value2.equals(lastName))&&(value3.equals(username))&&(value4.equals(pass))&&(value5.equals(city))&&(value6.equals(state))&&(value7.equals(country))){
out.println("You have already registered");
}
else{

String query2 = "insert into user set first_name='"+first_name+"',last_name='"+lastName+"',username='"+username+"',password='"+pass+"',city='"+city+"',state='"+state+"',country='"+country+"'";
stmt2 = conn.createStatement();
int i = stmt2.executeUpdate(query2);
out.println("You have registered successfully");
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

July 3, 2009 at 12:45 PM

continue...

3)In web.xml,do the servlet mapping:

<servlet>
<servlet-name>AddUser</servlet-name>
<servlet-class>AddUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddUser</servlet-name>
<url-pattern>/AddUser</url-pattern>
</servlet-mapping>

Hope that the above code will help you.

Thanks









Related Tutorials/Questions & Answers:
problem from registering a data in database with servlet - JSP-Servlet
problem from registering a data in database with servlet  Hi Rose, i created a servlet to validates ten user details in the database, if present...); System.out.println("Connected to the database"); String query1="Select * from user
Problem in accessing data from Database
Problem in accessing data from Database  hi..... i'm making a project on servlet and jsp with ms access 2007 at the backend. One field in my database... in currency column and then try to retrieve data using my servlet coding
Advertisements
data are not display in JSP from database - JSP-Servlet
data are not display in JSP from database   i want to finding some data through a SQL query from SQL server database to a JSP page based on some.... one.jsp value of 'school' request parameter are comming from the link
How to get the data from the database using Servlet or JSP program
the problem   Get data from database using servlet Retrieve data from...How to get the data from the database using Servlet or JSP program  hello My Netizen friend has given the answer to retrieve data from the database
Problem insert data into database using servlet n javabean - Java Beginners
Problem insert data into database using servlet n javabean  I created a servlet for registration proses I got a problem compiling my servlet... to solve it.. i really new in jsp+servlet+javabean,dont really unnderstand 1)my
inserting data from radio buttons to database-ERROR - JSP-Servlet
inserting data from radio buttons to database-ERROR  hi, i have... for this problem. thanks and regards nishi kishore  Hi Friend, The whole problem is because of 'option' field in database.It seems that 'option
use data from database table as hyperlink value - JSP-Servlet
use data from database table as hyperlink value  I'm creating a web page that allows user to search my database. The page will display partial results of my database, then the first column of the resultset was converted
how to store the data in a array retrived from the database - JSP-Servlet
how to store the data in a array retrived from the database  hi... in database,and i need to disply all the employees record in a single jsp. pls can any... I am sending a link, you please try to solve your problem by follow this link
inserting data from radio buttons to database - JSP-Servlet
inserting data from radio buttons to database  hi, my problem... button) now i have a database as professor and a table in this database... need a solution of this problem thanks and regards Nishi Kishore  
inserting data from radio buttons to database - JSP-Servlet
inserting data from radio buttons to database  hi, i am getting error in my code of feedback form. once i finished selecting all the radio buttons...'' at line 1 please provide me the solution of this problem. thanks
Acees data from database using combo box - JSP-Servlet
Acees data from database using combo box  please let me how i access the data from database when i select combo box combo2 having values Arts...("show.jsp?class_name="+cla+"&&from_year="+y1+"&&to_year="+y2+"&&school="+sch
selecting data from database
selecting data from database  how to select data from database using Dao in struts   Hi, You have to load the data from database using hibernate and send the data in a Java file object to jsp page.ADS_TO_REPLACE_1
How to get the data from the database (Oracle) in console or in ie using servlet or jsp as Front end
How to get the data from the database (Oracle) in console or in ie using servlet or jsp as Front end  hello i have a simple problem in jsp in the sense to get data from the database like oracle . I have created one jsp
problem in setting the values from database
problem in setting the values from database  hello friends, can anyone help me?? I am facing this problem for past one week. I could't set the values from database. here is the code: private JTextField getJTextField1
retrive data from database
retrive data from database   hi.. i made a application form. it's have attribute s.no,name,roll no and i enter a few records. now i want to view all record not in database access sheet i want to view it at any another
retrive data from database?
retrive data from database?  hellow i have a database sheet name as db1. it's contain sixty(60) sn,name ,rollno(primary key),father's name etc... from db1 and enter value in text box according there name,rollno and save
how to fetch data from servlet ????
how to fetch data from servlet ????  how to fetch data from servlet
fetch data from database in javascript
fetch data from database in javascript   How to fetch data from database in JavaScript when it is a leap year
how to retrieve data from database ?????/
how to retrieve data from database ?????/  how to retrieve data from database
how to retrieve data from database ?????/
how to retrieve data from database ?????/  how to retrieve data from database
how to retrieve data from database ?????/
how to retrieve data from database ?????/  how to retrieve data from database
how to retrieve data from database ?????/
how to retrieve data from database ?????/  how to retrieve data from database
Inserting data on a database in servlet - JSP-Servlet
Inserting data on a database in servlet  Hi I am following... to a databse by a servlet.I used the example in "Inserting Data In Database table... of a table,that is none of the data which i tried to enter into database by using
Getting data from servlet into javascript
Getting data from servlet into javascript  How do i get json data from my servlet on to a variable in javascript n bind the data to display onto sigma grid.Has anyone Idea how to do
how to retrieve data from database using combobox value without using request.getParameter in jsp - JSP-Servlet
how to retrieve data from database using combobox value without using request.getParameter in jsp  Answer pl
Sending form data from HTML page to SQLserver 2005 database by calling servlet code
Sending form data from HTML page to SQLserver 2005 database by calling servlet code  Hi sir, I would like to know how to send the form data from html page to database by calling servlet code from html page .   
how to retrieve data from database
how to retrieve data from database  unable to retrieve data from database using mySQL by using jsp sessions and beans for editing
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
How to delete the row from the Database by using servlet
then the user data to be delete from the database table. Assume in Database table have...: Delete row from database using servlet   In that link solution...How to delete the row from the Database by using servlet  Dear Sir
delete data from database - SQL
delete data from database  HOw to delete specific data from table> I want to delete one record from database. Thnx  Hi friend, -------------------------------------------- Visit for more information
To retrive data from database - Struts
To retrive data from database  How to get values ,when i select a select box in jsp and has to get values in textbox automatically from database? eg... come to jsp page automatically from database
how to display a table from database using servlet
how to display a table from database using servlet  how to display... the following link:ADS_TO_REPLACE_1 http://roseindia.net/jsp/servlet-jsp-data... the following link: http://roseindia.net/jsp/servlet-jsp-data-list.shtml ThanksADS
How to retrieve image from database in Servlet?
How to retrieve image from database in Servlet?  Hi, How to retrieve image from database in Servlet? Thanks   Hi, Please check the tutorial Retrieve image from database using Servlet. Thanks
How retreive data from database without using post method in jsp - JSP-Servlet
How retreive data from database without using post method in jsp  Tell me how?  Hi Friend, If you don't want to use post method then use... the selected value from the combo box and get textbox value from another page
Servlet to add the data into database
Servlet to add the data into database   ... the data in the database table from a html form.  This servlet program...; <description> Servlet to add the data into database <
retrieve data from database with hyperlink
retrieve data from database with hyperlink  sir....i have one table called 'name' having two values i have to retrieve those data with a hyperlink and show in browser and if we click hyperlink its shows his sub categories....how
how i conditional access the data from database using combo box. - JSP-Servlet
how i conditional access the data from database using combo box.   i... access the data from database when i select class 11 0r 12. Here is JSP file...="+cla+"&&from_year="+y1+"&&to_year="+y2+"&&school="+sch); alert(sch+" "+cla+ " "+y1
problem in selecting second arraylist from mysql database
problem in selecting second arraylist from mysql database  Hi... in mysql database. In the below program , i have hard-coded that second list, but i want second list to be dynamic from database. Please, help me out. Thank
How to retrieve data from database by using combo box value in jsp? - JSP-Servlet
How to retrieve data from database by using combo box value in jsp?  Hello friend i want use only one jsp page.. but u give 2 pages... I want to work in one page... Pl response.. I do not need getParameter... Pl respose me
retrieve data from mysql database
retrieve data from mysql database  hi am not familiar in php.....even... selected value on combobox which is to be retrieve the relevant data from mysql database using php.... below my code is that.. <html> <head>
how to display data from database in jsp
how to display data from database in jsp  how to display data from database in jsp
retreive integer data from database
retreive integer data from database  i made a table named result...=lalit&database=mydb"); String url="select marks from student where rollno... and two buttons "ok" and "cancel" i want to take input from textfield
jfreechart display from access database data.
jfreechart display from access database data.  I have made a database... to retrieve the data from the access database using prepared statement and then display... is to be done in a servlet.. Note that it is a access made database. How can I
Getting Textbox data from database
data from database and display it on the textboxes. <%@page language="java...Getting Textbox data from database  When i m trying to get data in textbox as readonly from database i m getting following error.and my code is shown
fetch record from oracle database using jsp-servlet?
fetch record from oracle database using jsp-servlet?  how can i fetch data from oracle database by using jsp-servlet. i'm using eclipse, tomcat server and oracle database and creating jsp pages and also using servlet
Getting Textbox data from database
;Here is a jsp code that retrieves data from database and display...Getting Textbox data from database  When i m trying to get data in textbox as readonly from database i m getting following error.and my code is shown
Data retrieve from mysql database
Data retrieve from mysql database  Hi sir, please give some example of jsp code for retrieving mysql database values in multiple dropdown list... from the dropdown, related data will get displayed on the textboxes. Here we have
retriving image from database - JSP-Servlet
retriving image from database  how to retrive image from mysql database by using servlet programming  Hi Friend, Please visit the following link: http://www.roseindia.net/servlets/retreiveimage.shtml Thanks
GWT -- retrive the data from Database
GWT -- retrive the data from Database  the user can create an event. In the create page, we have three buttons. Save as draft, preview and save... button, you have to create a dynamic jsp which should read the contents from
problem in servlet program
problem in servlet program  Dear Sir, I have a problem to insert the Blob type data like as video file, audio file in the database using Servlet and html code

Ads