Need help with basic ready to program issue

Like this then?

Code:
try
            {

                String user = myuser.getText ();
                String pass = mypass.getText ();

                //    String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                //    int count = rs.getInt (1);

                String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
             
                rs = stmt.executeQuery (sql);

                int count = Integer.parseInt (rs.getString ("Cnt"));
                               
                System.out.println ("The result of the sql count is " + count);    
 
                rs.beforeFirst ();

                rs.next ();

There's no printout of:

Code:
System.out.println ("The result of the sql count is " + count);

I can't seem to login to the database at all now, with incorrect or correct details. Still throws the same exception, though.

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
 
Last edited:
Database connection
Code:
try {
            con = DriverManager.getConnection("jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb)};DBQ=database.mdb");
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery("SELECT * FROM Table;");
        } catch (Exception e) {
            System.out.println(e.toString());
        }

Remember to declare your variables and import the sql package
Code:
import java.sql.*;

    Connection con;
    Statement stmt;
    ResultSet rs;

Code:
            String GH = myuser.getText ();
            
            String user = myuser.getText ();

Why are you getting the same value twice?
Can you upload your DB online?
 
Last edited:
Why are you getting the same value twice?
Can you upload your DB online?

The code:
String GH = myuser.getText ();

is merely for making a welcome message whenever the user logs in, i.e
User types: John White

John is obtained and placed @ top.


P.S Where would I be able to upload it easily?
 
Like this then?

Code:
try
            {

                String user = myuser.getText ();
                String pass = mypass.getText ();

                //    String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                //    int count = rs.getInt (1);

                String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
             
                rs = stmt.executeQuery (sql);

                int count = Integer.parseInt (rs.getString ("Cnt"));
                               
                System.out.println ("The result of the sql count is " + count);    
 
                rs.beforeFirst ();

                rs.next ();

There's no printout of:

Code:
System.out.println ("The result of the sql count is " + count);

I can't seem to login to the database at all now, with incorrect or correct details. Still throws the same exception, though.



beforeFirst () and next() must be placed before the getString(). Cant get info from the resultset if the cursor is off the set. Can you post a while Java file for more complete debugging
 
The code:
String GH = myuser.getText ();

is merely for making a welcome message whenever the user logs in, i.e
User types: John White

John is obtained and placed @ top.


P.S Where would I be able to upload it easily?

Isn't String GH and String user the same?
Any filehoster such as Mega or Zippyshare
 
Successfully connected to the database.
The result of the sql count is 1
Mybtn1 has encountered a error:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

Code:
if (eve.getSource () == mybtn1)
        {

            String GH = myuser.getText ();
            lblnaam.setText ("Welcome to your profile, " + GH + "                                          ");



            //if printed out using System.out.println should view as follows, with sample username and password user:pass
            // SELECT COUNT ( * ) FROM LOGIN WHERE Username = 'user' AND Password = 'pass';


            try
            {

                String user = myuser.getText ();
                String pass = mypass.getText ();

                //    String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                //    int count = rs.getInt (1);

                String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";

                rs = stmt.executeQuery (sql);

                rs.beforeFirst ();
                rs.next ();

                int count = Integer.parseInt (rs.getString ("Cnt"));

                System.out.println ("The result of the sql count is " + count);




                if (count == 1)

                    {
                        JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
                        myframe.setVisible (false);
                        JOptionPane.showMessageDialog (null, "You are now entering your profile.");
                        JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
                        myprofile.setVisible (true);

                    }

                else

                    {
                        JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
                    }


            }

            catch (Exception e)
            {
                System.out.println ("Mybtn1 has encountered a error:");
                e.printStackTrace ();
            }


        }

Seems to work when I fill in the correct details now, still not showing the errorbox when incorrect details are filled in.

I'm unsure what you mean by the complete java.. for debugging , but above is the complete method?
 
Can you post the whole class file, including the actual connection to the Database. I assume you've got this all in one big file?
 
Try replacing your code with this, and see where it stops

Code:
if (eve.getSource () == mybtn1)
        {

            String user = myuser.getText();
            String pass = mypass.getText();
            if ()!(user.equals("")||pass.equals("")))
            {
                lblnaam.setText ("Welcome to your profile, " + user + "                                          ");
                try
                {
                    String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                    Ssytem.out.println(sql);
                    rs = stmt.executeQuery (sql);
                    System.out.println("Query executed");
                    if (rs!=null)
                    {
                        System.out.println("Query executed succesfully")
                        rs.beforeFirst();
                        System.out.println("Cursor moved before first record");
                        rs.next();
                        System.out.println("Cursor pointing at first record");
                        String temp = rs.getString("Cnt");
                        System.out.println("Result of query is " + temp);
                        int count = Integer.parseInt(temp);
                        if (count == 1)
                        {
                            JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
                            myframe.setVisible (false);
                            JOptionPane.showMessageDialog (null, "You are now entering your profile.");
                            JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
                            myprofile.setVisible (true);
                        }
                        else
                        {
                            JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
                        }
                    }
                }
                catch (Exception e)
                {
                    System.out.println ("Mybtn1 has encountered a error:");
                    e.printStackTrace ();
                }
            }
        }
 
Code:
    void DatabaseConnection ()
    {
        try
        {
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
            String filename = "E:/Java/2013/PatDB1.mdb";
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
            database += filename.trim () + ";DriverID=22;READONLY=true}"; // add on to the end
            // now we can get the connection from the DriverManager
            con = DriverManager.getConnection (database, "", "");
            stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println ("Successfully connected to the database.");
        }
        catch (Exception ex)
        {
            System.out.println ("Connection with database NOT successful");
            ex.printStackTrace ();
        }
    }


    public void actionPerformed (ActionEvent eve)
    {

        if (eve.getSource () == mybtn)
        {



            try
            {
                int cnt = 0;
                String sql = "Select * From Games";




                if (cmbo.getSelectedItem ().toString ().length () > 0)
                {
                    sql = sql + " WHERE Genre ='" + cmbo.getSelectedItem ().toString () + "'";
                    cnt = 1;
                }
                else
                {
                    cnt = 0;
                }

                if (cmbo2.getSelectedItem ().toString ().length () > 0)

                    {
                        if (cnt == 1)
                        {
                            sql = sql + " AND Rating = '" + cmbo2.getSelectedItem ().toString () + "'";

                        }
                        else
                        {
                            sql = sql + " WHERE Rating = '" + cmbo2.getSelectedItem ().toString () + "'";
                            cnt = 1;
                        }

                    }


                rs = stmt.executeQuery (sql);

                while (rs.next ())
                {
                    if (check.isSelected ())
                    {
                        if (cnt == 1)
                        {
                            for (int x = 0 ; x <= 130 ; x++)
                            {
                                String Genre = rs.getString ("Genre");
                                String Popularity = rs.getString ("Popularity");
                                String HighestScore = rs.getString ("HighestScore");
                                String Rating = rs.getString ("Rating");
                                String PlayerName = rs.getString ("PlayerName");
                                String TournamentAvailable = rs.getString ("TournamentAvailable");
                                String Game = rs.getString ("Game");

                                data [cnt] [0] = Genre;
                                data [cnt] [1] = Popularity;
                                data [cnt] [2] = HighestScore;
                                data [cnt] [3] = Rating;
                                data [cnt] [4] = PlayerName;
                                data [cnt] [5] = TournamentAvailable;
                                data [cnt] [6] = Game;

                            }
                        }
                        table.updateUI ();
                    }
                    else
                    {
                        break;
                    }
                }


                rs = stmt.executeQuery (sql);



                while (rs.next ())
                {




                    String Genre = rs.getString ("Genre");
                    String Popularity = rs.getString ("Popularity");
                    String HighestScore = rs.getString ("HighestScore");
                    String Rating = rs.getString ("Rating");
                    String PlayerName = rs.getString ("PlayerName");
                    String TournamentAvailable = rs.getString ("TournamentAvailable");
                    String Game = rs.getString ("Game");






                    data [cnt] [0] = Genre;
                    data [cnt] [1] = Popularity;
                    data [cnt] [2] = HighestScore;
                    data [cnt] [3] = Rating;
                    data [cnt] [4] = PlayerName;
                    data [cnt] [5] = TournamentAvailable;
                    data [cnt] [6] = Game;

                    cnt++;

                    table.updateUI ();




                }

            }


            catch (Exception e)
            {
                System.out.println ("Vraag 1 onsuksesvol");
                e.printStackTrace ();
            }
        }






        if (eve.getSource () == GetStats)
        {
            try
            {
                int cnt = 0;


                String sql = "Select * from Profiles Order by HighestScore";


                rs = stmt.executeQuery (sql);

                while (rs.next ())
                {

                    String Game = rs.getString ("Game");
                    double HighestScore = (int) (Math.random () * 5001) + 1;
                    String Rating = rs.getString ("Rating");
                    String TournamentAvailable = rs.getString ("TournamentAvailable");
                    double TournamentsPlayed = (int) (Math.random () * 251) + 1;


                    double d = HighestScore;
                    int r = (int) Math.round (d * 1);
                    double f = r / 1;

                    double g = TournamentsPlayed;
                    int m = (int) Math.round (g * 1);
                    double p = m / 1;




                    datas [cnt] [0] = Game;
                    datas [cnt] [1] = Double.toString (f);
                    datas [cnt] [2] = Rating;
                    datas [cnt] [3] = TournamentAvailable;
                    datas [cnt] [4] = Double.toString (p);


                    cnt++;
                    tables.updateUI ();
                    GetStats.setEnabled (false);




                }
            }


            catch (Exception e)
            {
                System.out.println ("Profile Onsuksesvol");
                e.printStackTrace ();
            }
        }






        if (eve.getSource () == quitbtn)
        {
            JOptionPane.showMessageDialog (null, "You will now be redirected to the main page.");
            myprofile.setVisible (false);
            myframe.setVisible (true);
        }


        if (eve.getSource () == mybtn2)
        {
            myframe.setVisible (false);
        }


        if (eve.getSource () == TNBTN)
        {

            myframe2.setVisible (false);
            JOptionPane.showMessageDialog (null, "You are now viewing the Tournament Database.");
            myframe.setVisible (true);
        }


        if (eve.getSource () == mybtn3)
        {
            JOptionPane.showMessageDialog (null, "To use this program effectively, please choose all fields respectively");
            JOptionPane.showMessageDialog (null, "To search for games, fill in the Genre box");
            JOptionPane.showMessageDialog (null, "To search for highest scores, fill in the Rating box");
            JOptionPane.showMessageDialog (null, "To search which game has the highest population, sort by Population or just search with no fields attached.");
            JOptionPane.showMessageDialog (null, "If you find any errors contact the creator to fix it asap");
        }


        if (eve.getSource () == myclearbtn)
        {
            int cnt = 0;
            for (int x = 0 ; x <= 110 ; x++)
            {
                data [cnt] [0] = "";
                data [cnt] [1] = "";
                data [cnt] [2] = "";
                data [cnt] [3] = "";
                data [cnt] [4] = "";
                data [cnt] [5] = "";
                data [cnt] [6] = "";
                cnt++;
            }
            table.updateUI ();

        }


        if (eve.getSource () == mybtn1)
        {

            String GH = myuser.getText ();
            lblnaam.setText ("Welcome to your profile, " + GH + "                                          ");



            //if printed out using System.out.println should view as follows, with sample username and password user:pass
            // SELECT COUNT ( * ) FROM LOGIN WHERE Username = 'user' AND Password = 'pass';


            try
            {

                String user = myuser.getText ();
                String pass = mypass.getText ();

                //    String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                //    int count = rs.getInt (1);

                String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";

                rs = stmt.executeQuery (sql);

                rs.beforeFirst ();
                rs.next ();

                int count = Integer.parseInt (rs.getString ("Cnt"));

                System.out.println ("The result of the sql count is " + count);




                if (count == 1)

                    {
                        JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
                        myframe.setVisible (false);
                        JOptionPane.showMessageDialog (null, "You are now entering your profile.");
                        JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
                        myprofile.setVisible (true);

                    }

                else

                    {
                        JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
                    }


            }

            catch (Exception e)
            {
                System.out.println ("Mybtn1 has encountered a error:");
                e.printStackTrace ();
            }
 
Sorry, I double posted accidently, don't know how to delete this one
 
Have you tried replacing your code with the one I just gave you?

I used this code (what you gave me)

Code:
  if (eve.getSource () == mybtn1)
        {
            String user = myuser.getText ();
            String pass = mypass.getText ();
            String GH = myuser.getText ();
            lblnaam.setText ("Welcome to your profile, " + GH + "                                          ");
            try
            {
                String sql = "SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
                System.out.println (sql);
                rs = stmt.executeQuery (sql);
                System.out.println ("Query executed");
                if (rs != null)
                {
                    System.out.println ("Query executed succesfully");
                    rs.beforeFirst ();
                    System.out.println ("Cursor moved before first record");
                    rs.next ();
                    System.out.println ("Cursor pointing at first record");
                    String temp = rs.getString ("Cnt");
                    System.out.println ("Result of query is " + temp);
                    int count = Integer.parseInt (temp);
                    if (count == 1)
                    {
                        JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
                        myframe.setVisible (false);
                        JOptionPane.showMessageDialog (null, "You are now entering your profile.");
                        JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
                        myprofile.setVisible (true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
                    }
                }
            }
            catch (Exception e)
            {
                System.out.println ("Mybtn1 has encountered a error:");
                e.printStackTrace ();
            }
        }
    }

and got this error:
Code:
Successfully connected to the database.
SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = 'dsad' AND Password = 'dasd';
Query executed
Query executed succesfully
Cursor moved before first record
Cursor pointing at first record
Mybtn1 has encountered a error:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
        at Pat.actionPerformed(Pat.java:513)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
 
And?

Where did it bomb out! It'll help us going forward. I assume from the code posted above that you called DatabaseConnection() in the constructor, before you tried to worry about button clicks?
 
The DatabaseConnection() is called first thing in the constructor called Pat()

Where did it bomb out?

I don't understand ~ I entered wrong details & obtained the error in quote below
entering correct details still works though.

Successfully connected to the database.
SELECT COUNT(*) AS Cnt FROM LOGIN WHERE Username = 'dsad' AND Password = 'dasd';
Query executed
Query executed succesfully
Cursor moved before first record
Cursor pointing at first record
Mybtn1 has encountered a error:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
 
Top
Sign up to the MyBroadband newsletter
X