/*Update Department method was created for Update a record from Department, folowing DepartmentID*/ public static void updateDepartment(Department department) throws SQLException, ClassNotFoundException { Connection conn = EmpConnection.getConnection(); StringBuilder query = new StringBuilder(); query.append("use QLNS_JAVA2 UPDATE [dbo].[Departments] SET [DepartmentName] = ?"); query.append(" WHERE [DepartmentID] = ?"); PreparedStatement pStmt = conn.prepareStatement(query.toString()); pStmt.setString(1, department.getDepartmentName()); pStmt.setInt(2, department.getDepartmentID()); pStmt.executeUpdate(); EmpConnection.closePreparedStatement(pStmt); EmpConnection.closeConnection(conn); }
public static int insertDepartment(Department department) throws ClassNotFoundException, SQLException { int result = 0; Connection conn = EmpConnection.getConnection(); StringBuilder query = new StringBuilder(); query.append("use QLNS_JAVA2 INSERT INTO [dbo].[Departments]( [DepartmentName] ) VALUES(?) "); PreparedStatement pStmt = conn.prepareStatement(query.toString()); pStmt.setString(1, department.getDepartmentName()); pStmt.executeUpdate(); // Get Max ID and Return/ pStmt = conn.prepareStatement("use QLNS_JAVA2 SELECT MAX(DepartmentID) AS MaxID FROM Departments"); ResultSet rs = pStmt.executeQuery(); rs.next(); result = rs.getInt("MaxID"); EmpConnection.closePreparedStatement(pStmt); EmpConnection.closeConnection(conn); return result; // de minh chi cho ban cho ma chay bi loi ne }