Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet problem from registering a data in database with servlet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


DARE
problem from registering a data in database with servlet
3 Answer(s)      5 years and 5 months ago
Posted in : JSP-Servlet



Advertisement
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
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
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
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
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. Please read at http
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
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 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
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 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
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
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
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
how to fetch data from servlet ????
how to fetch data from servlet ????  how to fetch data from servlet
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
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
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
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
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 .   
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
 

 

 

DMCA.com