#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0

    checking if data exist in database


    in my program, i am checking if the user clicks on a certain button. if a button is clicked, i need to do a query and update the table. however, before i update the database, i need to also check if the data retrieved from the textfield already exists.

    I am using the request.getParameter to get the values of the user input. after getting the values, i need to check the database to see if it already exist.

    i am currently using:
    if(check!="null"){xyz}

    where xyz is the query.

    how do i do it???
    pls help asap....
  2. #2
  3. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    14
    do a query for rowcount...if the rowcount is 0 then it is probably not there...
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Vicksburg, MS
    Posts
    7
    Rep Power
    0
    In java, the results of a jdbc sql query come back in a ResultSet object. BY calling the more method of ResultSet, you can determine if the result set has any data.

    A ResultSet is processed as in the following method:

    /**Processes the ResultSet object passed, and creates html string text
    *for display by the HTMLFrame viewer.
    */
    public void displayResults(ResultSet rs){
    if (rs == null) return;
    String displaystring = "";
    try{
    // use ResultSetMetaData to check column headings
    ResultSetMetaData rsmd = rs.getMetaData ();
    // Get the number of columns from the ResultSetMetaData
    int columns = rsmd.getColumnCount();
    boolean more = rs.next();
    //more will be false if there is no return data to display
    if (more) {
    displaystring = "<html><body>\n" + "<table>\n";
    displaystring = displaystring + "<tr>\n";
    for (int j=1; j <= columns; j++) {
    displaystring = displaystring + "<td><b>" + rsmd.getColumnName(j) + "</b></td>";
    }
    displaystring = displaystring + "<tr>\n";
    while (more){
    displaystring = displaystring + "<tr>\n";
    for (int i=1; i <= columns; i++) {
    displaystring = displaystring + "<td>" + rs.getString(i) + "</td>";
    }
    displaystring = displaystring + "<tr>\n";
    //more will change to false when the end of the result set is reached
    more = rs.next();
    }
    displaystring = displaystring + "<table>\n" + "</h2></body></html>";
    }else{
    displaystring = "<html><body>SQL statement executed. No results to display.</h2></body></html>";
    }
    if (debug) println(displaystring);
    resultsdisplay.show(displaystring);
    }catch (SQLException sqlexception){
    displaySqlExceptionInformation(sqlexception);
    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo