Java examples for java.sql:Derby
save Rss Feed using embedded derby
import java.sql.*; import java.util.*; public class Main{ final static Properties props = new Properties(); private static String framework = "embedded"; private static String driver = "org.apache.derby.jdbc.EmbeddedDriver"; private static String protocol = "jdbc:derby:"; public static int saveRssFeed(RssFeed rssFeed) { int pk = rssFeed.link.hashCode(); loadDriver();//www . ja v a 2 s. c o m Connection conn = null; ArrayList statements = new ArrayList(); PreparedStatement psInsert = null; Statement s = null; ResultSet rs = null; try { // database name String dbName = "demoDB"; conn = DriverManager.getConnection(protocol + dbName + ";create=true", props); rs = conn.createStatement().executeQuery( "select count(id) from rssFeed where id = " + rssFeed.link.hashCode()); rs.next(); int count = rs.getInt(1); if (count == 0) { // handle transaction conn.setAutoCommit(false); s = conn.createStatement(); statements.add(s); psInsert = conn .prepareStatement("insert into rssFeed values (?, ?, ?)"); statements.add(psInsert); psInsert.setInt(1, pk); String escapeTitle = rssFeed.channelTitle.replaceAll("\'", "''"); psInsert.setString(2, escapeTitle); psInsert.setString(3, rssFeed.link); psInsert.executeUpdate(); conn.commit(); System.out.println("Inserted " + rssFeed.channelTitle + " " + rssFeed.link); System.out.println("Committed the transaction"); } shutdown(); } catch (SQLException sqle) { sqle.printStackTrace(); } finally { // release all open resources to avoid unnecessary memory usage // ResultSet close(rs); // Statements and PreparedStatements int i = 0; while (!statements.isEmpty()) { // PreparedStatement extend Statement Statement st = (Statement) statements.remove(i); close(st); } //Connection close(conn); } return pk; } private static void loadDriver() { try { Class.forName(driver).newInstance(); System.out.println("Loaded driver"); } catch (Exception e) { e.printStackTrace(); } } private static void shutdown() { // standard checking code when shutting down database. // code from http://db.apache.org/derby/ if (framework.equals("embedded")) { try { // the shutdown=true attribute shuts down Derby DriverManager.getConnection("jdbc:derby:;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"); se.printStackTrace(); } } } } private static void close(AutoCloseable closable) { try { if (closable != null) { closable.close(); closable = null; } } catch (Exception sqle) { sqle.printStackTrace(); } } }