public Personal persona(int id) { String query = "SELECT * FROM `" + nameTable() + "` WHERE " + nameField()[0] + "=?;"; personal = new Personal(); try { Connection con = Conexion.createConnection(); PreparedStatement preparedStatement = con.prepareStatement(query); preparedStatement.setString(1, id + ""); ResultSet p = preparedStatement.executeQuery(); if (p.next()) { personal.id = p.getInt(nameField()[0]); personal.nombre = p.getString(nameField()[1]); personal.correo = p.getString(nameField()[2]); personal.seguroSocial = p.getString(nameField()[3]); personal.telefono = p.getLong(nameField()[4]); personal.direccion = p.getString(nameField()[5]); personal.lugarnaciemiento = p.getString(nameField()[6]); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); personal.fechanacimeinto = formatter.parse(p.getString(nameField()[7])); personal.puesto_id = p.getInt(nameField()[8]); personal.activo = p.getInt(nameField()[9]); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } catch (Exception e) { } return personal; }
public static List<Personal> listaPersonal() { String query = "SELECT * FROM " + nameTable(); List<Personal> personas = new ArrayList<Personal>(); try { Connection con = Conexion.createConnection(); PreparedStatement preparedStatement = con.prepareStatement(query); ResultSet p = preparedStatement.executeQuery(); while (p.next()) { Personal personal = new Personal(); personal.id = p.getInt(nameField()[0]); personal.nombre = p.getString(nameField()[1]); personal.correo = p.getString(nameField()[2]); personal.seguroSocial = p.getString(nameField()[3]); personal.telefono = p.getLong(nameField()[4]); personal.direccion = p.getString(nameField()[5]); personal.lugarnaciemiento = p.getString(nameField()[6]); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(p.getString(nameField()[7])); personal.fechanacimeinto = formatter.parse(p.getString(nameField()[7])); personal.puesto_id = p.getInt(nameField()[8]); personal.activo = p.getInt(nameField()[9]); personas.add(personal); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } catch (Exception e) { } return personas; }
private int Update() { int id = -1; if (personal != null) { String query = "UPDATE " + nameTable() + " SET " + nameField()[1] + " = ?, " + nameField()[2] + " = ?, " + nameField()[3] + " = ?, " + nameField()[4] + " = ?, " + nameField()[5] + " = ?, " + nameField()[6] + " = ?, " + nameField()[7] + " = ?, " + nameField()[8] + " = ? " + " WHERE " + nameField()[0] + " = ? ;"; try { Connection con = Conexion.createConnection(); PreparedStatement insertar = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); insertar.setString(1, personal.nombre); insertar.setString(2, personal.correo); insertar.setString(3, personal.seguroSocial); insertar.setLong(4, personal.telefono); insertar.setString(5, personal.direccion); insertar.setString(6, personal.lugarnaciemiento); insertar.setDate(7, new java.sql.Date(personal.fechanacimeinto.getTime())); insertar.setInt(8, personal.puesto_id); insertar.setInt(9, personal.id); insertar.executeUpdate(); ResultSet generate = insertar.getGeneratedKeys(); if (generate.next()) { id = generate.getInt(1); } con.close(); } catch (SQLException e) { System.out.println(e.getMessage()); JOptionPane.showMessageDialog( null, "no se pudo Actualizar " + "verifique su conexion a la base de datos", "Inntepco", JOptionPane.ERROR_MESSAGE); } } return id; }
private int insertar() { int id = -1; if (personal != null) { String query = "insert into " + nameTable() + " " + Arrays.toString(nameField()).replace('[', '(').replace(']', ')') + " values (null,?,?,?,?,?,?,?,?,1)"; try { Connection con = Conexion.createConnection(); PreparedStatement insertar = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); insertar.setString(1, personal.nombre); insertar.setString(2, personal.correo); insertar.setString(3, personal.seguroSocial); insertar.setLong(4, personal.telefono); insertar.setString(5, personal.direccion); insertar.setString(6, personal.lugarnaciemiento); insertar.setDate(7, new java.sql.Date(personal.fechanacimeinto.getTime())); insertar.setInt(8, personal.puesto_id); insertar.executeUpdate(); ResultSet generate = insertar.getGeneratedKeys(); generate.next(); id = generate.getInt(1); con.close(); } catch (SQLException e) { System.out.println(e.getMessage()); JOptionPane.showMessageDialog( null, "no se pudo insertar " + "verifique su conexion a la base de datos", "Inntepco", JOptionPane.ERROR_MESSAGE); } } return id; }