[英]Prepared statement with SELECT returns 0
我創建了此方法,使用藝術家的名字從藝術家中選擇ID,但它返回0。
public int getIdArtist(){
Conexion connection=new Conexion();
ArtistVO artistVO=new ArtistVO();
int id=0;
try{
String cSql="SELECT ArtistId from artist where Name=?";
PreparedStatement sqlArtistId=connection.getConnection().prepareStatement(cSql);
sqlArtistId.setString(1, artistVO.getNameArtist());
ResultSet result=sqlArtistId.executeQuery();
System.out.println(id); //debug
while(result.next()){
id=result.getInt("ArtistId");
}
return id;
}catch(SQLException exception){
exception.printStackTrace();
return 0;
}
}
我已經嘗試過一些打印,以檢查它在哪里失敗,並且在executeQuery()
之后就失敗了; 知道為什么會這樣嗎?
public class ArtistVO {
private int idArtist;
private String nameArtist;
public int getIdArtist(){
return idArtist;
}
public void setIdArtist(int idArtist){
this.idArtist=idArtist;
}
public String getNameArtist(){
return nameArtist;
}
public void setNameArtist(String nameArtist){
this.nombreArtist=nombreArtist;
}
}
這是我設置藝術家名稱的代碼的一部分,其余方法無關緊要:
String nameArtist=chooseArtist.getSelectedItem().toString();
System.out.println(nameArtist); //debug
artistVO.setNameArtist(nameArtist);
它從組合框獲取名稱,並使用打印顯示名稱,顯示,設置和獲取名稱,這不是問題。
#2:更新
此方法用於添加相冊sqlAddAlbum.setInt(3, getIdArtist());
應該使用getIdArtist
的ID,但根據您的第一次更新,它以紅色下划線顯示,我不知道如何調整它:
public void addAlbum(AlbumVO albumVO){
Conexion connection=new Conexion();
try{
String cSql="INSERT into album values(?,?) where ArtistId=?";
PreparedStatement sqlAddAlbum=connection.getConnection().prepareStatement(consultaSql);
sqlAddAlbum.setInt(1, getMaxIdAlbum());
sqlAddAlbum.setString(2, albumVO.getNameAlbum());
sqlAddAlbum.setInt(3, getIdArtist());
Integer affectedRows=sqlAddAlbum.executeUpdate();
if(affectedRows>0){
JOptionPane.showMessageDialog(null, "Album has been inserted","Information",JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "Album has not been inserted", "Error", JOptionPane.ERROR_MESSAGE);
}
sqlAddAlbum.close();
connection.closeConnection();
}catch(SQLException exception){
excepcion.printStackTrace();
JOptionPane.showMessageDialog(null, "Album could not be added", "Error", JOptionPane.ERROR_MESSAGE);
}
}
@Rafael Osipov的最新更新需要
String nameArtist=chooseArtist.getSelectedItem().toString();
int artistId = getIdArtist(nameArtist);
System.out.println("artistId = " + artistId);
我粘貼了該代碼以顯示如何從組合框獲取藝術家的姓名,不需要更改或調整它,打印行是調試行,因此也不需要更改。
就像我說的那樣,問題出在addAlbum()
因為您對getIdArtist()
進行的第一個更改以紅色突出顯示了對該方法的調用,並且我不知道如何解決它,我希望我自己弄清楚現在。
在線上:
ArtistVO artistVO=new ArtistVO();
您正在通過ArtistV0
類創建一個新對象。 由於我們沒有此類的代碼,因此我假設該對象內部為空,並且在創建該對象時未設置藝術家名稱。
你打電話時:
artistVO.getNameArtist()
在剛剛創建的對象上,您將得到一個空字符串,並且您的sql如下所示:
SELECT ArtistId from artist where Name=''
而且,當您執行sql時,不會獲得藝術家姓名為空的記錄,因為在artist
表中沒有此類記錄。
更新
請查看您的getIdArtist()
方法。 即使您在此方法之外選擇了歌手姓名,也不要在此方法內使用此所選歌手姓名。 您正在此方法內創建一個新對象,並且該對象的藝術家名稱為空。
將外部填充的 ArtistVO
對象作為參數傳遞給此方法並使用它:
public int getIdArtist(ArtistVO artistVO){
Conexion connection=new Conexion();
int id=0;
try{
String cSql="SELECT ArtistId from artist where Name=?";
PreparedStatement sqlArtistId=connection.getConnection().prepareStatement(cSql);
sqlArtistId.setString(1, artistVO.getNameArtist());
ResultSet result=sqlArtistId.executeQuery();
System.out.println(id); //debug
while(result.next()){
id=result.getInt("ArtistId");
}
return id;
}catch(SQLException exception){
exception.printStackTrace();
return 0;
}
}
請注意,java語句區分大小寫。 我不確定chooseArtist.getSelectedItem()
返回什么,因此我再次更新了您的方法。
檢查下面的代碼。
以新方式聲明getIdArtist()
:
public int getIdArtist(String artistName){ Conexion connection=new Conexion(); int id=0; try{ String cSql="SELECT ArtistId from artist where Name=?"; PreparedStatement sqlArtistId=connection.getConnection().prepareStatement(cSql); sqlArtistId.setString(1, artistName); ResultSet result=sqlArtistId.executeQuery(); System.out.println(id); //debug while(result.next()){ id=result.getInt("ArtistId"); } return id; }catch(SQLException exception){ exception.printStackTrace(); return 0; } }
要在您的代碼中使用此方法:
String nameArtist=chooseArtist.getSelectedItem().toString(); int artistId = getIdArtist(nameArtist); System.out.println("artistId = " + artistId);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.