wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes Unable to insert data into mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Unable to insert data into mysql " Watch "Unable to insert data into mysql " New topic

Unable to insert data into mysql

ravi ramesh

Joined: Sep 08, 2006
Posts: 1
can any one of you tell me why i am unable to insert values into MYsql.

I wrote a jsp page where i am trying to insert data through Java bean into Mysql.

i am ableprint all fileds in jsp page but in mysql all the columns are Zero.
when i try to re enter the same valuse its giving me Duplicate error

'ERROR 1062 (23000): Duplicate entry '0' for key 2'

the code in Bean is following
String sqlstmt="insert into user_details(fname,lname,uname,pwd,rpwd,age,email,remail,phone,mobile,addr) values(fname='"+fname+"',lname='"+lname+"',uname='"+uname+"',pwd='"+pwd+"',rpwd='"+rpwd+"',age="+age+",email='"+email+"',remail='"+remail+"',phone="+phone+",mobile="+mobile+",addr='"+addr+"')";

int i=st.executeUpdate(sqlstmt);

can any body please help me on the same.
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

I guess you are trying to insert duplicate values for some UNIQUE column.
What is the schema of the table? Also, what are the values that are being passed as part of the query?

[My Blog] [JavaRanch Journal]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Moving to the JDBC forum where database questions go.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rashid Mayes
Ranch Hand

Joined: Jan 11, 2006
Posts: 160
Yep, I would have to suspect the same. Use one of the MySQL gui tools, Query Browser or Admistrator, and look at the details of your table. You should be able to see the second key and determine the duplicate value.

If you do not have a gui tool use the command line client. Below are the helpful commands.

show keys from tableName
show columds from tableName

If you have enabled logging, you can check the log to determine which values caused the error.

After you determine the duplication column/value, change the code to use a prepared statement. In fact, I am guessing there are more blocks of code that should be using prepared statements

Rashid Mayes
http://www.hostj2me.com/ - http://www.worlddeveloper.org/
I agree. Here's the link: http://aspose.com/file-tools
subject: Unable to insert data into mysql
It's not a secret anymore!