Guest User

Untitled

a guest
Jun 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package desguace;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. import desguaceForms.registroVehiculosForm;
  11.  
  12.  
  13. public class registrarVehicDAO {
  14. public void registrarVehic(registroVehiculosForm rvehiculosForm){
  15.  
  16. try {
  17. Class.forName("oracle.jdbc.OracleDriver");
  18. Connection conexion = DriverManager.getConnection(
  19. "jdbc:oracle:thin:@127.0.0.1:1521:TALLER","TALLER","TALLER");
  20. //recogemos el nom_modelo del formulario y comprobamos si existe en la BBDD
  21.  
  22. String existe = ("SELECT ID_MODELO FROM MODELO WHERE NOM_MODELO='"+ rvehiculosForm.getNom_modelo()+"'");
  23. PreparedStatement pst1 = conexion.prepareStatement(existe);
  24. ResultSet rs1 = pst1.executeQuery();
  25. String insertar;
  26. String insertar2;
  27.  
  28. if (rs1== null){
  29. insertar2 = ("INSERT INTO MODELO (id_modelo, nom_modelo) VALUES" +
  30. " (MAX(id_modelo)+1, "+ rvehiculosForm.getNom_modelo()+")");
  31. PreparedStatement pst2 = conexion.prepareStatement(insertar2);
  32. pst2.executeUpdate();
  33. insertar = ("INSERT INTO COCHE (matricula, id_modelo, id_entrada, id_salida) VALUES " +
  34. "(?,(SELECT id_modelo FROM MODELO WHERE id_modelo=MAX(id_modelo)),?,?)");
  35.  
  36. }
  37. else {
  38. insertar = ("INSERT INTO COCHE (matricula, id_modelo, id_entrada, id_salida) VALUES" +
  39. " (?,(SELECT id_modelo FROM MODELO WHERE nom_modelo= "+ rvehiculosForm.getNom_modelo()+"),?,?)");
  40.  
  41. }
  42. PreparedStatement pst =conexion.prepareStatement(insertar);
  43.  
  44. SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
  45.  
  46. Date entrada = sdf.parse(rvehiculosForm.getId_entrada());
  47. Date salida = sdf.parse(rvehiculosForm.getId_salida());
  48.  
  49. java.sql.Date entradaS = new java.sql.Date(entrada.getTime());
  50. java.sql.Date salidaS = new java.sql.Date(salida.getTime());
  51.  
  52. pst.setString(1, rvehiculosForm.getMatricula());
  53. //pst.setInt(2, Integer.parseInt(rvehiculosForm.getId_modelo()));
  54. pst.setDate(2, entradaS);
  55. pst.setDate(3, salidaS);
  56.  
  57. pst.executeUpdate();
  58.  
  59. pst.close();
  60.  
  61. conexion.close();
  62. }catch (Exception e){
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. //fin clase
  68. }
Add Comment
Please, Sign In to add comment