java servlets with database interaction

Ads
 

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

Ads









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
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
Advertisements
java servlets
java servlets  please help... how to connect java servlets with mysql i am using apache tomcat 5.5
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
java servlets - JSP-Servlet
java servlets  how to get the parameter value of input type file using java servlets  Hi, You can use request.getParameter(). In Jsp, --- --- IN servlet, String fileName=request.getParameter('fl'); If you
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
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
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
java database
java database  help me with this...i never learn build java database... to design and implement objects needed for a program that maintains a database.../teacher information, search it, alter it, etc. The database is maintained as a list
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  what is the duties of response object in servlets
Database,Java
Database,Java  help me for the following things if one customer... me the how to write the query in MySQL database and i want coding for this one in Java.because i am using Java as the front end.   1)page.jsp: <
servlets
what are advantages of servlets  what are advantages of servlets   Please visit the following link: Advantages Of Servlets
java and database
java and database  sir i want to prepare a sign in form and i want to check that user name is exist or not in database if it is exist in database then return it is allready exist otherwise it is registered in data base thank u
JAVA SERVLETS ? An Overview
JAVA SERVLETS ? An Overview     ... programming in general and Java Servlets in particular. In addition...; Introduction to Java Servlets    Installation
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
Servlets
Servlets  How to edit and delete a row from the existing table in servlets
Introduction to Java Servlets
Introduction to Java Servlets       Java Servlets are server side Java programs that require... associated information required for creating and executing Java Servlets
What is Java Servlets?
What is Java Servlets?      ... enabled web server. Servlets run entirely inside the Java Virtual Machine. Since.... Servlets can access the entire family of Java APIs, including the JDBC API
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
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
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  How to check,whether the user is logged in or not in servlets to disply the home page
Register page using servlets,jsp and java beans
Register page using servlets,jsp and java beans  i want code for register page using jsp,serlets and java beans.iam getting error for my code in java beans pls help me   Hi Friend, Please visit the following links
How to export web page to excel using java or jsp or servlets
errors. Please can anyone tell me how to do this using java or jsp or servlets...How to export web page to excel using java or jsp or servlets  Hi I am trying to export web page(jsp page ) to excel using jsp or servlets. I am
how to convert java fram to java servlets
how to convert java fram to java servlets   hi every java master or java professional can you teach me how to convert JFram to Java Servlets program hope yours can reply me Thank you. below this is my own coding import
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
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
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
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

Ads