smeacham

BetterHelloDatabase Example

Sep 18th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.sql.ResultSet;
  2.  
  3. public class BetterHelloDatabase
  4. {
  5.     public static void main (String[] args) throws Exception
  6.     {
  7.         final String dropTable = "DROP TABLE IF EXISTS dummy";
  8.         final String createTable = "CREATE TABLE dummy (id numeric, response text)";
  9.         final String insertSql = "INSERT INTO dummy VALUES(1,'Hello from the database')";
  10.         final String selectSql = "SELECT response from dummy";
  11.  
  12.         final DbClass db = new DbClass("jdbc:sqlite:hello.db");
  13.         try {
  14.  
  15.         db.execute(dropTable);
  16.         db.execute(createTable);
  17.         db.execute(insertSql);
  18.  
  19.         ResultSet rs = db.executeQuery(selectSql);
  20.         try {
  21.             while(rs.next())
  22.             {
  23.                 String sResult = rs.getString("response");
  24.                 System.out.println(sResult);
  25.             }
  26.             } finally {
  27.                 try { rs.close(); } catch (Exception ignore) {}
  28.             }
  29.         } finally {
  30.             try { db.closeConnection(); } catch (Exception ignore) {}
  31.         }
  32.     }
  33. }
  34. ===== DbClass.java =====
  35. import java.sql.Connection;
  36. import java.sql.DriverManager;
  37. import java.sql.ResultSet;
  38. import java.sql.SQLException;
  39. import java.sql.Statement;
  40.  
  41. public class DbClass {
  42.     public String url; // for advertising and debug purposes
  43.     private String driver;
  44.     private Connection conn;
  45.     private Statement stmt;
  46.  
  47.     public DbClass(String url) throws Exception {
  48.         this.url = url;
  49.         setConnection();
  50.     }
  51.  
  52.     private void setConnection() throws Exception {
  53.         try {
  54.             Class.forName("org.sqlite.JDBC");
  55.         } catch (Exception e) {
  56.             System.out.println("DriverName: " + driver + " was not available");
  57.             System.err.println(e);
  58.             throw e;
  59.         }
  60.  
  61.         conn = DriverManager.getConnection(url);
  62.         try {
  63.             stmt = conn.createStatement();
  64.         } catch (Exception e) {
  65.             closeConnection();
  66.         }
  67.     }
  68.  
  69.     public void closeConnection() {
  70.         if (stmt != null) { try { stmt.close(); stmt = null; } catch (Exception ignore) {} }
  71.         if (conn != null) { try { conn.close(); conn = null; } catch (Exception ignore) {} }
  72.     }
  73.  
  74.     public ResultSet executeQuery(String instruction) throws SQLException {
  75.         return stmt.executeQuery(instruction);
  76.     }
  77.  
  78.     public void execute(String instruction) throws SQLException {
  79.         stmt.executeUpdate(instruction);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment