[Débutant][JDBC][MYSQL] Erreur lors de la lecture d'un resultset
Bonjour,
Je suis actuellement confronté à un problème lors de la lecture de mon resultset. J'ai une base de données nommée "Facturation", avec une table "CLIENTS" contenant une ligne avec un id "ID" egal à 1 et un nom "NAME" égal à "coucou" en VARCHAR.
J'execute le code suivant :
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
import java.sql.*;
//import com.mysql.jdbc.*;
public class ConnectionManager
{
private static Connection connection;
private static Statement stmt = null;
/**
* Function to connect on Database. Must be called before any other operations on database.
* @param url The URL of the Database
* @param user The User name
* @param password The password linked to the user
* @return true if the connection is established, false otherwise
**/
public static boolean connect(String url,String user, String password)
{
try
{
connection = DriverManager.getConnection("jdbc:mysql://"
+url
+"?"
+"user="+user
+"&password="+password);
stmt=connection.createStatement();
}
catch (SQLException ex)
{
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return false;
}
return true;
}
/**
* Function to select Database on wich we will work
* @param database The name of the database to work on
* @return true if we can select the database, false otherwise
**/
public static boolean selectDatabase(String database)
{
String query="USE "+database;
try
{
stmt.executeQuery(query);
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
/**
* Disconnects from the server.
* @return true if the deconnection happened normally, false in all other cases (connection may not be established)
**/
public static boolean disconnect()
{
try
{
if (stmt != null)
{
try
{
stmt.close();
} catch (SQLException sqlEx)
{
stmt = null;
}
}
if(connection!=null)
connection.close();
}catch(Exception e)
{
System.out.println(e);
return false;
}
return true;
}
/**
* Function to execute INSERT and UPDATE queries on database.
* @param query The query to execute (only INSERT and UPDATE queries)
* @return true if the query have well been executed, false if any problem occurs.
**/
public static boolean executeInsertUpdate(String query)
{
if(connection==null)
return false;
try
{
stmt.execute(query);
}catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
/**
* Function to execute a SELECT queries on database.
* @param query The query to execute (only SELECT queries)
* @return the ResultSet if the query was well executed, null otherwise
**/
public static ResultSet executeSelect(String query)
{
if(connection==null)
return null;
ResultSet rs = null;
try
{
rs = stmt.executeQuery(query);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
/*if (stmt.execute(query))
{
rs = stmt.getResultSet();
}*/
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
return null;
}
finally
{
}
return rs;
}
public static void main(String[] args) throws Exception
{
System.out.println(connect("mabase.net","monLogin","monPass"));
System.out.println(selectDatabase("Facturation"));
ResultSet rs=executeSelect("SELECT * from CLIENTS WHERE ID=1");
System.out.println(rs!=null);
//rs.next();
System.out.println(rs.getString("NAME"));
System.out.println(disconnect());
}
} |
j'obtiens comme sortie :
Citation:
D:\Tests>java ConnectionManager
true
true
true
Exception in thread "main" java.sql.SQLException
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:552
8)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at ConnectionManager.main(ConnectionManager.java:160)
D:\Tests>
Les deux true signifient donc que la connexion au serveur et le choix de la BDD se font sans problèmes.
Le ResultSet est lui aussi bien récupéré puisque je n'ai pas de null renvoyé.
Cependant, normalement je devrai avoir une ligne avec une colone "NAME" et ici je n'en ai pas, d'où l'exception. J'ai essayé d'aller chercher le numéro de collone, ca ne marche pas non plus (0 et 1 et 2) ainsi que d'appeller un next() avant, celà ne fonctionne pas non plus (empty apres le next).
Je ne vois pas où est mon erreur, quelqu'un pourrait-il m'éclairer ?
Merci
Fred