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
| import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
public class App {
public static Connection connectionToMemory;
public static Connection connectionToFile;
public static void main(String[] args) throws InstantiationException, IllegalAccessException, SQLException {
//HSQL DB
System.out.println("HSQLDB TEST");
System.out.println("-----------");
connectToHsqlDB();
System.out.println("Return code de la création de la table:" +
executeQuery("CREATE TABLE test (" + "colonne1 INTEGER NOT NULL , colonne2 INTEGER NOT NULL )" ));
System.out.println("Return code de l'insertion de la table:" +
executeQuery("INSERT INTO test(colonne1,colonne2) VALUES (1 , 2) )"));
int j = 2;
for (int i = 2; i <= 10; i++) {
executeQuery("INSERT INTO test(colonne1,colonne2) VALUES (" + i + " ," + j + ")");
}
ResultSet resultSet = getSQLResult("SELECT * FROM test");
if (! resultSet.equals(null)) {
while (resultSet.next()) {
System.out.println("Colonne1:" + resultSet.getInt("colonne1")
+ " Colonne2:" + resultSet.getInt("colonne2"));
}
}
}
public static void connectToHsqlDB() throws InstantiationException, IllegalAccessException, SQLException
{
try { DriverManager.getDriver("jdbc:hsqldb:mem:testpvu");
} catch (SQLException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Class.forName("org.hsqldb.jdbcDriver").newInstance();
} catch (ClassNotFoundException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
try {
//connectionToFile = DriverManager.getConnection("jdbc:hsqldb:file:database;shutdown=true","SA","");
connectionToMemory = DriverManager.getConnection("jdbc:hsqldb:mem:testpvu;shutdown=true", "SA", "");
//Backup DB
Statement statement = connectionToMemory.createStatement();
statement.executeQuery("SHUTDOWN");
statement.close();
} catch (SQLException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
System.out.println("Unhandeld exception:" + ex.getMessage());
}
}
public static int executeQuery(String query) {
try {
Statement statement = connectionToMemory.createStatement();
return statement.executeUpdate(query);
}
catch (SQLException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex) {
System.out.println("Unhandeld exception:" + ex.getMessage());
}
return -1;
}
public static ResultSet getSQLResult(String query) {
try {
Statement statement = connectionToMemory.createStatement();
return statement.executeQuery(query);
}
catch (SQLException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex) {
System.out.println("Unhandeld exception:" + ex.getMessage());
}
return null;
}
} |
Partager