2016-04-27 46 views
0

我正在用Java创建一个应该能够注册艺术家,专辑和曲目的软件。在Java中添加艺术家(人物)时出错

一切都连接到MySQL DB,软件只需要艺术家的名字,我告诉你我创建的方法。对不起,许多名字都是西班牙文,但这 是为我的学校,我不能使用太多的英文单词。我想你可以把它理解

这是类Conexión这使得连接到数据库:

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(); 
    } 
} 

} 

这是类Coordinador,这个类是用来做不同类之间的联盟(以下模型MVC),我粘贴您添加艺术家的一部分,其他的都只是涉及到的意见:

public void addArtista(ArtistaVO artistaVO){ 
    miLogica.validarRegistroArtista(artistaVO); 
} 

ArtistaVO在那里我得到建立和s艺术家的等:

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); 
    } 
} 

此方法被用来创建艺术家ID:

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); 
     } 
    } 

} 

我得到的错误是Error al añadir Artista(错误添加艺术家),我不知道失败的位置。

这是例外,我得到:

java.lang.NullPointerException 
at controlador.Coordinador.addArtista(Coordinador.java:108) 
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

这是方法validarRegistroArtista

public void validarRegistroArtista(ArtistaVO artistaVO){ 
    ArtistaDAO artistaDAO; 

    if(artistaVO.getIdArtista()>=0){ 
     artistaDAO=new ArtistaDAO(); 
     artistaDAO.addArtista(artistaVO); 
    }else{ 
     JOptionPane.showMessageDialog(null, "Debe de introducirse algún numero de ID", "Advertencia", JOptionPane.WARNING_MESSAGE); 
    } 
} 
+0

错误在行'Coordinador.java:108'并且是'NullPointerException' ...原因?我们应该看到代码:'miLogica.validarRegistroArtista(artistaVO);'但我敢打赌'artistaVO == null' ....还有... http://es.stackoverflow.com –

+0

刚刚添加了方法 – NeoChiri

+0

@最低点似乎是正确的...按照步骤,让我们知道,也检查重复的问题,一个NPE只需要本地化....最后请**告诉你的老师在西班牙的法人生活我们用英文写代码... ** –

回答

3

绑定值

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); 
     } 

这应该之前,您正在执行的数据库查询看起来像这样:

PreparedStatement sqlAddArtist=conexion.getConnection().prepareStatement("INSERT into artist values(?,?)"); 
sqlAddArtist.setString(1, artistaVO.getNombreArtista()); 
sqlAddArtist.setInt(2, getMaxId()+1); 
sqlAddArtist.execute();