Results 1 to 13 of 13
  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Unhappy JDBC MySql question

    Hi, im a newbeginner at JDBC and Mysql.
    here is my problem:
    Java Code:
    public static void findUser(){
       
        try{
          
      
            card = Integer.parseInt(GUI.tmp);
            ResultSet RS = stmt.executeQuery
                ("SELECT name FROM borrowers WHERE cardnr = '"+ card + "'");
    
                while (RS.next()) {
                     System.out.print( RS.getString(1) );
                   
                 }
    
        }catch(Exception e) {
          System.err.println("Fail: " + e.getMessage());
          System.out.println("Fail: " + e);
          System.out.println("Card: " + card);
        }
    }
    All i want to do is check if the variable card is in cardnr column ... But i get nullpointerexception (card has a value) and database/Table exist...

    Please someone help me =(

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    What line is throwing the NullPointer?
    In order to find that you need to change the catch block to e.printStackTrace().

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Java Code:
    Database.findUser(Database.java:131)
    GUI$4.actionPerformed(GUI.java:133)
    javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    java.awt.Component.processMouseEvent(Component.java:6267)
    javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    java.awt.Component.processEvent(Component.java:6032)
    java.awt.Container.processEvent(Container.java:2041)
    java.awt.Component.dispatchEventImpl(Component.java:4630)
    java.awt.Container.dispatchEventImpl(Container.java:2099)
    java.awt.Component.dispatchEvent(Component.java:4460)
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    java.awt.Container.dispatchEventImpl(Container.java:2085)
    java.awt.Window.dispatchEventImpl(Window.java:2478)
    java.awt.Component.dispatchEvent(Component.java:4460)
    java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    I'm only noob i cant read that =P cant see nullpointer =/

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Database.findUser(Database.java:131)

    Line 131 of Database.java has the NullPointer.

    So...which line is line 131?

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    This was the line where

    for (StackTraceElement ex : new Throwable().getStackTrace())
    System.out.println(ex);

    to print the stack trace =/

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    obs my misstake

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    The real printstacktrace shows a error at ResultSet RS = stmt.executeQuery line.

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by santa View Post
    The real printstacktrace shows a error at ResultSet RS = stmt.executeQuery line.
    So have you declared that "stmt" anywhere? You will need to use the Connection instance with createStatement().

    Something like,

    Java Code:
    Statement stmt = Connection_Instance.createStatement();

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    So, as goldest says, stmt is null.
    Without some more code we cannot tell you why it's null.

  10. #10
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Java Code:
    import javax.swing.*;
    import java.sql.*;
    
    public class Database {
    private static Statement stmt;
    public static String database,allt3[] = new String[10];
    public static int card;
    private static boolean blank = false;
    
     public static void connectDatabase(){
     Statement stmt;
     Connection con = null;
     if(database.equals("")){
         JOptionPane.showMessageDialog(null, "Can't run without database!");
         System.exit(0);}
    
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql://",  // Using anonymous cause i feel like it =)
            "root", "test");
    
    
          if(!con.isClosed())
            System.out.println("Duude youre connected " +
              "trough TCP/IP...");
    
          stmt = con.createStatement();
          try{
          stmt.executeUpdate("USE " + database);
          System.out.println("Used old database!");
            }catch (Exception e){
               stmt.executeUpdate("CREATE DATABASE " + database);
               stmt.executeUpdate("USE " + database);
               System.out.println("Database created!");
            }
          stmt.executeUpdate ("CREATE TABLE Borrowers ("
                  + "cardnr INT,"
                  + "name CHAR(60), "
                  + "street CHAR(40), "
                  + "zip INT,"
                  + "town CHAR(50))");
    
                  stmt.executeUpdate ("CREATE TABLE GoodBooks ("
             + " ISBN long,"
            + " CopyNumber INT,"
            + " Title VARCHAR(60),"
            + " Author VARCHAR(30),"
            + " Publisher VARCHAR(50),"
            + " Year INT,"
            + " Statistics INT,"
            + " BorrowDate INT,"
            + " ReturnDate INT,"
            + " LibraryCardNumber INT)");
       for(int j = 0 ; j < Borrowers.borrow.length; j++){
                     stmt.executeUpdate("INSERT INTO Borrowers "
                             +"(cardnr, name, street, zip, town) "
                             + "VALUES "
                             + "('"+ Borrowers.borrow[j].LibraryCardNumber + "',"
                             + "'"+ Borrowers.borrow[j].Name + "',"
                             + "'"+ Borrowers.borrow[j].Street + "',"
                             + "'"+ Borrowers.borrow[j].ZipCode + "',"
                             + "'"+ Borrowers.borrow[j].Town + "')");
            }
    
    
    
    
    
    
    
    for(int j = 0 ; j <Bookobjects.bookO.length ; j++){
                     stmt.executeUpdate("INSERT INTO goodbooks "
                             +"(ISBN, CopyNumber, Title, Author, Publisher, Year, Statistics,"
                             + " BorrowDate, ReturnDate, LibraryCardNumber) "
                             + "VALUES "
                             + "('"+ Bookobjects.bookO[j].ISBN + "',"
                             + "'"+ Bookobjects.bookO[j].CopyNumber + "',"
                             + "'"+ Bookobjects.bookO[j].Title + "',"
                             + "'"+ Bookobjects.bookO[j].Author + "',"
                             + "'"+ Bookobjects.bookO[j].Publisher + "',"
                             + "'"+ Bookobjects.bookO[j].Year + "',"
                             + "'"+ Bookobjects.bookO[j].Statistics + "',"
                             + "'"+ Bookobjects.bookO[j].BorrowDate + "',"
                             + "'"+ Bookobjects.bookO[j].ReturnDate + "',"
                             + "'"+ Bookobjects.bookO[j].LibraryCardNumber + "')");
            }
      
    
    
    
    
        } catch(Exception e) {
          System.err.println("Fail: " + e.getMessage());
          System.out.println("Fail: " + e);
        } /*finally {
          try {
            if(con != null)
               System.out.println("Connecton succesfully closed!");
              con.close();
          } catch(SQLException e) {}
        }*/
    
    
        }
    public static void findUser(){
       
        try{
            if(GUI.tmp.equals("")){
                JOptionPane.showMessageDialog(null, "I don't accept blanks! Enter a number.");
             blank = true;
            }
          
         if(!blank){
            card = Integer.parseInt(GUI.tmp);
            ResultSet RS = stmt.executeQuery("SELECT name FROM borrowers WHERE cardnr = '"+ card + "'");
    
                while (RS.next()) {
                     System.out.print( RS.getString(1) );
                   
                 }
            }
        }catch(Exception e) {
          System.err.println("Fail: " + e.getMessage());
         e.printStackTrace();
    
         System.out.println("Card: " + card);
        }
        blank = false;
    }
    }

    Here is the class ... any ideas?

  11. #11
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    I forgot to remove "Statement stmt;".... THANK YOU GUYS =) I LOVE U ... The whole day spent on this mistake... and now im freee

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    You beat me to it!
    :)

  13. #13
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    haha without you guys i would still be sitting changing the MySQL code and hoping for the best ... =D Thanks

Similar Threads

  1. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 06:09 AM
  2. Replies: 4
    Last Post: 03-31-2010, 01:08 PM
  3. JDBC data retrieval with MySQL
    By hadesflames in forum JDBC
    Replies: 5
    Last Post: 04-23-2009, 05:38 AM
  4. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 02:57 AM
  5. help ... JDBC or PHP/MySQL
    By bluebarca in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 11:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •