When I run the following code I get an error:
java.lang.String cannot be cast to java.lang.Integer
This is my code:
try{
int rows = jTable1.getRowCount();
String myUrl = "jdbc:mySql://localhost:3306/hospital";
Connection con = DriverManager.getConnection(myUrl,"root","");
con.setAutoCommit(false);
String query = "Insert into `test and treatment_has_admission`(`Test and Treatment_id`,`Admission_a_id`,`Test_Result`,`Treatment_Result`,`Date`)"+" values (?, ?, ?, ?, ?)";
PreparedStatement pst = con.prepareStatement(query);
for(int row=0; row<rows; row++){
int TestTreatmentID = (int) jTable1.getValueAt(row, 1);
String test_result = (String) jTable1.getValueAt(row, 2);
String treatment_result = (String) jTable1.getValueAt(row, 3);
String Date = (String) jTable1.getValueAt(row, 4);
pst.setInt(1,TestTreatmentID);
pst.setInt(2,Integer.parseInt(t2.getText()));
pst.setString(3, test_result);
pst.setString(4, treatment_result);
pst.setString(5, Date);
pst.addBatch();
}
pst.executeBatch();
con.commit();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Error: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
Why am I receiving such an error? Please help me to resolve it.