Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class Conexion {
  2.  
  3. Connection conexion=null;
  4.  
  5. /*Conectamos a la Base de Datos*/
  6.  
  7. public Conexion(){
  8. try{
  9. Class.forName("com.mysql.jdbc.Driver");
  10. conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
  11. }catch(Exception excepcion){
  12. excepcion.printStackTrace();
  13. }
  14. }
  15.  
  16. /*Devolvemos la conexion*/
  17.  
  18. public Connection getConnection(){
  19. return conexion;
  20. }
  21.  
  22. /*Cerramos la conexión a la Base de Datos*/
  23.  
  24. public void desconectar(){
  25. try{
  26. conexion.close();
  27. }catch(Exception excepcion){
  28. excepcion.printStackTrace();
  29. }
  30. }
  31.  
  32. }
  33.  
  34. public void addArtista(ArtistaVO artistaVO){
  35. miLogica.validarRegistroArtista(artistaVO);
  36. }
  37.  
  38. public class ArtistaVO {
  39.  
  40. private int idArtista;
  41. private String nombreArtista;
  42.  
  43. public int getIdArtista(){
  44. return idArtista;
  45. }
  46.  
  47. public void setIdArtista(int idArtista){
  48. this.idArtista=idArtista;
  49. }
  50.  
  51. public String getNombreArtista(){
  52. return nombreArtista;
  53. }
  54.  
  55. public void setNombreArtista(String nombreArtista){
  56. this.nombreArtista=nombreArtista;
  57. }
  58. }
  59.  
  60. public void addArtista(ArtistaVO artistaVO){
  61.  
  62. Conexion conexion=new Conexion();
  63.  
  64. try{
  65. //Insertamos los datos del Artista
  66. PreparedStatement sqlAddArtist=conexion.getConnection().prepareStatement("INSERT into artist values(?,?)");
  67. ResultSet resultado=sqlAddArtist.executeQuery();
  68. while(resultado.next()){
  69. sqlAddArtist.setString(1, artistaVO.getNombreArtista());
  70. sqlAddArtist.setInt(2, getMaxId()+1);
  71. }
  72. JOptionPane.showMessageDialog(null, "Se ha añadido exitosamente","Información",JOptionPane.INFORMATION_MESSAGE);
  73. sqlAddArtist.close();
  74. conexion.desconectar();
  75.  
  76. }catch(SQLException excepcion){
  77. System.out.println(excepcion.getMessage());
  78. JOptionPane.showMessageDialog(null,"No se registro", "Error", JOptionPane.ERROR_MESSAGE);
  79. }
  80. }
  81.  
  82. public int getMaxId(){
  83. int id=0;
  84. Conexion conexion=new Conexion();
  85.  
  86. try{
  87. Statement sqlMaxId=conexion.getConnection().createStatement();
  88. ResultSet resultado=sqlMaxId.executeQuery("SELECT max(ArtistId) from artist");
  89.  
  90. if(resultado.next()){
  91. id=resultado.getInt(0);
  92. }
  93.  
  94. conexion.desconectar();
  95. sqlMaxId.close();
  96. }catch(SQLException excepcion){
  97. System.out.println(excepcion.getMessage());
  98. }
  99.  
  100. return id;
  101. }
  102.  
  103. public void actionPerformed(ActionEvent evento) {
  104.  
  105. if(evento.getSource()==botonAñadir){
  106.  
  107. try{
  108. ArtistaVO artistaVO=new ArtistaVO();
  109. artistaVO.setNombreArtista(campoTextoArtista.getText());
  110.  
  111. miCoordinador.addArtista(artistaVO);
  112. }catch(Exception excepcion){
  113. JOptionPane.showMessageDialog(null, "Error al añadir Artista", "Error", JOptionPane.ERROR_MESSAGE);
  114. }
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement