Reputation: 27640
I have a table in MySQL which name is "clienttable" and it has 11 columns that the 9th column is "yahooId" and the 11th column is "password".I have two exception : 1: SQL Syntax for line rst = stmt.executeQuery("SELECT yahooId AND password FROM clienttable");
and 2: NullPoniterException for line while (rst.next())
please help me,thanks.
Also this is what I have written in my database class:
public static boolean Test(String userName, String password) {
boolean bool = false;
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
ResultSet rst = null;
try {
rst = stmt.executeQuery("SELECT yahooId AND password FROM clienttable");
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
try {
while (rst.next()) {
if (rst.getString(9).equals(userName) && rst.getString(11).equals(password)) {
bool = true;
} else {
bool = false;
}
}
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return bool;
}
the exception:
init:
deps-jar:
compile-single:
run-single:
Jan 10, 2010 6:45:52 PM ClientDataBase.Manager Test
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM clienttable' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
at ClientDataBase.Manager.Test(Manager.java:94)
at ClientGUI.MainFrame.submit(MainFrame.java:374)
at ClientGUI.MainFrame.jButton1ActionPerformed(MainFrame.java:257)
at ClientGUI.MainFrame.access$600(MainFrame.java:33)
at ClientGUI.MainFrame$7.actionPerformed(MainFrame.java:143)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ClientDataBase.Manager.Test(Manager.java:99)
at ClientGUI.MainFrame.submit(MainFrame.java:374)
at ClientGUI.MainFrame.jButton1ActionPerformed(MainFrame.java:257)
at ClientGUI.MainFrame.access$600(MainFrame.java:33)
at ClientGUI.MainFrame$7.actionPerformed(MainFrame.java:143)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Upvotes: 0
Views: 1509
Reputation: 182782
The SQL error is causing the second error. You need to separate the column names with commas, not with "AND".
Also, it doesn't matter that yahooid and password are the 9th and 11th column in the table. Once you fix the SQL syntax error, they are the first and second columns in the query, and so you retrieve them with rst.getString(1) and rst.getString(2) respectively.
Also, it's a REALLY, REALLY bad idea to just log and ignore exceptions the way you've done. Because you haven't done anything with the first exception other than log it, you've caused cascading other exceptions. This will continue to happen unless you make your exception handlers actually handle the exceptions.
Upvotes: 4
Reputation: 2758
Your select should be:
SELECT yahooId, password FROM clienttable
You have an AND instead of a comma. Since the query isn't valid, no ResultSet is returned and you get the null pointer.
EDIT: Your query will return a result set, just not the one you want. yahooId AND password will return one column - the result of logical AND on those columns. When you then to rs.getString("yahooId"), that returns null and you have a .equals() right on there. The root issue is that you need a comma but my reason for the null was wrong.
EDIT 2: I tried the query and it worked on my mysql. Maybe it's a version thing. In any case, for his issue, I was correct before the edit.
Upvotes: 6
Reputation: 67760
(1)
SELECT yahooId AND password FROM clienttable
AND
doesn't belong in there. It should read
SELECT yahooId, password FROM clienttable
(2)
Your loop with next
appears after you catch an exception from your query. If your query threw an exception, then there's nothing to look at in rst
; so this code should all be within the try
.
Upvotes: 3