Results 1 to 13 of 13
Thread: JDBC MySql question
- 01-18-2011, 02:27 PM #1
JDBC MySql question
Hi, im a newbeginner at JDBC and Mysql.
here is my problem:
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...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); } }
Please someone help me =(
- 01-18-2011, 02:33 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
What line is throwing the NullPointer?
In order to find that you need to change the catch block to e.printStackTrace().
- 01-18-2011, 02:46 PM #3
I'm only noob i cant read that =P cant see nullpointer =/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)
- 01-18-2011, 02:50 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Database.findUser(Database.java:131)
Line 131 of Database.java has the NullPointer.
So...which line is line 131?
- 01-18-2011, 02:53 PM #5
This was the line where
for (StackTraceElement ex : new Throwable().getStackTrace())
System.out.println(ex);
to print the stack trace =/
- 01-18-2011, 02:55 PM #6
obs my misstake
- 01-18-2011, 02:58 PM #7
The real printstacktrace shows a error at ResultSet RS = stmt.executeQuery line.
- 01-18-2011, 03:05 PM #8
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-18-2011, 03:07 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
So, as goldest says, stmt is null.
Without some more code we cannot tell you why it's null.
- 01-18-2011, 03:30 PM #10
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?
- 01-18-2011, 03:33 PM #11
I forgot to remove "Statement stmt;".... THANK YOU GUYS =) I LOVE U ... The whole day spent on this mistake... and now im freee
- 01-18-2011, 03:41 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
You beat me to it!
:)
- 01-18-2011, 03:47 PM #13
Similar Threads
-
com.mysql.jdbc.Driver
By uthpalaw in forum EclipseReplies: 2Last Post: 10-14-2010, 05:09 AM -
ClassNotFoundException com.mysql.jdbc.Driver
By Heather in forum JDBCReplies: 4Last Post: 03-31-2010, 12:08 PM -
JDBC data retrieval with MySQL
By hadesflames in forum JDBCReplies: 5Last Post: 04-23-2009, 04:38 AM -
MySQL/JDBC Mysql query output
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-13-2009, 01:57 AM -
help ... JDBC or PHP/MySQL
By bluebarca in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 10:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks