Hey guys,
i found this tutorial online that involved searching your database for an existing value. I have changed the code around so it fits with my system but when i run it the input displays but once i press ok the input box closes and nothin else happens.

Heres the Code :

private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {
        Scanner sc = new Scanner(System.in);
        try{
            java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ireland","root" ,"password"); 


        Statement stmt = con.createStatement();

       JOptionPane.showInputDialog("Please Enter the pubs name you wish to search.");

        String pubName = sc.next();

        String SQL = "SELECT * FROM pubs WHERE Pub_Name = '" + pubName +"'";

        ResultSet rs = stmt.executeQuery(SQL);

        if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Pub already exists!!");
            }
        else {
                JOptionPane.showMessageDialog(null, "Pub doesn't exists!!");
            }
        }
        catch (Exception e){
          JOptionPane.showMessageDialog( null, "There was a problem inserting into the database please try again ", "ERROR", JOptionPane.ERROR_MESSAGE);
        }

    }

Can anyone tell me whats goin on ??

Thanks :)

Recommended Answers

All 5 Replies

Just to understand what you are saying...
the option pane on line 9 displays, but then you do nor see any of the option panes on lines 18, 21 or 25?

hai joseph.lyons,

you need to take the input value for pub name to search from either of the one mechanism

by using this way-1

 String pubName = JOptionPane.showInputDialog("Please Enter the pubs name you wish to search.");
 // this mechanism expects value for pubname from its input box

or using this way-2

String pubName = sc.next();    // this mechanism expects input value for pub name from the command prompt

i think this might be the one reason:

when you came after the vale given into the JoptionPane.ShowInputDialog() method your programs is waiting for the user input which takes from command prompt until you enter the value (i mean this sc.next() makes your program like that)

but you did not do that i think.

thats the reason your program was stopped there without executing the remaining statements

please use one of the above ways for getting input from the user

and make changes to your program accordingly the ways what i described above

i think you will get my point

let me know if you have any doubts in my calrification as well as the status after the modification of your program

any comments are appreciated
[modified]

I made both changes and it doesn't do anything still. I get what ya mean alright. And I am not getting any errors either?

Way 2

   Scanner sc = new Scanner(System.in);

        try{
            java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ireland","root" ,"pass"); 


        Statement stmt = con.createStatement();


       JOptionPane.showInputDialog("Please Enter the pubs name you wish to search.");

         String pubName = sc.next();


        String SQL = "SELECT * FROM pubs WHERE Pub_Name = '" + pubName +"'";

        ResultSet rs = stmt.executeQuery(SQL);

        if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Pub already exists!!");
            }
        else {
                JOptionPane.showMessageDialog(null, "Pub doesn't exists!!");
            }
        }
        catch (Exception e){
          JOptionPane.showMessageDialog( null, "There was a problem inserting into the database please try again ", "ERROR", JOptionPane.ERROR_MESSAGE);
}

Way 1

Scanner sc = new Scanner(System.in);
        try{
            java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ireland","root" ,"pass"); 


        Statement stmt = con.createStatement();


       String pubName = JOptionPane.showInputDialog("Please Enter the pubs name you wish to search.");

          pubName = sc.next();


        String SQL = "SELECT * FROM pubs WHERE Pub_Name = '" + pubName +"'";

        ResultSet rs = stmt.executeQuery(SQL);

        if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Pub already exists!!");
            }
        else {
                JOptionPane.showMessageDialog(null, "Pub doesn't exists!!");
            }
        }
        catch (Exception e){
          JOptionPane.showMessageDialog( null, "There was a problem inserting into the database please try again ", "ERROR", JOptionPane.ERROR_MESSAGE);
        }

The same thing I press the check DB button on the jframe form the input box is displayed I enter the name the box disepears and thats it ??

Any help will be greatly appreciated

hai joseph.lyons,

i dont know whether you got my point or not

no problem at all i think you are in the learning stage

i said use either of one way but you included both

i mean in your code use this line (in this case you have to enter your input in the dialog box)

String pubName = JOptionPane.showInputDialog("Please Enter the pubs name you wish to search.");

or this line (in this case you have to enter your input at command prompt)

String pubName = sc.next();

thats it

please do accordingly and let me know the status as well as if you have any doubts in my clarification

any comments are appreciated

Thanks man got it workin. One more question how do i stop it from recognising white spaces as part of the name??

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.