I want to extract a only one data from my sql database in java. I tried to used the resultSet but when i want to exctract the first row into an int Variables, it said that the resultSet have no content.
Here is my code
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);
You should call
next()method on the result set to “move” the iterator:And if the security and better performance is important for your application, you should also consider using prepared statement.