Test de codes sur les classes JDBCRowSetImpl et CachedRowSetImpl dans Netbeans
Bonsoir :D
Je suis en train de me faire le livre "Programmer en Java - Couvre Java 10 à Java 14" de Claude Delannoy.
J'essaie de tester les exemples de code qui se trouvent :
"Chapitre 29 : Utilisation de bases de données avec JDBC".
"7 - L'interface Rowset".
Pour la classe JDBCRowSetImpl :
Construction à partir d’un objet de type ResultSet
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
|
import java.sql.* ;
import javax.sql.rowset.* ; // pour RowSet
import com.sun.rowset.JdbcRowSetImpl ; // indispensable
public class Rowset
{ public static void main (String[] args) throws ClassNotFoundException, SQLException
{ Class.forName ("com.mysql.cj.jdbc.Driver") ;
Connection connec = DriverManager.getConnection
("jdbc:mysql://root@localhost:3306/stocks");
// création d'un ResultSet
String commande = "SELECT nom, quantite FROM produits" ;
Statement stmt = connec.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE) ;
ResultSet res ;
res = stmt.executeQuery(commande);
// création d'un RowSet enveloppe du ResultSet et utilisation
JdbcRowSet rs = new JdbcRowSetImpl (res) ;
String nom ; int qte ;
// petite mise à jour
rs.first() ;
rs.updateString (1, "Cafetière 6 T") ;
rs.updateRow();
// liste dans l'ordre inverse
rs.afterLast();
while (rs.previous())
{ nom = rs.getString (1) ; qte = rs.getInt (2) ;
System.out.println (nom + " " + qte) ;
}
stmt.close () ; res.close (); rs.close (); connec.close () ;
}
} |
Construction d’un objet autonome
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
|
import java.sql.* ;
import javax.sql.rowset.* ; // pour rowset
import com.sun.rowset.JdbcRowSetImpl ; // indispensable !!!
public class Rowset1
{ public static void main (String[] args) throws ClassNotFoundException, SQLException
{ Class.forName ("com.mysql.cj.jdbc.Driver") ;
JdbcRowSet rs = new JdbcRowSetImpl () ;
rs.setUrl ("jdbc:mysql://root@localhost:3306/stocks");
rs.setType (ResultSet.TYPE_SCROLL_SENSITIVE) ;
rs.setConcurrency (ResultSet.CONCUR_UPDATABLE) ;
rs.setCommand ("SELECT nom, quantite FROM produits") ;
rs.execute();
// petite mise à jour
rs.first() ;
rs.updateString (1, "Cafetière 9 T") ;
rs.updateRow();
// liste dans l'ordre inverse
String nom ; int qte ;
rs.afterLast();
while (rs.previous())
{ nom = rs.getString (1) ; qte = rs.getInt (2) ;
System.out.println (nom + " " + qte) ;
}
rs.close ();
}
} |
Pour la classe CachedRowSetImpl
Construction à partir d’un objet de type ResultSet
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
|
import java.sql.* ;
import com.sun.rowset.CachedRowSetImpl ; // ou sun.rowset.CachedRowSetImpl
//import com.sun.rowset.JdbcRowSetImpl;
import javax.sql.rowset.* ; // pour CachedRowSet
public class Rowset3
{ public static void main (String[] args) throws ClassNotFoundException, SQLException
{ Class.forName ("com.mysql.cj.jdbc.Driver") ;
Connection connec = DriverManager.getConnection
("jdbc:mysql://root@localhost:3306/stocks");
// création d'un ResultSet
String commande = "SELECT nom, quantite FROM produits" ;
Statement stmt = connec.createStatement() ;
ResultSet res ;
res = stmt.executeQuery(commande);
// création d'un RowSet enveloppe du ResultSet et utilisation
CachedRowSet rs = new CachedRowSetImpl() ;
rs.populate(res) ; // on peuple le CachedRowSet avec les données du ResultSet
// petite mise à jour du premier enregistrement
rs.first() ;
rs.updateString (1, "Cafetière 18 T") ;
rs.updateRow();
rs.acceptChanges(connec) ; // indispensable de fournir ici les infos de connexion
// liste dans l'ordre inverse
String nom ; int qte ;
rs.afterLast ();
while (rs.previous())
{ nom = rs.getString (1) ; qte = rs.getInt (2) ;
System.out.println (nom + " " + qte) ;
}
rs.close(); res.close () ;
}
} |
Construction d’un objet autonome
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
|
import java.sql.* ;
import com.sun.rowset.CachedRowSetImpl ; // ou sun.rowset.CachedRowSetImpl
import javax.sql.rowset.* ; // pour CachedRowSet
public class Rowset2
{ public static void main (String[] args) throws ClassNotFoundException, SQLException
{ Class.forName ("com.mysql.cj.jdbc.Driver") ;
CachedRowSet rs = new CachedRowSetImpl () ;
rs.setUrl ("jdbc:mysql://root@localhost:3306/stocks");
rs.setType (ResultSet.TYPE_SCROLL_SENSITIVE) ;
rs.setConcurrency (ResultSet.CONCUR_UPDATABLE) ;
rs.setCommand ("SELECT nom, quantite FROM produits") ;
rs.execute();
// petite mise à jour du premier enregistrement
rs.first() ;
rs.updateString (1, "Cafetière 15 T") ;
rs.updateRow();
rs.acceptChanges() ; // indispensable ici
// liste dans l'ordre inverse
String nom ; int qte ;
rs.afterLast ();
while (rs.previous())
{ nom = rs.getString (1) ; qte = rs.getInt (2) ;
System.out.println (nom + " " + qte) ;
}
rs.close();
}
} |
J'utilise :
NetBeans 14
JDK 17.0.3.1
MySQL Connector/J 8.0.30
MySQL 5.7.17
Quand je tente de compiler le 1er code et le 2ème code, j'ai ce message d'erreur :
Citation:
error: package com.sun.rowset is not visible
import com.sun.rowset.JdbcRowSetImpl ; // indispensable
(package com.sun.rowset is declared in module java.sql.rowset, which does not export it)
Quand je tente de compiler le 3ème code et le 4ème code, j'ai ce message d'erreur :
Citation:
error: package com.sun.rowset is not visible
import com.sun.rowset.CachedRowSetImpl ; // ou sun.rowset.CachedRowSetImpl
(package com.sun.rowset is declared in module java.sql.rowset, which does not export it)
Que faire ?
Merci d'avance :D