PetrovIgor

DerbyDAO (simple)

Jan 15th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.23 KB | None | 0 0
  1. package od.igor.petrov.derby;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11. import od.igor.petrov.utils.TestTableBean;
  12.  
  13. public class DerbyDAO {
  14.    
  15.     /* ========================================== *
  16.      *     BASIC MANIPULATIONS WITH DATABASE
  17.      * ========================================== */
  18.     protected Connection retrieveConnection() {
  19.         try {
  20.             if (conn == null || conn.isClosed()) {
  21.                 Class.forName(DRIVER);
  22.                 System.out.println("Driver loaded!");
  23.                 conn = DriverManager.getConnection(CONNECTION_URL);
  24.                 System.out.println("Connection really opened");
  25.             }
  26.         } catch (ClassNotFoundException e) {
  27.             System.out.println("Problems with driver");
  28.         } catch (SQLException e) {
  29.             System.out.println("Some error");
  30.         } finally {
  31.             System.out.println("Had chance to open connection");
  32.         }
  33.         return conn;
  34.     }
  35.    
  36.     protected void closeConnection() {
  37.         try {
  38.             if (conn != null) {
  39.                 conn.close();
  40.                 System.out.println("Closing connection");
  41.             }
  42.         } catch (SQLException e) {
  43.             System.out.println("Failed to close connection");
  44.         } finally {
  45.             conn = null;
  46.         }
  47.     }
  48.    
  49.    
  50.     /* =============================================== *
  51.      *                     C R U D
  52.      * =============================================== */
  53.     protected void createTable() {
  54.         try {
  55.             s = retrieveConnection().createStatement();
  56.             if (tableExists()) {
  57.                 System.out.println("Found previous version of table\nDropping");
  58.                 s.execute(DROP_TABLE);
  59.             }
  60.             s.execute(CREATE_TABLE);
  61.         } catch (SQLException e) {
  62.             System.out.println("Error while creating statement");
  63.         }
  64.     }
  65.    
  66.     protected boolean tableExists() {
  67.         try {
  68.             s = retrieveConnection().createStatement();
  69.             s.execute(TEST_STATEMENT);
  70.         } catch (SQLException e) {
  71.             String err = (e).getSQLState();
  72.             if (err.equals(ERROR_TABLE_EXISTS)) {
  73.                 return false;
  74.             }
  75.         }
  76.         return true;
  77.     }
  78.    
  79.     public void insertValues(){
  80.         createTable();
  81.         try {
  82.             ps = retrieveConnection().prepareStatement(INSERT_VALUES);
  83.             for (int i = 0; i < 10; i++) {
  84.                 ps.setInt(1, i);
  85.                 ps.setString(2, "value" + i);
  86.                 switch (i%3) {
  87.                 case 0:
  88.                     ps.setString(3, "Firm-0");
  89.                     break;
  90.                 case 1:
  91.                     ps.setString(3, "Firm-1");
  92.                     break;
  93.                 case 2:
  94.                     ps.setString(3, "Firm-2");
  95.                     break;
  96.                 default:
  97.                     ps.setString(3, "Dafault");
  98.                     break;
  99.                 }
  100.                 ps.executeUpdate();
  101.             }
  102.             System.out.println("Inserted some values");
  103.             if (ps != null) {
  104.                 ps.close();
  105.             }
  106.         } catch (SQLException e) {
  107.             System.out.println("Error while creating prepared statement");
  108.         } finally {
  109.             ps = null;
  110.             closeConnection();
  111.         }
  112.     }
  113.    
  114.     public ArrayList<TestTableBean> selectValues(){
  115.         try {
  116.             s = retrieveConnection().createStatement();
  117.             results = s.executeQuery(SELECT_VALUES);
  118.             while (results.next()) {
  119.                 result.add(new TestTableBean(results.getInt(1), results.getString(2), results.getString(3)));
  120.             }
  121.             if (results != null) {
  122.                 results.close();
  123.             }
  124.         } catch (SQLException e) {
  125.             System.out.println("Some error with getting SQL");
  126.             return null;
  127.         } finally {
  128.             results = null;
  129.             closeConnection();
  130.         }
  131.         return result;
  132.     }
  133.  
  134.     private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
  135.     private static final String DB_NAME = "TestDB";
  136.     private static final String CONNECTION_URL = "jdbc:derby:" + DB_NAME + ";create=true";
  137.  
  138.     private static final String CREATE_TABLE = "CREATE TABLE TESTTABLE(ID INT NOT NULL, ITEM VARCHAR(10) NOT NULL, FIRM VARCHAR(10) NOT NULL)";
  139.     private static final String INSERT_VALUES = "INSERT INTO TESTTABLE VALUES(?, ?, ?)";
  140.     private static final String SELECT_VALUES = "SELECT * FROM TESTTABLE";
  141.     private static final String DROP_TABLE = "DROP TABLE TESTTABLE";
  142.  
  143.     private static final String TEST_STATEMENT = "UPDATE TESTTABLE SET ID = 1, ITEM = 'TEST' WHERE 1 = 3";
  144.     private static final String ERROR_TABLE_EXISTS = "42X05";
  145.  
  146.     private Connection conn = null;
  147.     private Statement s = null;
  148.     private PreparedStatement ps = null;
  149.     private ResultSet results = null;
  150.    
  151.     private ArrayList<TestTableBean> result = new ArrayList<TestTableBean>();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment