Solved

Validation of data

Posted on 2002-03-24
7
157 Views
Last Modified: 2010-03-31
Can anyone help me with simple authentication in the existing code.

In this i need to check if the input data exist in the particular field in database, if it does not exist then insert into database else throw an error

import javax.servlet.http.HttpServlet;

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

public class subServlet extends HttpServlet{
  public void doGet(HttpServletRequest req,
                    HttpServletResponse res)
                      throws ServletException, IOException{
                           
   System.out.println("req fetched");                    
    try {
      Statement stmt;//used for database access
      ResultSet rs;//used for database access

      //Set the content type of the data to be sent back
      // to the client.
      res.setContentType("text/html");
     
      //Get an output writer object to send data back to
      // the client.
      PrintWriter out = res.getWriter();
     
           
      //Register the JDBC driver
      String JDBCDriver="oracle.jdbc.driver.OracleDriver";
      Class.forName(JDBCDriver);
      System.out.println("oracle.jdbc.driver.OracleDriver");
      //Construct URL for database on node3
      String JDBCUrl = "jdbc:oracle:dev";
      String JDBCUser = "bang";
      String JDBCPassword = "viz";
      System.out.println("");

      //Get a connection to the database
       Connection con = DriverManager.getConnection(JDBCUrl, JDBCUser, JDBCPassword);                  

   
      //Get a Statement object
      stmt = con.createStatement();
      System.out.println("createStatement");

      //Get the two incoming data parameters from the
      // client
       String email_id = req.getParameter("email_id");
       System.out.println("EmailID :"  + email_id);
   
       
      //Put the data into the database table. Don't forget
      // to enclose the string data in single quotes to
      // satisfy the SQL syntax requirement.
   
    /**
     * Add the value into database if true only.
     * @return true, if add was successful
     */


     if(req.getParameter("email_id") == (){
             System.out.println("Sorry Dude");
        }
        else
             stmt.executeUpdate("INSERT INTO SUB_TABLE (email_id) values ('" + email_id + "')");                    
            System.out.println("insert SUB_TABLE");
           

       stmt.executeUpdate("INSERT INTO SUB_TABLE (email_id) values ('" + email_id + "')");                    
       System.out.println("insert SUB_TABLE");
       
      //Use the statement object to execute a query on the
      // database.
      rs = stmt.executeQuery("SELECT * from SUB_TABLE  ORDER BY EMAIL_ID");
     
      //Extract data from the ResultSet object and display
      // in the HTML page sent back to the client.
         System.out.println("Display all results:");
          while(rs.next()) {
          String e = rs.getString("EMAIL_ID");
         String a = rs.getString("ACT_ID");
         System.out.println(e + "   " + a);
    }//end while loop
         
      //Close the connection to the database      
      con.close();
    }catch( Exception e ) {
      e.printStackTrace();
    }//end catch
  }//end doGet()
}//end class
0
Comment
Question by:hardraks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Accepted Solution

by:
Nushi earned 50 total points
ID: 6893681
its not clear to me what you are trying to do.
you want to check if you already have a record in the database for acertain field?

the way to avoid it is to decleare the fiels as PRIMARY key (in the database) and by doing that you can insert each value only once and you will get exception when trying to insert the same value more then once.

so basiccly you will have ask the DBA at your company to update the table script foryou and it will do the work for you.

nushi.
0
 
LVL 10

Expert Comment

by:Nushi
ID: 6893685
if you want to check it manually you will have to do the select yourself.

execute the select FILED_NAME from ... bla bla bla

and if the resaultSet you get in return is empty you dont have any data regards to this value.
else you can do your insert.

Nushi.
0
 

Author Comment

by:hardraks
ID: 6895180
So can u be more specific on the manual comment...ie
execute the select FILED_NAME from ... bla bla bla

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Nushi
ID: 6895769
yep.

the steps you have to do are the following:
1. declare the needed field as primary Key in the database (ask your dba how to do it or if you dont know any ill explain to you how to do it). doing this will insure that this column CANNOT contains 2 identical values. each try to insert value that already exist will throw exception by oracle (parent key vioalted exception or something like that). you will have to see in your application if you can insert same start date with diffrent end date (it can be done too by declaring the start date AND end date as the key for the table so those 2 columns will have to be unique.

2. after doing that you have 2 options:
A. execute a select query on the table (select * from SUB_TABLE where START_DATE=[your value]. if you will get resault with values it means that you already has a record.
B. try to insert the data as you did before and you will get the exception i explained before.

if you need any thing else just ask me.

Nushi.
0
 

Author Comment

by:hardraks
ID: 6929932
Thanks for your comments i'm working on it...If any issue i shall shoot back...
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7894629
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Nushi
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7962531
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 117
Convert from a json string array to a Java object 3 65
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 78
Java Eclipse Loop 3 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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