Results 1 to 2 of 2

Thread: Select Count

  1. #1
    Apple2 is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default Select Count

    The following is sql code which checks for multiple “titles” with the same value (less than or equal to 3)

    "SELECT count(*)<=3 FROM module WHERE title='?'".

    I am trying to write a snippet of code that does a select count(*) from module where title=? (above code), and then if the result of that is 3=3 then do the INSERT (following code) else display an error message without inserting the record.

    In simple terms the following code should only execute if the above code is true, else display a message without saving. can someone please help me out as I am kind of stuck.

    PreparedStatement new_module = connection.prepareStatement("INSERT INTO module (room_number, module_code, title, provider, duration, time_slot, weekdays)"+ "VALUES(?,?,?,?,?,?,?)");

    new_module.setString(1, request.getParameter("room_number"));
    new_module.setString(2, request.getParameter("module_code"));
    new_module.setString(3, request.getParameter("title"));
    new_module.setString(4, request.getParameter("provider"));
    new_module.setInt(5, 1);
    new_module.setString(6, request.getParameter("time_slot"));
    new_module.setString(7, request.getParameter("weekdays"));

    new_module.executeUpdate();
    connection.commit();
    new_module.close();
    response.sendRedirect("index.jsp");
    connection.setAutoCommit(savedCommitValue);

    }

  2. #2
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default

    hi,

    Can u split the code into two something like this.

    select count(1)no_of_rows from module where title like 'title_value'

    rs=pstmt.executeQuery();

    if(rs.next())
    {
    if(rs.getInt("no_of_rows")<=3)
    {
    --code to insert the second query
    }else{
    --code to show the error message
    }
    }

    Regards,
    Felix

Similar Threads

  1. Replies: 0
    Last Post: 04-06-2008, 08:45 PM
  2. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM
  3. how to count 2 inserts together?
    By kim85 in forum New To Java
    Replies: 0
    Last Post: 01-20-2008, 12:25 PM
  4. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 10:49 PM
  5. Getting row count from executeQuery()
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •