• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to insert data into a MySQL database

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have done all you have advised me to do but no change is observed in the database
wendy gibbons please explain more
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic