Extraire des données d'Oracle vers Excel via JDBC
Salut,
Je suis nouvel utilisateur de java(avec eclipse)
et j'essai de faire un programme qui me permette d'extraire des données d'une table oracle vers un fichier excel.
(si vous avez des exemples je suis preneur.
Je viens de trouver un code qui permet de se connecter à une base oracle et de faire une requête mais j'ai un soucis et je ne comprends pas pourquoi ?
Citation:
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.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)
Hello
le code en question :
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
|
package export_data;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class create_excel {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:TEST";
private static final String DB_USER = "test";
private static final String DB_PASSWORD = "test";
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
System.out.println("Hello");
// start
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
selectRecordsFromTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
// end
}
private static void selectRecordsFromTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String selectSQL = "SELECT * FROM DUAL";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);
// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String userid = rs.getString("USER_ID");
String username = rs.getString("USERNAME");
System.out.println("userid : " + userid);
System.out.println("username : " + username);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
//end create_excel
} |
Merci à tous.