在Java中添加艺术家(人)时出错

时间:2016-04-27 10:09:17

标签: java

我正在用Java创建一个能够注册艺术家,专辑和曲目的软件。

所有内容都与MySQL DB相关联,软件只需要艺术家名称,我会向您展示我创建的方法。对不起,很多名字都是西班牙语,但是这个 是为了我的学校,我不能使用太多的英语单词。我想你可以理解它

这是连接到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);
}

我创建艺术家的get和set的课程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);
    }
}

此方法用于创建艺术家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);
    }
}

1 个答案:

答案 0 :(得分: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();