Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Conexion {
- Connection conexion=null;
- /*Conectamos a la Base de Datos*/
- public Conexion(){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
- }catch(Exception excepcion){
- excepcion.printStackTrace();
- }
- }
- /*Devolvemos la conexion*/
- public Connection getConnection(){
- return conexion;
- }
- /*Cerramos la conexión a la Base de Datos*/
- public void desconectar(){
- try{
- conexion.close();
- }catch(Exception excepcion){
- excepcion.printStackTrace();
- }
- }
- }
- public void addArtista(ArtistaVO artistaVO){
- miLogica.validarRegistroArtista(artistaVO);
- }
- public class ArtistaVO {
- private int idArtista;
- private String nombreArtista;
- public int getIdArtista(){
- return idArtista;
- }
- public void setIdArtista(int idArtista){
- this.idArtista=idArtista;
- }
- public String getNombreArtista(){
- return nombreArtista;
- }
- public void setNombreArtista(String nombreArtista){
- this.nombreArtista=nombreArtista;
- }
- }
- public void addArtista(ArtistaVO artistaVO){
- Conexion conexion=new Conexion();
- try{
- //Insertamos los datos del Artista
- PreparedStatement sqlAddArtist=conexion.getConnection().prepareStatement("INSERT into artist values(?,?)");
- ResultSet resultado=sqlAddArtist.executeQuery();
- while(resultado.next()){
- sqlAddArtist.setString(1, artistaVO.getNombreArtista());
- sqlAddArtist.setInt(2, getMaxId()+1);
- }
- JOptionPane.showMessageDialog(null, "Se ha añadido exitosamente","Información",JOptionPane.INFORMATION_MESSAGE);
- sqlAddArtist.close();
- conexion.desconectar();
- }catch(SQLException excepcion){
- System.out.println(excepcion.getMessage());
- JOptionPane.showMessageDialog(null,"No se registro", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- public int getMaxId(){
- int id=0;
- Conexion conexion=new Conexion();
- try{
- Statement sqlMaxId=conexion.getConnection().createStatement();
- ResultSet resultado=sqlMaxId.executeQuery("SELECT max(ArtistId) from artist");
- if(resultado.next()){
- id=resultado.getInt(0);
- }
- conexion.desconectar();
- sqlMaxId.close();
- }catch(SQLException excepcion){
- System.out.println(excepcion.getMessage());
- }
- return id;
- }
- public void actionPerformed(ActionEvent evento) {
- if(evento.getSource()==botonAñadir){
- try{
- ArtistaVO artistaVO=new ArtistaVO();
- artistaVO.setNombreArtista(campoTextoArtista.getText());
- miCoordinador.addArtista(artistaVO);
- }catch(Exception excepcion){
- JOptionPane.showMessageDialog(null, "Error al añadir Artista", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement