import java.io.*; import java.sql.*; public class jdbc2Batch { static String ajuri = "sun.jdbc.odbc.JdbcOdbcDriver"; static String url = "jdbc:odbc:JDBCHenkilot"; private static String kysely[] = { "UPDATE henkilotiedot SET sposti='jaska@jokunen.net' WHERE nimi='Jaska Jokunen'", "UPDATE henkilotiedot SET ika=ika+1 WHERE nimi='Jaska Jokunen'", "UPDATE henkilotiedot SET ika=ika+2 WHERE nimi='Jaska Jokunen'" }; private static String select[] = { "select * from henkilotiedot", "select * from henkilotiedot where nimi like ?" }; //************** Pääohjelma ************************** public static void main(String[] args){ // throws SQLException { // **** Muodostetaan yhteys tietokontaan (Connection-olio) ************ Connection con = HenkiloTietokanta.yhdista(ajuri, url); if (con == null) { System.exit(0); } int palautus=0; //**** Kannan tietojen muokkaus (sql insert/update) ****** //**** Tällä kertaa suoritetaan Batch-update ja päivitetään samaa tietuetta kolme kertaa ****** try { HenkiloTietokanta.paivita(con, kysely); } catch (SQLException sqle) { while (sqle != null) { System.out.println(sqle.toString()); sqle = sqle.getNextException(); } } catch (Exception sqle) { System.out.println(sqle.toString()); } //**** Kysely (sql select)****** try { System.out.println("Kysely:" + select[0]); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(select[0]); HenkiloTietokanta.kysely(rs); stmt.close(); } catch (SQLException sqle) { while (sqle != null) { System.out.println(sqle.toString()); sqle = sqle.getNextException(); } } // Suljetaan yhteys lopuksi try { if (con != null) con.close(); } catch (SQLException e) { } } }