Hi guys what I want to do is once the user has entered the id number it displays the results for the particular record. This works fine however what would I need to do to the following code so if the id number does not exist it produces an error message.

Code can be seen below:


method class:

public static void UPDATEPLAYLIST( ) {
String p = CheckVideos.videoNo.getText();
try {
            ResultSet res = stmt.executeQuery(
        "SELECT * FROM VIDEOS WHERE VIDEOID = " + p + " ORDER BY VIDEOID");
      while (res.next()) {
         CheckVideos.information.append(
           "  "+res.getInt("VIDEOID") + ""
           );
        } catch (Exception e) {
            System.out.println(e);
        }
    }

Recommended Answers

All 5 Replies

simply change your while condition, so that instead, you do the rs.next() in a if, so if .next returns false then you can launch say a JOption pane in the else section. then after the whole process lauch a confirm dialog and loop the while on the dialog result

int ds; //i think DialogResults are ints but they could be string
do{
    //save user input
    //execute statement & save rs
    if(rs.next()){
       //do your stuff
    }
    else{
        JOptionPane.showMessageDialog(this,"wrong input" , "Error!" , JOptionPane.ERROR_MESSAGE); //soemthing like that~ ish
    }
    
    ds = JOptionPane.showConfirmDialog(...); // ask if user wants to continue
}while(dr == DialogResult.YES); //not 100% sure about this syntax im writing this off the top of my head, but it should be something similar

something like that would allow you to keep looping as needed and display an error message, that is considering your are only interested in 1 row at a time per user request, if you need to loop through multiple rows for each request then you will need to adapt this snippet a bit to put the if in another while loop inside the current while, and do something liek a boolean flag variable that you set to false in the "else" section so that the loop stops.

anyhow, good luck, let me know how you fix it :D

simply change your while condition, so that instead, you do the rs.next() in a if, so if .next returns false then you can launch say a JOption pane in the else section. then after the whole process lauch a confirm dialog and loop the while on the dialog result

int ds; //i think DialogResults are ints but they could be string
do{
    //save user input
    //execute statement & save rs
    if(rs.next()){
       //do your stuff
    }
    else{
        JOptionPane.showMessageDialog(this,"wrong input" , "Error!" , JOptionPane.ERROR_MESSAGE); //soemthing like that~ ish
    }
    
    ds = JOptionPane.showConfirmDialog(...); // ask if user wants to continue
}while(dr == DialogResult.YES); //not 100% sure about this syntax im writing this off the top of my head, but it should be something similar

something like that would allow you to keep looping as needed and display an error message, that is considering your are only interested in 1 row at a time per user request, if you need to loop through multiple rows for each request then you will need to adapt this snippet a bit to put the if in another while loop inside the current while, and do something liek a boolean flag variable that you set to false in the "else" section so that the loop stops.

anyhow, good luck, let me know how you fix it :D

Thanks for that can't believe it was so simple makes me look stupid

there are no stupid questions :P

everyone starts somewhere!

simply change your while condition, so that instead, you do the rs.next() in a if, so if .next returns false then you can launch say a JOption pane in the else section. then after the whole process lauch a confirm dialog and loop the while on the dialog result

int ds; //i think DialogResults are ints but they could be string
do{
    //save user input
    //execute statement & save rs
    if(rs.next()){
       //do your stuff
    }
    else{
        JOptionPane.showMessageDialog(this,"wrong input" , "Error!" , JOptionPane.ERROR_MESSAGE); //soemthing like that~ ish
    }
    
    ds = JOptionPane.showConfirmDialog(...); // ask if user wants to continue
}while(dr == DialogResult.YES); //not 100% sure about this syntax im writing this off the top of my head, but it should be something similar

something like that would allow you to keep looping as needed and display an error message, that is considering your are only interested in 1 row at a time per user request, if you need to loop through multiple rows for each request then you will need to adapt this snippet a bit to put the if in another while loop inside the current while, and do something liek a boolean flag variable that you set to false in the "else" section so that the loop stops.

anyhow, good luck, let me know how you fix it :D

while(dr == DialogResult.YES) ...
what is dr? did you mean ds? also, something I don't understand about the code shown so far: what exactly do you need a while loop for anyway? since you're testing on the id of the video ... do you have duplicate id's? if not you shouldn't iterate over the ResultSet, since it's only supposed to contain one element.

while(dr == DialogResult.YES) ...
what is dr? did you mean ds? also, something I don't understand about the code shown so far: what exactly do you need a while loop for anyway? since you're testing on the id of the video ... do you have duplicate id's? if not you shouldn't iterate over the ResultSet, since it's only supposed to contain one element.

i simply adapted his code a bit, i didnt check what hes requesting and what is being returned, just helped with with the error handling a bit.

dr was a in i created before the loop to store the answer from JOptionPane, but the whole loop thing was a bad concept, since after inputing data to loopup you will want to browse it not be prompted if you want to find another right away lol.

didnt think this one through while typing it, but it wasnt the main issue with this post so no biggie i guess, the if/else part was all this guy needed, it might be worth noting that his original loop is messed up, that thing shouldnt compile. hes got the try block surrounding the while but the catch block is inside the loop...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.