• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ODBC-JDBC connection

 
Hewa Naimanage Sumedha Amalka
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai, guys I was trying to connect a excel sheet data to mysql database. but some of my codes are not working properly

try {
Statement stmnt = connexl.createStatement();
String query = "SELECT * FROM [IJTS$]";
stmnt.execute(query);
ResultSet rsxl = stmnt.getResultSet();

Statement dbsta = conndb.createStatement();
dbsta.execute("SELECT email_address, category FROM emailrec");
ResultSet rsdb = dbsta.getResultSet();
PreparedStatement p = conndb.prepareStatement("INSERT INTO emailrec(email_address,category) VALUES(?,?);");

while (rsxl.next()) {
String excelemail = rsxl.getString(1);
String excelcategory = rsxl.getString(2);

if (excelemail == null && excelcategory == null) {
break;
}

else {
System.out.println(excelemail + " " + excelcategory);

if(rsdb.next()) { // ### the error must be here !!!

System.out.println("Comes Here !");

String dbemail = rsdb.getString("email_address");
String dbcategory = rsdb.getString("category");

if(excelemail.equals(dbemail)&& (!excelcategory.equals(dbcategory))) {

// Prepare a statement to update a record
String sql = "UPDATE emailrec SET category ='"+excelcategory+"' WHERE email_address = '"+excelemail+"';";

// Execute the insert statement
int updateCount = stmnt.executeUpdate(sql);
// updateCount contains the number of updated rows

JOptionPane.showMessageDialog(null," Some of your data have been updated !");
this.dispose();
}

else {
p.setString(1,excelemail);
p.setString(2,excelcategory);

p.execute();
p.close();

JOptionPane.showMessageDialog(null,"Some of your data have been inserted !");
this.dispose();
}
}
}
}


stmnt.close();
dbsta.close();

connexl.close();
conndb.close();

}

catch( Exception e ) {
System.out.println( e );
}
it does not show an exception but dont connet with sql database it seems like never ending loop running on this code
just one record is displaying !
pls help to me !
 
Wei Dai
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>stmnt.execute(query);
> stmnt.executeUpdate(sql)
>int updateCount = stmnt.executeUpdate(sql);
Create another statement object for the 2nd and 3rd sql opeartion, otherwise your rsdb is in unkown status.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic