java servlets with database interaction

java servlets with database interaction

hai friends i am doing a web application in that i have a registration page, after successfully registered it has to display a message like "you have successfully registered" and those values should be saved in to the database.but i am getting an exception like "not enough values" can you please assist how to get rid of this problem what problem i have done if you want i can send sample code.

mycode

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class RNewReg extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { PrintWriter out=res.getWriter(); res.setContentType("text/html"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection c=DriverManager.getConnection("jdbc:odbc:online","system","manager"); Statement st=c.createStatement();

           String user=req.getParameter("uid");
           String passwd=req.getParameter("pw");
           String repass=req.getParameter("rpw");  
           String fname=req.getParameter("fname");
           String mname=req.getParameter("mnane");
           String lname=req.getParameter("lname");                                 
           int mobile=Integer.parseInt(req.getParameter("mobile"));
           int land=Integer.parseInt(req.getParameter("land"));
           String email=req.getParameter("email");             
           String empid=req.getParameter("empid");
           String compemail=req.getParameter("compemail");

           ResultSet rs=st.executeQuery("select userid from RUsersInfo");
           boolean exist=false;
           while(rs.next())
           {
                    if(rs.getString(1).equals(user))
                           exist=true; 
           }
           out.println("<html>");
           if(exist==false)
           {
           int i=st.executeUpdate("insert into rusersInfo values('" + user +"','" + passwd + "','"+repass+"," + fname + "','" + mname + "','" + lname + "','" + mobile + "','"+ land+"','" + email+"','"+ empid + "','" + compemail + "')");

           out.println("<body bgcolor=\"lightyellow\">");
           out.println("<form action=\"./regusers.html\">");
           out.println("<font size=4 color=blue>");
           out.println(user + " is Created");
           out.println("<br><input type=submit name=b2 value=Next>");
           out.println("</font></form></body>");
           }
           else
           {
                    out.println("<body bgcolor=lightyellow><font size=4 color=red> Sorry!. An User with " + user +
                    " is already existing.Please Choose another login ID.");
                    out.println("<br><a href=\"newusers.html\">Back..</a>");
                    out.println("</font></body>");                        
           }
           out.println("</html>");
           st.close();
           c.close();
      }
      catch(Exception e)
      {

         out.println(e.toString());
      }
 }
 public void doPost(HttpServletRequest req,HttpServletResponse res)
 throws ServletException,IOException
 {
    doGet(req,res);
 }

}

View Answers

February 8, 2011 at 11:42 AM

Connect Servlet with database and insert data

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

public class InsertServlet extends HttpServlet { 
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String name=req.getParameter("name");
        String pass=req.getParameter("pass");
        String address=req.getParameter("address");
int no=Integer.parseInt(req.getParameter("no"));
String gender=req.getParameter("gender");
String country=req.getParameter("country");
try{
Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into mail(name,password,address,telno,gender,country) values('"+name+"','"+pass+"','"+address+"',"+no+",'"+gender+"','"+country+"')");
         out.println("Data is inserted successfully");
         con.close();
        }
catch(Exception e){
    System.out.println(e);
}
            }
        }

For more information, visit the following link:

Servlet Tutorials









Related Tutorials/Questions & Answers:
java servlets with database interaction
java servlets with database interaction  hai friends i am doing a web application in that i have a registration page, after successfully registered... Servlet with database and insert data import java.io.*; import
RFID Interaction with Database - MobileApplications
RFID Interaction with Database  Can anyone help me with connecting a rfid reader with a database such that any one who comes in the range of my RFID reader its value gets in the database or it checks with tha database
Advertisements
database and servlets
database and servlets  how can get the questions from a database and use it as questions for a form. and present one question per page. its for a tomcat server
java servlets
java servlets  please help... how to connect java servlets with mysql i am using apache tomcat 5.5
Accessing Database from servlets through JDBC!
Java Servlets - Downloading and Installation         Java Servlets are server... and reusability issues make it less than optimal solutions. Java Servlets changes
Accessing Database from servlets through JDBC!
; This article shows you how to access database from servlets...;Java Web Server. For the sake of simplicity I have used Microsoft Access Database as backend and Sun's JDBC-ODBC bridge to connect to access
Java Servlets
Java Servlets  If the binary data is posted by both doGet and doPost then which one is efficient?Please give me some example by using both doGet and doPost. Thanks in Advance   The doPost method is more efficient
servlets
package supports the development of servlets that use the HTTP protocol. The classes... javax.servlet.GenericServlet and serves as the base class for HTTP servlets. HttpServlet-Request and HttpServletResponse allow additional interaction with the client. Finally, since
servlets - Java Beginners
servlets   Q: i write program to upload image into perticular folder.but i want to store that path into database but not image and display that image in page from the db.pls any one send me code for that in servlets asap.pls
servlets - Java Beginners
to respond to HTTP requests. A JSP layered on top of Java Servlets. Whereas...servlets  what is the difference b/w servlets and JSP, what servlets..., The Java Servlet is the fundamental component to using Java for web
java Servlets - JSP-Servlet
java Servlets  Hi i am having a doubt regarding servlets as i am in learning stage give me any clew how to retrive data from mysql database after... the database. out.println(""); out.println(""); out.println
Using MYSQL Database with JSP & Servlets.
Using MYSQL Database with JSP & Servlets.  ... database. We will use tomcat web server to run over web application which acceres the MYSQL database. Here I am using MYSQL & tomcat server
Help needed on java standalone app and default browser interaction.
Help needed on java standalone app and default browser interaction.  Hi Developers and tutors, Is anyone able to guide on how am i able to extract the url thats displaying on my default browser to my java standalone application
java servlets - Java Beginners
java servlets  hai my question is on xml documentation in creation of a java servlets. whether we have to create for each and every servelet we have to create each xml documentation?   Hi friends Exactly what you
Servlets
Servlets  Java Servlet technology  You have set.... Anyways, please visit the following links: http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml http://www.roseindia.net/servlets
servlets - Java Beginners
servlets   I am doing small project in java on servlets. I want to generate reports on webpage ,how is it possible and what is the difference between dynamic pages & reports ? tell me very urgent pls,pls
servlets
servlets  hi i am using servlets i have a problem in doing an application. in my application i have html form, in which i have to insert on date... into java.sql.Date type and insert into database
servlets
servlets  why we are using servlets
servlets
servlets  what is the duties of response object in servlets
servlets
what are advantages of servlets  what are advantages of servlets   Please visit the following link: Advantages Of Servlets
Servlets - Java Beginners
Servlets  Hi, How can i run the servlet programon my system? I wrote the code in java.(I've taken the Helloworld program as example program... for more information, http://www.roseindia.net/servlets/ Thanks Amardeep
java servlets - JDBC
java servlets  First thank you sir for your reply sir i am using "Oracle Database" it asking for 1.User Name 2.Password 3.Host String if we... that situation how i am connect to the "Oracle Database"  when u r installing
servlets - Java Interview Questions
html and processing in servlets and store in DB like ORACLE. And next i want to retrieve this image into web page using servlets. please try to send the answer to me servlets. i was not strong otherthan this. so please send me answer using
java servlets jdbc - JDBC
java servlets jdbc  i am running the servlet program with jdbc connections in this porgram i used two 'esultset' objects.. in this wat ever coding is available in the first resultset is executed like updating and retrieiving
Servlets
Servlets  How to edit and delete a row from the existing table in servlets
servlets
servlets  How do you communicate between the servlets?   We can communicate between servlets by using RequestDespatcher interface and servlet chaining
servlets bulk - Java Beginners
servlets bulk  Hi, My problem is " i want to send a one mail to many persons(i.e.,same mail to many persons) using servlets". Pls help me.  Hi Friend, Please visit the following links: http
servlets mails - Java Beginners
servlets mails  Hi, My problem is " i want to send a one mail to many persons(i.e.,same mail to many persons) using servlets". Pls help me.  Hi Friend, Please visit the following links: http
servlets
what is the architecture of a servlets package  what is the architecture of a servlets package   The javax.servlet package provides interfaces and classes for writing servlets. The Servlet Interface The central
Servlets
Servlets  How to check,whether the user is logged in or not in servlets to disply the home page
servlets - Java Beginners
servlets  Hello! am doin my servlet course at niit, i want know in detail methods of servlets,and where can we implement it i.e. i want to know the examples of the methods used in it. what is getInParameter(); does.please
Servlets - Java Interview Questions
and are available to all the servlets within that application. It represents your web... are specified for a particular servlet and are unknown to other servlets... information visit to : http://www.roseindia.net/servlets/ServletContext.shtml
servlets
servlets  why we require wrappers in servlets? what are its uses? Please explain   These wrappers classes help you to modify request...://www.roseindia.net/servlets/response-filte.shtml
servlets
servlets  How to open and read the contents of a text file in servlets?   Please visit the following link: Read text file using Servlet
Building Search Engine Applications Using Servlets !
using Java Servlets. You can Download the source code of search engines and modify it according to requirement. Java Servlets... these days. This search engine shows you how to use Java Servlets
servlets deploying - Java Beginners
servlets deploying  how to deploy the servlets using tomcat?can you...); } } -------------------------------------------------------  This is servlets.... http://www.roseindia.net/servlets Thanks Amardeep
How to Upload a file directly to Oracle database using JSP or Servlets?
How to Upload a file directly to Oracle database using JSP or Servlets?  Hi I want to upload a file(csv or excel) to Oracle 10g Database using JSP, here is my code which gives me an error.. Addfile.jsp <%@ page import
servlets
what are filters in java servlets  what are filters in java   Filters are powerful tools in servlet environment. Filters add certain functionality to the servlets apart from processing request and response paradigm
servlets
servlets   Hi what is pre initialized servlets, how can we achives?   When servlet container is loaded, all the servlets defined in the web.xml file does not initialized by default. But the container receives
servlets
servlets why do we need web-inf directory in web application  why do we need web-inf directory in web application? means what's the benefits of doing so
Servlets and
Servlets and   Sir...! I want to insert or delete records form oracle based on the value of confirm box can you please give me the idea.... thanks
servlets
which are the differ ways you can communicat between servlets  which are the differ ways you can communicat between servlets   Different ways of communicating between servlets:- 1)Using RequestDispatcher object. 2
Java Servlets - Java Interview Questions
Java Servlets  1.What is Pojo class? How and where it is used? 2.Can... where can i download NetBean 5.5.1?  Pojo class is normal java... " THE JAVA CLASS CANNNOT EXTENDS ANY PREDEFINED THIRDPARTY API CLASS OR INTERFACE". 3
servlets
what are sessions in servlets  what are sessions in servlets   A Session refers to all the request that a single client makes to a server...: http://roseindia.net/jsp/jspsession/ http://www.roseindia.net/servlets
servlets
servlets  what is ResultSet?   ResultSet is a java object that is used for database connectivity to hold the data returned by a select query. When we run a select query it returns us the data in a table format with each
the servlets
what is diff between generic servlets and httpservlets  what is diff between generic servlets and httpservlets   Difference between GenericServlet and HTTPServlet: 1)GenericServlet belongs to javax.servlet package
servlets
regarding the user usage and habits. Servlets sends cookies to the browser client...://www.roseindia.net/jsp/jspcookies.shtml http://www.roseindia.net/servlets/use
java servlets - Java Beginners
java servlets  i want to close window when ever he logout from one page... this closing is done with yes/no option. when ever press "yes" it means he successfully logout from page. if press "no" it means he not logout from
how to write the coding for converting the database data for example population into any type of graph using jsp and servlets?//
coding for converting the database data  how to write the coding for converting the database data for example population into any type of graph using jsp and servlets pls help me out how to write the coding for converting
java struts servlets writing - Struts
java struts servlets writing  sir..i am using eclipse here for software development.and i just want to know what all are the neccessary steps to write servlets pragrams for stuts applicatuin.here am developing a small application

Ads