This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to insert data into mysql

 
ravi ramesh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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+"')";

System.out.println(sqlstmt);
int i=st.executeUpdate(sqlstmt);

can any body please help me on the same.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JDBC forum where database questions go.
 
Rashid Mayes
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic