extraire d'une base vers excel

extraire d'une base vers excel - Java - Programmation

Marsh Posté le 23-11-2013 à 19:35:00    

Salut,
 
Je débute en java,
et je cherche des cours ou des exemples qui me permettrait d'extraire des données
d'une table oracle vers un fichier excel.
 
Merci.

Reply

Marsh Posté le 23-11-2013 à 19:35:00   

Reply

Marsh Posté le 23-11-2013 à 20:41:12    

j'ai essayé ce code  
 

Code :
  1. package export_data;
  2. import java.sql.DriverManager;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. public class create_excel {
  8. private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  9. private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:TEST";
  10. private static final String DB_USER = "test";
  11. private static final String DB_PASSWORD = "test";
  12. public static void  main(String[] args) throws ClassNotFoundException {
  13.  // TODO Auto-generated method stub
  14.  System.out.println("Hello" );
  15. // start  
  16.  try {
  17.    Class.forName ("oracle.jdbc.OracleDriver" );
  18.   selectRecordsFromTable();
  19.  } catch (SQLException e) {
  20.   System.out.println(e.getMessage());
  21.  }
  22. // end
  23. }
  24. private static void selectRecordsFromTable() throws SQLException {
  25.  Connection dbConnection = null;
  26.  PreparedStatement preparedStatement = null;
  27.  String selectSQL = "SELECT * FROM DUAL";
  28.  try {
  29.   dbConnection = getDBConnection();
  30.   preparedStatement = dbConnection.prepareStatement(selectSQL);
  31.   preparedStatement.setInt(1, 1001);
  32.   // execute select SQL stetement
  33.   ResultSet rs = preparedStatement.executeQuery();
  34.   while (rs.next()) {
  35.    String userid = rs.getString("USER_ID" );
  36.    String username = rs.getString("USERNAME" );
  37.    System.out.println("userid : " + userid);
  38.    System.out.println("username : " + username);
  39.   }
  40.  } catch (SQLException e) {
  41.   System.out.println(e.getMessage());
  42.  } finally {
  43.   if (preparedStatement != null) {
  44.    preparedStatement.close();
  45.   }
  46.   if (dbConnection != null) {
  47.    dbConnection.close();
  48.   }
  49.  }
  50. }
  51. private static Connection getDBConnection() {
  52.  Connection dbConnection = null;
  53.  try {
  54.   Class.forName(DB_DRIVER);
  55.  } catch (ClassNotFoundException e) {
  56.   System.out.println(e.getMessage());
  57.  }
  58.  try {
  59.   dbConnection = DriverManager.getConnection(
  60.                              DB_CONNECTION, DB_USER,DB_PASSWORD);
  61.   return dbConnection;
  62.  } catch (SQLException e) {
  63.   System.out.println(e.getMessage());
  64.  }
  65.  return dbConnection;
  66. }
  67. //end create_excel
  68. }


 
j'ai l'erreur suivante que je ne comprends pas trop:
 

Citation :


Hello
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at export_data.create_excel.main(create_excel.java:24)
 

Reply

Marsh Posté le 25-11-2013 à 11:23:27    

tu as un problème de chargement du driver oracle, il te manque le jar ou le jar n'est pas dans le classpath.
C'est une erreur de base, google t'aurais donné la réponse
http://javarevisited.blogspot.fr/2 [...] river.html
 
Pour l'export excel, tu pourras prendre Apache POI une API pour accèder aux documents MS.
Je l'ai utilisé il y a quelques années, et c'est la seule bibli non payante à ma connaissance


Message édité par willy le kid le 25-11-2013 à 11:33:55
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed