This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Insert for mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Insert for mysql" Watch "Insert for mysql" New topic

Insert for mysql

Jeremiah Hoyle

Joined: Feb 01, 2012
Posts: 4
Ok I know I have a ton to learn with java, I have been doing a homework assignment and for over a week now I have been trying to figure out how to get the insert to work for my assignment with no luck. Here is the code I am hoping someone has some pointers for me.

I keep getting the error code when I run it and I have triple checked my database connection and I cannot find a problem there.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Well, first of all you have no idea what caused your error, because you put in some code which ignores all the information which is built into the Exception and simply says "There was an error".
So start by replacing that by something like

I'm willing to bet that the actual problem is that you have fouled up your INSERT statement in some way, but it's impossible to tell how from your code. It's just a thicket of SQL quotes and Java string quotes which are easy to get wrong. I strongly suggest you check out PreparedStatement as a way to simplify that code. Of course it also might be something else like "level" being a reserved word, but once you fix your error reporting you should see a proper error message describing your problem.
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Once you have done as @paul suggests, and have it working, make sure you try
entering names such as O'Hara

Hint: your current code will blow up with ugly errors on a lot of Irish and Italian names.
Rumesh Eranga
Ranch Hand

Joined: Jan 26, 2011
Posts: 34
try executeUpdate() method...

It is better if you can post the error that you getting mate..
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Rumesh Eranga wrote:try executeUpdate() method...

Why do you suggest that change? I do not believe it will impact the OP's problem.
I agree. Here's the link:
subject: Insert for mysql
It's not a secret anymore!