aspose file tools*
The moose likes JDBC and the fly likes Null pointer exception in con.createStatement(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Null pointer exception in con.createStatement();" Watch "Null pointer exception in con.createStatement();" New topic
Author

Null pointer exception in con.createStatement();

Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
i am getting null pointer exception at the following line
stmt = con.createStatement();
int val = stmt.executeUpdate("INSERT INTO WEBDB.HNM VALUES('1','hRISHIKESH')");

my code is as follows->



SCJP 1.5
www.licexpadvice.com
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
to be precise the error is at line number 31
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not a Struts issue; moving to... JDBC?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There are several issues with this code, aside from looking like it was written 10 years ago :(

You're obviously not getting a connection when you think you are--any log messages? Is logging turned on at the DEBUG level?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2500
    
    8

Hint: you load the driver in the scope of a try block. You try to reuse it outside the scope of the try block.
What happens if you move line 31 and associated lines inside the try?


OCUP UML fundamental and ITIL foundation
youtube channel
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Can you post the full stack trace ? Apart from the driver name (I'm not sure about that driver class you have used) that code should work as it did for me.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
we have to use the following directly or some settings have to be made
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Hrishikesh Maluskar wrote:we have to use the following directly or some settings have to be made

It's apparent that you are using a different driver. I think you should check the documentation of that to see how to connect. For mysql's driver this works fine.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The old org.gjt... driver is still provided--it's the same driver.
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
now i am getting this error

Access denied for user 'mysql'@'localhost' (using password: YES)
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

Looks like there is some problem with your mysql password or username.....


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
thanks everybody , i installed mysql again and set the username and password to root.
 
jQuery in Action, 2nd edition
 
subject: Null pointer exception in con.createStatement();