[英]Java sql ResultSet has no data
我想從java中的sql數據庫中只提取一個數據。 我試圖使用resultSet,但是當我想將第一行exctract為int Variables時,它表示resultSet沒有內容。
這是我的代碼
try {
statement = connexion.createStatement();
statementArtist = connexion.createStatement();
String artist = "Mac Miller";
ResultSet resultat = statement.executeQuery("USE albums SELECT Album.numero_artist FROM Album INNER JOIN Artist ON Album.num_artist = Artiste.num_artist where name like '"+artist+"'");
int result = resultat.getInt(1); // Here is the problem
String query = "USE albums INSERT INTO dbo.Album(Album.num_artist, title, price, genre, date, home, image) VALUES("
+ result
+ ", '"
+ title
+ "', "
+ price
+ ", '"
+ genre
+ "', '"
+ date
+ "', '"
+ home
+ "', '"
+ image
+ "')";
statement.executeUpdate(query);
你應該在結果集上調用next()
方法來“移動”迭代器:
...
ResultSet resultat = statement.executeQuery("USE albums SELECT Album.numero_artist FROM Album INNER JOIN Artist ON Album.num_artist = Artiste.num_artist where name like '"+artist+"'");
resultat.next();
int result = resultat.getInt(1); // Here is the problem
...
如果安全性和更好的性能對您的應用程序很重要,您還應該考慮使用預准備語句。
需要使用next(),也應該測試結果即
ResultSet resultat = statement.executeQuery("USE albums SELECT Album.numero_artist FROM Album INNER JOIN Artist ON Album.num_artist = Artiste.num_artist where name like '"+artist+"'");
if (resultat.next()) {
int result = resultat.getInt(1); // Here is the problem
也像“+ + artist +”'“容易出錯,例如,如果藝術家包含引號”'“你會得到大多數DB的Sql錯誤。最高使用Sql參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.