KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > demos > scalability > jdbc > JDBCScalabilityConnection


1 package org.prevayler.demos.scalability.jdbc;
2
3 import org.prevayler.demos.scalability.*;
4 import java.sql.*;
5
6
7 // Contributions by Stefan Ortmanns.
8
abstract class JDBCScalabilityConnection {
9
10     protected final Connection connection;
11     private final PreparedStatement insertStatement;
12
13     protected JDBCScalabilityConnection(Connection connection) {
14         this.connection = connection;
15         try {
16             connection.setAutoCommit(false);
17         } catch (SQLException sqlx) {
18             dealWithSQLException(sqlx, "setting AutoCommit to false");
19         }
20
21         insertStatement = prepare("insert into " + table() + " (ID,NAME,STRING1,BIGDECIMAL1,BIGDECIMAL2,DATE1,DATE2) values(?,?,?,?,?,?,?)");
22     }
23
24
25     protected abstract String JavaDoc table();
26
27
28     protected void insert(Record recordToInsert) {
29         try {
30             insertStatement.setLong(1,recordToInsert.getId());
31             insertStatement.setString(2,recordToInsert.getName());
32             insertStatement.setString(3,recordToInsert.getString1());
33             insertStatement.setBigDecimal(4,recordToInsert.getBigDecimal1());
34             insertStatement.setBigDecimal(5,recordToInsert.getBigDecimal2());
35             insertStatement.setDate(6,new java.sql.Date JavaDoc(recordToInsert.getDate1().getTime()));
36             insertStatement.setDate(7,new java.sql.Date JavaDoc(recordToInsert.getDate2().getTime()));
37             insertStatement.execute();
38         } catch (SQLException sqlx) {
39             dealWithSQLException(sqlx, "inserting record");
40         }
41     }
42
43
44     protected PreparedStatement prepare(String JavaDoc statement) {
45         try {
46             return connection.prepareStatement(statement);
47         } catch (SQLException sqlx) {
48             dealWithSQLException(sqlx, "preparing statement: " + statement);
49             return null;
50         }
51     }
52
53
54     void replaceAllRecords(int records) {
55         RecordIterator newRecords = new RecordIterator(records);
56
57         try {
58             connection.createStatement().execute("delete from " + table());
59         } catch (SQLException sqlx) {
60             dealWithSQLException(sqlx, "deleting all records from " + table());
61         }
62
63         while (newRecords.hasNext()) {
64             insert(newRecords.next());
65         }
66
67         try {
68             connection.commit();
69         } catch (SQLException sqlx) {
70             dealWithSQLException(sqlx, "commiting insertion of test records");
71         }
72     }
73
74
75     static protected void dealWithSQLException(SQLException sqlx, String JavaDoc duringOperation) {
76         sqlx.printStackTrace();
77         throw new RuntimeException JavaDoc("SQLException " + duringOperation + ".");
78     }
79 }
Popular Tags