my dog learned polymorphism*
The moose likes JDBC and the fly likes problem in sql syntax (method calling) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem in sql syntax (method calling)" Watch "problem in sql syntax (method calling)" New topic
Author

problem in sql syntax (method calling)

sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
hello,
I am using mysql database for account validation, for this i wrote swing program which works good.
I can also able to recognise driver and can connect to the database. The problem is, if i use a method loadAccounts

i called above method in a actioncommand

if i run the progarm i am getting exceptions..
java.lang.NullPointerException
at MyField.loadAccounts(MyField.java:483)
at MyField.actionPerformed(MyField.java:581)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5134)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1590)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

I would think this is not the right way to call loadAccounts();
can i get a right solution from anybody..
any idea or suggestions,
thankyou..
am
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30384
    
150

You are missing the single quotes around the username. It should be:
rs = statement.executeQuery("select acc_password from acc where username='"+Uname.getText() + "'");

You can use prepared statements to avoid having to worry about the quotes.
However, that wouldn't cause a null pointer exception. It would cause a sql exception. Can you show the code where you create the statement object. This is likely to be the culprit.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
thankyou for the comment,
i worked with rs = statement.executeQuery("select acc_password from acc where username='"+Uname.getText() + "'"); which shows the same excepions as you said.
I have created statement in the method which connects to database..
<code>
public void connectToDB() {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/accounts");
System.out.println("database connected");
statement = connection.createStatement();
statement.setMaxRows(5);
statement.setFetchSize(2);
} catch(SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
System.exit(1);
}
}
</code>

thankyou once more..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Welcome to the Ranch sarbon!
You'll find this forum a great place to seek help on JDBC, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
Ok, I've stick to the rules and changed the display name.Any help on my previous post??
Regards
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30384
    
150

I assume you are calling connectToDB() somewhere? Looking quickly, your code seems fine. Try putting System.out.println() statements in to find out exactly which line is throwing the null pointer.
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
Also, just a thought, use a PreparedStatment instead, it's safer.

One more thing...since this is a null pointer exception, I believe this is your culprit:
if (event.getActionCommand().equals(submit))
it is looking for a submit object. submit needs to be in quotes.
if (event.getActionCommand().equals("submit)")


An egg is a chicken's house!
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
I meant:
if (event.getActionCommand().equals("submit"))

!!!
sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
Hello,
When calling my loadAccounts method i am getting null exceptions

please help me..
thankyou..
sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
Mr.Jason Steele
I will check for "submit", but instead of calling loadAccounts(); if i write someother code i am not getting exceptions, submit is a actionlistener name for button..
but i will see your idea..
thankyou for the post..
sarath bon
Greenhorn

Joined: Jan 30, 2004
Posts: 6
I cannot use
if (event.getActionCommand().equals("submit"));
with "submit"..
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
getActionCommand() is looking for a String. So, if your button text is 'submit', then it will be ok.
When you have
if(event.getActionCommand().equals(submit))
Pseudo says:
if(the button's text is equal to "submit") then loadAccounts().
So if your button displays "submit" as its text, then it should be:
if(event.getActionCommand().equals("submit"))
if your button displays "Load Accounts" as its text, then is should be:
if(event.getActionCommand().equals("Load Accounts"))
Also, trap to see if your actually making it inside the if block like:

if you get the message then you know your ok and it is probably something in your loadAccouts() method.
I hope I have helped you.
Jason Steele
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Can this call 'Uname.getText' return null?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in sql syntax (method calling)