gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:36:40 27 4
gpt4 key购买 nike

我正在用 Java 创建一个软件,它应该能够注册艺术家、专辑和轨道。

一切都连接到 MySQL DB ,该软件只需要艺术家姓名,我向您展示我创建的方法。抱歉,很多名字都是西类牙语,但是这个是给我的学校的,我不能使用太多英语单词。我想你能理解

这是类(class)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();
}
}

}

这是类(class)Coordinador ,这个类用于实现不同类之间的并集(遵循模型MVC),我把添加艺术家的部分粘贴给你,其他的只是与 View 相关:

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

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

该方法用于创建Artist 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);
}
}

最佳答案

您在绑定(bind)值之前执行数据库查询

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

关于java - 在Java中添加艺术家(人)时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com