I am totally new to JDBC programming using MySQL as the DB.
I have trouble updating values in the employee_bak table, when there is a key match with employee table.
import java.sql.*;
public class Employee {
public static Connection con;
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/training","root", "");
java.util.Date util_today = new java.util.Date();
java.sql.Date sql_today = new java.sql.Date(util_today.getTime());
Employee emp = new Employee();
// Below statement should be executed only once
emp.createEmployee("employee");
emp.createEmployee("employee_bak");
//pre-populating employee table
//emp.insertEmployee("employee",1004,"John Smith",5671.34f,sql_today);
//emp.insertEmployee("employee",1005,"Jane Doe",5000.86f, sql_today);
//emp.insertEmployee("employee",1034,"Mary Jane",6784.57f, sql_today);
//emp.insertEmployee("employee",1054,"James Seymour",4675.47f, sql_today);
//emp.insertEmployee("employee",1258,"Philip Rogue",5848.33f, sql_today);
//pre-populating employee_bak table
emp.insertEmployee("employee_bak",1004,"Jack Hand",5252.4f,sql_today);
emp.insertEmployee("employee_bak",1034,"Mary Beth",5000.42f,sql_today);
emp.insertEmployee("employee_bak",1258,"Phil Hersh",3500.65f,sql_today);
emp.copyEmployeeTable(); // I can make it to this call, but get stuck inside
con.close();
}
catch(Exception e) { e.printStackTrace();}
}
public int createEmployee(String tname1) throws SQLException{
Statement stmt = con.createStatement();
int count = stmt.executeUpdate("CREATE TABLE " +tname1+" (emp_id INT, name VARCHAR(20), salary FLOAT, "+"doj DATE)");
return count;
}
public int insertEmployee(String tname1, int emp_id1, String name1, float salary1, java.sql.Date doj1) throws SQLException{int count = 0;
PreparedStatement p;
p = con.prepareStatement("insert into "+tname1+" (emp_id, name, salary, doj) values(?,?,?,?) ");
p.setInt(1, emp_id1);
p.setString(2, name1);
p.setFloat(3, salary1);
p.setDate(4, doj1);
count = p.executeUpdate();
p.close();
return count;
}
public int copyEmployeeTable() throws SQLException{
int count = 0;
Statement stat = con.createStatement();
String sql = "SELECT emp_id, name, salary, doj FROM employee ORDER BY emp_id";
String sql2 = "SELECT emp_id, name, salary, doj FROM employee_bak ORDER BY emp_id";
ResultSet rs = stat.executeQuery(sql);
ResultSet rs2 = stat.executeQuery(sql2);
boolean found;
while (rs.next()){ //err msg: Operation not allowed after ResultSet closed
found = false;
while (rs2.next()){ //compare the key emp_id, if there is match use an update query to match the values from employee table
if (rs.getInt("emp_id") == rs2.getInt("emp_id")){
found=true;
System.out.println("Found record in backup table\nUpdating record\n\n");
//use update method
count = stat.executeUpdate("Update employee_bak set name="+rs.getString("name")+",salary="+rs.getFloat("salary")+",doj="+rs.getDate("doj")+" WHERE emp_id="+rs.getInt("emp_id")+")");
break;
}
}
}
}
rs.close();
stat.close();
return count;
}
}
This is the code in its entirety.
Created 2 tables: employee and employee_bak.
Successfully pre-populated both tables.
I call the copyEmployeeTable method, if the keys match from both tables then update employee_bak from employee.
I get the err msg: Operation not allowed after ResultSet closed.
Plus, I am not sure if I am doing the executeUpdate properly.