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
| package org.debatz.schoolManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import com.sun.tools.javac.util.Pair;
public class DatabaseManager {
private final static String dbUser = "user1";
private final static String dbName = "derbyDB";
private final static String dbPassword = "user1";
private final static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private final static String protocol = "jdbc:derby:";
public final static String COURSES_TABLE = "COURSES";
private static Connection connect;
public DatabaseManager () throws SQLException {
Properties properties = new Properties();
properties.put("user", dbUser); // schema
properties.put("password", dbPassword);
connect = DriverManager.getConnection(protocol + dbName + ";created=true", properties);
connect.setAutoCommit(false);
System.out.println("Nous voilà connecté à la base " + dbName);
System.out.println("connect = " + connect); // on affiche ce que contient la variable de connexion
loadDriver(); // on charge les drivers
}
public boolean tableExists (String tableName) throws Exception {
ResultSet rs;
boolean exists;
try {
DatabaseMetaData md = connect.getMetaData();
rs = md.getTables(null, dbName, tableName, null);
exists = rs.next();
} finally {
closeConnection();
}
return exists;
}
public boolean createTable (String tableName, List<Pair<String, String>> fields) throws Exception {
Statement s = null;
if (!tableExists(tableName)) {
String query = "CREATE TABLE " + tableName + "(";
s = connect.createStatement();
for (int i = 0; i < fields.size(); i++)
query += (i == 0) ? fields.get(i).fst + " " + fields.get(i).snd : ", " + fields.get(i).fst + " " + fields.get(i).snd;
query += ")";
System.out.println(query);
return s.execute(query); // renvoi false ici.. pourquoi?
}
return false;
}
private static void closeConnection () {
if (connect == null) {
try {
connect.close();
DriverManager.getConnection("jdbc:derby:" + dbName + ";shutdown=true");
}
catch (SQLException se)
{
if (se.getErrorCode() == 50000 && ("XJ015").equals(se.getSQLState())) {
System.out.println("Derby shut down normally");
} else {
System.err.println("Derby did not shut down normally");
printSQLException(se);
}
}
}
}
private static void loadDriver() {
try {
Class.forName(driver).newInstance();
System.out.println("Loaded the appropriate driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("\nUnable to load the JDBC driver " + driver);
System.err.println("Please check your CLASSPATH.");
cnfe.printStackTrace(System.err);
} catch (InstantiationException ie) {
System.err.println("\nUnable to instantiate the JDBC driver " + driver);
ie.printStackTrace(System.err);
} catch (IllegalAccessException iae) {
System.err.println("\nNot allowed to access the JDBC driver " + driver);
iae.printStackTrace(System.err);
}
}
public static void printSQLException(SQLException e)
{
while (e != null)
{
System.err.println("\n----- SQLException -----");
System.err.println(" SQL State: " + e.getSQLState());
System.err.println(" Error Code: " + e.getErrorCode());
e = e.getNextException();
}
}
} |
Partager