File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to Update Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to Update Database" Watch "How to Update Database" New topic
Author

How to Update Database

Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Hi everyone,

I'm working with Java, MS Access project. I'm trying to prepare a
Gui for updating.
I need to update the species column of Main tale when it is equal to
the primary key of that table (SVID)
below query and java code gave lot of errors.

here's what i need to do
ex: I need to change the one species of the Main table when SVID
equal to some value.





Please help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Errors? Those error messages are there to explain to you what went wrong. You should read them.

And if you're going to ask somebody else about the error messages, you should show them the error messages.

As for my guess about the problem you're having, I would recommend you use a PreparedStatement in that code. I know you already know about PreparedStatement because you used one in the last piece of code you posted.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

It looks like the reason you are getting errors is because you have an unnecessary ') after the integer value SVID and do not have single quotes around the String species value.

With that aside, you should not using executeUpdate to see if your query even affected the database. You should be using a PreparedStatement instead of a regular Statement. What happens if someone enters "1); delete from Main;//" into the SVID value?


OCJP6, OCWCD5
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Thanks all for reply, Really appreciate your kind help.

I used PreparedStatement



Im getting this error when I add 140 add SVID in my Gui,

Error: java.lang.NullPointerException

Please help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

No, no, no.


As for the error message, it may or may not have anything to do with your bad try at PreparedStatement. Look at the stack trace; it tells you exactly what line of code threw the exception.
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Thank you again and again for kind help, Really appreciate your
kind help.

Can't tell exactly what is the line.

it shows

Error: java.lang.NullPointerException

Here's how my current code looks like

UpdateGui.java


UpdateCode.java


I'm new 2 Java. Please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
In which line are you getting the exception? Put e.printStackTrace() in all catch blocks and post the full stack trace. Just seeing the textual error message doesn't really help us help you.


Ping & DNS - my free Android networking tools app
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
Hi,
Add System.out.printls to your code . Track SVID, Species,DBConn,S variable values by adding System.out.printl's.
Do you know how to debug a Java application ?Which Java editor you are using for right now ? Java IDE provides Debug facilities by default .
check this
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Thank you very much for kind help.

I'm using NetBeans 7.0
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Ulf Dittmer wrote:In which line are you getting the exception? Put e.printStackTrace() in all catch blocks and post the full stack trace. Just seeing the textual error message doesn't really help us help you.


I add e.printStackTrace() to all catch blocks, Even it is difficult for me. Please tell me
is this the correct way of adding e.printStackTrace() to catch blocks.



I also add import java.lang.reflect.*; to the top.
Both newly added parts are highlighted in yellow.

Please help.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
Please tell me is this the correct way of adding e.printStackTrace() to catch blocks.

Does it work?

I also add import java.lang.reflect.*; to the top.

Why did you do that? Are you planning on using reflection? Anyway, the coloring is probably just the IDE's way of telling you that those imports are not used.
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Thanks again for help,

I remove import java.lang.reflect.*;, It's not valuable.

I'm still getting the error.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
If you don't post the stack trace, then there's really no way for us to help you.
Fatih Keles
Ranch Hand

Joined: Sep 01, 2005
Posts: 182
Same issue as it is in your older thread Delete-Records-form-Database
Sander Silva
Greenhorn

Joined: May 15, 2011
Posts: 28
Thank you very much for kind help,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to Update Database