We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,220 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

database check user input

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);
        }
    }
3
Contributors
5
Replies
20 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
TIM_M_91
Junior Poster in Training
83 posts since Feb 2012
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0

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

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

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

TIM_M_91
Junior Poster in Training
83 posts since Feb 2012
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0

there are no stupid questions :P

everyone starts somewhere!

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

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.

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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...

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0842 seconds using 2.72MB