wood burning stoves 2.0*
The moose likes JDBC and the fly likes insert values into MySQL table if not present or update if present Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "insert values into MySQL table if not present or update if present" Watch "insert values into MySQL table if not present or update if present" New topic
Author

insert values into MySQL table if not present or update if present

Rethish Varma
Greenhorn

Joined: Sep 25, 2009
Posts: 4
Hi,

I am doing a project for academic software and I am facing this problem,I want to insert rollno,name,subjectmarks,division ito a table if the values are not present already and if present then I have to update it.I tried the way given below.I am getting values from an excel sheet.But with this code If I press submit then the value if present also it is inserting again,not checking whether present and not updating ,If I change the subject then it should get updated not inserted,suggest me a solution,

Please send some example coding to do this.

for (int row = 1; row < numberOfRows; row ++ )
{
rollno=sheet.getCell(0,row).getContents().trim();
names=sheet.getCell(1,row).getContents().trim();
marks=sheet.getCell(4,row).getContents().trim();

rs1=stmt.executeQuery("select * from s_"+clas+"_dtl where rollno="+rollno+" and name='"+names+"' and division='"+div+"' and term='"+term+"';");
if(!rs1.next())
{
query1="insert into s_"+clas+"_dtl(rollno,name,division,"+subname1+") values("+rollno+",'"+names+"','"+div+"',"+marks+");";
stmt.executeUpdate(query1);
}
else
{
stmt.executeUpdate("Update s_"+clas+"_dtl set "+subname1+"='"+marks+"' where rollno="+rollno+" and name='"+names+"'and division='"+div+"';");

}System.out.println("+++++++++++++++++++Query:"+query1);

}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Please do not post in a ll uppercase. I have adjusted the subject title for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: insert values into MySQL table if not present or update if present
 
Similar Threads
page refresh problem
Any one have idea regarding this query??
Problem with SQL uqery or DSN
Invalid column index
pagination with jsp