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);
}
}
Related Article: Check user input
is a Java discussion thread by sha11e that has 32 replies and was last updated 1 year ago.
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
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
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