Solved

insert new values in table using servlet ??

Posted on 2002-05-07
2
160 Views
Last Modified: 2013-11-24
would u give me simple example how to store values which are send from a form to a servlet into tables in microsoft access.
0
Comment
Question by:star4
2 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
Comment Utility
import java.sql.*;

Connection con = null;

try {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

   con = DriverManager.getConnection("jdbc:odbc:<DSN>");

   Statement stmt = con.createStatement();
   stmt.executeUpdate("Insert into <your table> (field1, field2, field3) values (<param1>, <param2>, <param3>)");
   stmt.close();
} catch (Exception e) {
   System.out.println(e.getMessage());  
} finally {
   if (con != null) { con.close(); }
}

to get parameters from form:

String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
String param3 = request.getParameter("param3");

0
 

Author Comment

by:star4
Comment Utility
may u help again bobbit31 :
i tried this code but nothing is entered in the database ?

 String strSQL,strSQL1,strSQL2;
               String empno,password,todaydate,massage;
         
               //Storing the result of Query in those variavles
               //after initializing them to null
               ResultSet rs=null, rs1=null;
            Statement stmt;
           
               //Making variables and initialze them
               int i=0;
               //In order to establish a connection to the databse
               //we should have the approperiate driver connect to
               //the DBMS.
               //db1 : is the name of the database where it is defind
               //on the jdbc:odbc driver
               
               Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
           con = DriverManager.getConnection("jdbc:odbc:db1");
                                                                //Creating statement for connection
               stmt = con.createStatement();
               
               //Get userid ,password ,month and year from the
               //web site forms, and store them in the approperiate variables
               empno = req.getParameter("empid");
            password = req.getParameter("password");
               todaydate = req.getParameter("todaydate");
            massage = req.getParameter("massage");
               //Check the Empid and password intered by the user
               //match the Empid and password stored in table LoginTable
               strSQL = "select * from LoginTable " +
            "where EmpID like '" + empno + "' and " +
           "Password like '" + password + "'";
              rs = stmt.executeQuery(strSQL);
           
               //If password and Empid are correct then assign true to logged
               //otherwise assign false
               if (rs.next())                                                      
                         {  
                     //Password and Empid are correct
                     logged=true;
                     //close the result
                     rs.close();
                }
                else
                {  
                  //Password or Empid are not correct
                     logged =false;
                     rs.close();
                }
               
               //IF logged = true
                if (logged)
                {      
                out.println("gooooooooood");              
            stmt.executeUpdate("Insert into reqleaves (empno,sendeddate,massage)values (empno, todaydate,massege)");
              }
             else
                {  
              out.println("Please Check your id and password");
                }    
                  //close the statement
               stmt.close();
                 
              //if errors exist then display them
       } catch (Exception e)
         {
           e.printStackTrace();
       }
         finally
         {
            if (con != null)
               {
                 try
                     {
                      con.close();
                 } catch (SQLException se) {
                      se.printStackTrace(System.err);
                 }
            }
       }
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now