• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rethish Varma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post in a ll uppercase. I have adjusted the subject title for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic