Философия Java


Более изощренный пример - часть 5


{ "EVTMEMS", new Integer(1), // Dave is going to

new Integer(1), // the Software event.

new Integer(0) }, { "EVTMEMS", new Integer(2), // Bruce is going to

new Integer(2), // the Archeology event.

new Integer(0) }, { "EVTMEMS", new Integer(3), // Robert is going to

new Integer(1), // the Software event.

new Integer(1) }, { "EVTMEMS", new Integer(3), // ... and

new Integer(2), // the Archeology event.

new Integer(1) }, }; // Use the default data set:

public TestSet() {} // Use a different data set:

public TestSet(Object[][] dat) { data = dat; } }

public class LoadDB { Statement statement; Connection connection; TestSet tset; public LoadDB(TestSet t) throws SQLException { tset = t; try { // Load the driver (registers itself)

Class.forName(CIDConnect.dbDriver); } catch(java.lang.ClassNotFoundException e) { e.printStackTrace(System.err); } connection = DriverManager.getConnection( CIDConnect.dbURL, CIDConnect.user, CIDConnect.password); statement = connection.createStatement(); } public void cleanup() throws SQLException { statement.close(); connection.close(); } public void executeInsert(Object[] data) { String sql = "insert into " + data[0] + " values("; for(int i = 1; i < data.length; i++) { if(data[i] instanceof String) sql += "'" + data[i] + "'"; else

sql += data[i]; if(i < data.length - 1) sql += ", "; } sql += ')'; System.out.println(sql); try { statement.executeUpdate(sql); } catch(SQLException sqlEx) { System.err.println("Insert failed."); while (sqlEx != null) { System.err.println(sqlEx.toString()); sqlEx = sqlEx.getNextException(); } } } public void load() { for(int i = 0; i< tset.data.length; i++) executeInsert(tset.data[i]); } // Выбрасываем исключение на консоль:

public static void main(String[] args) throws SQLException { LoadDB db = new LoadDB(new TestSet()); db.load(); try { // Получаем ResultSet из загруженной базы данных:

ResultSet rs = db.statement.executeQuery( "select " + "e.EVT_TITLE, m.MEM_LNAME, m.MEM_FNAME "+ "from EVENTS e, MEMBERS m, EVTMEMS em " + "where em.EVT_ID = 2 " + "and e.EVT_ID = em.EVT_ID " + "and m.MEM_ID = em.MEM_ID"); while (rs.next()) System.out.println( rs.getString(1) + " " + rs.getString(2) + ", " + rs.getString(3)); } finally { db.cleanup(); } } } ///:~




Начало  Назад  Вперед