récupération d'une donnée d'une base de donnée dans un String (SELECT FROM WHERE AND)
bonjour, voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
import java.sql.*;
public class Meteo
{
String nmrs1;
public Meteo (Connection connect)
{
try{
if (Main.connect == null)
{
// Chargement du driver ODBC
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver ok");
// Connexion à la base
Main.connect = DriverManager.getConnection("jdbc:mysql://localhost/ferme", "root", "");
System.out.println("connection ok");
Main.connect.createStatement();
}
PreparedStatement ps1;
String queryString1 = "INSERT INTO `ressources` (UT_CLT,NB_TP_RS,NM_RS) VALUES (?,?,?)";
ps1 = Main.connect.prepareStatement(queryString1);
ps1.setString(1, "bonbon");
ps1.setString(2, "1");
ps1.setString(3, "toto");
ps1.executeUpdate();
System.out.println("ecriture test ok");
String nomclient = "bonbon";
String nbtp = "1";
PreparedStatement ps;
String querystring = "SELECT NM_RS FROM `ressources` WHERE UT_CLT= ' " + nomclient + " ' AND NB_TP_RS = ' " + nbtp + " ' ";
ps = Main.connect.prepareStatement(querystring);
System.out.println("ca marche31");
System.out.println("NM_RS_1 = "+nomclient+"ok");
System.out.println("ca marche4");
ResultSet rs1 = ps.executeQuery(querystring);
System.out.println("ca marche5");
nmrs1 = rs1.getString("NM_RS");
System.out.println("NM_RS_1 = "+nmrs1+"ok");
}
catch (Exception e)
{
e.printStackTrace();
}
}
} |
les problemes que j'ai sont à la ligne : nmrs1 = rs1.getString("NM_RS");
voici ce que m'indique la console :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Driver ok
connection ok
ecriture test ok
ca marche31
NM_RS_1 = bonbonok
ca marche4
ca marche5
java.sql.SQLException: Illegal operation on empty result set.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at Meteo.<init>(Meteo.java:46)
at Main.main(Main.java:18) |
je vous remercie d'avance