aspose file tools*
The moose likes JDBC and the fly likes How to insert data into a MySQL database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to insert data into a MySQL database" Watch "How to insert data into a MySQL database" New topic
Author

How to insert data into a MySQL database

charles macharia
Greenhorn

Joined: Oct 04, 2012
Posts: 10
hello,
i'm trying to insert data into a database called personaldetails but no change appears on the database
here is the code is use:


please help me out
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Welcome to the ranch.

Please use code tags when posting code and is this the best forum for a DB question? I'll ask the mods to move it for you.

Are you getting any error messages?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Welcome to the Ranch, Charles !

Tony Docherty wrote:Welcome to the ranch.

Please use code tags when posting code and is this the best forum for a DB question? I'll ask the mods to move it for you.

....


We moved your post to our JDBC forum, and have added code tags.
Please have a look at the formatting options that are available to you. They are there to make your posts more readable, and will help to get better responses.

Good luck with the resolution of your problem.

Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Why are you indexing parameters of you statement as 2-14 when it should be 1-13? You have 13 parameters to set, and as you could see in the API indexing starts at value 1. That should throw an exception.


The quieter you are, the more you are able to hear.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Kemal Sokolovic wrote:Why are you indexing parameters of you statement as 2-14 when it should be 1-13? You have 13 parameters to set, and as you could see in the API indexing starts at value 1. That should throw an exception.

Exactly. My bet is that your catch clause hides the exception from you.

Verify that you report exceptions correctly. If you're still stuck, post more of your code (especially the catch clause).
charles macharia
Greenhorn

Joined: Oct 04, 2012
Posts: 10
no the program is running fine but it catches an error when i attempt to insert data into the db
and i have started from 2 because im not sure if the primary key is also included
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

if the primary key is a auto sequence column, you don't need to include it in your insert statement at all, it magically happens on the database whenever you do an insert.

You always need to start filling in your prepared statement from index 1.

The exception will only be thrown when you try to run the insert statement against the DB.
And "only throwing an exception when..." is not running fine it is throwing an exception. You need to include the full exception trace for us, as it often contains really really helpful stuff.
charles macharia
Greenhorn

Joined: Oct 04, 2012
Posts: 10
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

we need to see the contents of the error from a x.printStackTrace() (to standard error your console if you are using eclipse and other IDE's i expect) or if you want it in your error pane x.getStackTrace, work through it and build up a string buffer.
A minimum you should be including x.getMessage() into your dialog.

So people were correct, your code is hiding the details of the exception.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

charles macharia wrote:no the program is running fine but it catches an error when i attempt to insert data into the db
and i have started from 2 because im not sure if the primary key is also included


Well, you certainly can't catch an exception unless you execute the code that produces it - in this case the code for inserting new data into your database.

I think you didn't get how the PreparedStatement works. When you set those parameters (1-13), indexes don't represent columns in the database but values that will be set instead of those question marks in your sql query you keep in sql String (I trivialized the explanation, but that's how you can think of it). No matter what the order of columns is in the database, you're setting parameters based on your sql query. You might as well write something like this (supposing other columns can contain null values):

That will insert a new row in your database table with values set above, and other columns having null values.

On the other hand, message like "Error on database operation,Updation failure" is not very suitable for this kind of situation. To you as developer it says nothing about where the problem occured, and to other users it's "too technical" so you need to take care of that too in your applications.

And one final note: Never, ever hide the exceptions in your applications. You better crash it, at least you'll know it's not working at all.
charles macharia
Greenhorn

Joined: Oct 04, 2012
Posts: 10
Kemal Sokolovic wrote:
charles macharia wrote:no the program is running fine but it catches an error when i attempt to insert data into the db
and i have started from 2 because im not sure if the primary key is also included


Well, you certainly can't catch an exception unless you execute the code that produces it - in this case the code for inserting new data into your database.

I think you didn't get how the PreparedStatement works. When you set those parameters (1-13), indexes don't represent columns in the database but values that will be set instead of those question marks in your sql query you keep in sql String (I trivialized the explanation, but that's how you can think of it). No matter what the order of columns is in the database, you're setting parameters based on your sql query. You might as well write something like this (supposing other columns can contain null values):

That will insert a new row in your database table with values set above, and other columns having null values.
i seem to have done as you have told me but no change is observed

On the other hand, message like "Error on database operation,Updation failure" is not very suitable for this kind of situation. To you as developer it says nothing about where the problem occured, and to other users it's "too technical" so you need to take care of that too in your applications.

And one final note: Never, ever hide the exceptions in your applications. You better crash it, at least you'll know it's not working at all.
charles macharia
Greenhorn

Joined: Oct 04, 2012
Posts: 10
i have done all you have advised me to do but no change is observed in the database
wendy gibbons please explain more
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I'm going to assume that your INSERT statement was actually executed and that no exceptions were thrown.

In which case I'm going to guess that your database is Microsoft Access and that you didn't commit the change or close the database connection. That would result in what you saw. But Access or not, you should still close the database connection after you're finished using it.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

charles macharia wrote:i have done all you have advised me to do but no change is observed in the database
wendy gibbons please explain more


here is a very useful page all about stack traces, how to read them what they contain etc.
ten top tips for stack traces

read point 9 first.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to insert data into a MySQL database