2012-09-30 2 views
0

자바에서 내 sql 데이터베이스의 데이터를 하나만 추출하려고합니다. 내가 resultSet을 사용하려고했지만 int 변수에 첫 번째 행을 exctract하려는 경우 결과 집합에 내용이 없습니다. 여기 Java sql ResultSet에 데이터가 없음

당신을 "이동"로 설정 결과에 next() 메소드를 호출해야 내 코드

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

답변

2

입니다 반복자 :

... 
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 
... 

그리고 보안 및 성능 향상을 응용 프로그램에 대한 중요한 경우

준비된 문장을 사용하는 것도 고려해야합니다.

+0

오, 고마워요. 내 오류였습니다! .next()를 잊어 버렸습니다. – Sebastien

0

필요도 또한

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 

즉 결과를 테스트해야합니다) (다음을 사용하는 등 ' "+ 아티스트 +"' " 오류가하는 경향이, 예를 들면 작가는 견적이 포함 된 경우" ' " 대부분의 DB에서 Sql 오류가 발생합니다 .SMS 매개 변수를 사용하여 지원합니다.

관련 문제