001: package net.sourceforge.jdbclogger.demo;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024: import java.sql.Timestamp;
025: import java.util.Calendar;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * @author Catalin Kormos (latest modification by $Author: catalean $)
032: * @version $Revision: 121 $ $Date: 2007-09-17 13:39:03 -0700 (Mon, 17 Sep 2007) $
033: */
034: public abstract class AbstractJdbcLoggerDemo {
035:
036: private static Log log = LogFactory.getLog("JdbcLoggerDemo");
037:
038: public void executeJdbcStatements() {
039: Connection conn = null;
040: try {
041: conn = getConnection();
042:
043: executeCreateTestTableStatement(conn);
044: executeJdbcInsertStatements(conn);
045: executeRepeatedBatchStatements(conn);
046: } catch (Exception exc) {
047: log.error("Error getting database connection", exc);
048: } finally {
049: if (conn != null) {
050: try {
051: conn.close();
052: } catch (SQLException exc) {
053: log.error(
054: "Error while closing database connection",
055: exc);
056: }
057: }
058: }
059: }
060:
061: /**
062: * @param conn
063: * @throws SQLException
064: */
065: public void executeJdbcInsertStatements(Connection conn)
066: throws SQLException {
067: log.info("Executing test JDBC inserts...");
068:
069: //prepare the statement for inserting some data in the database
070: PreparedStatement ps = conn
071: .prepareStatement("insert into TEST_TABLE values (?, ?, ?, ?);");
072:
073: byte[] b = { 1, 2, 3 };
074:
075: for (int i = 1; i < 7; i++) {
076: ps.setInt(1, i);
077: ps.setString(2, "text " + String.valueOf(i));
078: ps.setTimestamp(3, new Timestamp(Calendar.getInstance()
079: .getTimeInMillis()));
080: ps.setBytes(4, b);
081:
082: ps.addBatch();
083: }
084:
085: //executing the batch...
086: ps.executeBatch();
087:
088: conn.commit();
089:
090: log.info("Test JDBC inserts successfuly executed.");
091: }
092:
093: /**
094: * @param conn
095: * @throws SQLException
096: */
097: public void executeRepeatedBatchStatements(Connection conn)
098: throws SQLException {
099: log.info("Executing test repeated batch JDBC inserts...");
100:
101: //prepare the statement for inserting some data in the database
102: PreparedStatement ps = conn
103: .prepareStatement("insert into TEST_TABLE values (?, ?, ?, ?);");
104:
105: byte[] b = { 1, 2, 3 };
106:
107: int id = 1;
108: for (int batchCount = 0; batchCount < 2; batchCount++) {
109: log.info("Execute new batch");
110: for (int i = 1; i < 7; i++) {
111: ps.setInt(1, id);
112: ps.setString(2, "text " + String.valueOf(id));
113: ps.setTimestamp(3, new Timestamp(Calendar.getInstance()
114: .getTimeInMillis()));
115: ps.setBytes(4, b);
116:
117: ps.addBatch();
118:
119: id += 1;
120: }
121:
122: //executing the batch...
123: ps.executeBatch();
124: }
125:
126: conn.commit();
127:
128: log.info("Repeated batch JDBC inserts successfuly executed.");
129: }
130:
131: /**
132: * @param conn
133: * @throws SQLException
134: */
135: public void executeCreateTestTableStatement(Connection conn)
136: throws SQLException {
137: Statement st = conn.createStatement();
138: st
139: .execute("create table TEST_TABLE (id int, text varchar(255), createdTs timestamp, content Object)");
140: conn.commit();
141: }
142:
143: /**
144: * @return
145: * @throws SQLException
146: */
147: protected Connection getConnection() throws SQLException {
148: return DriverManager.getConnection("jdbc:hsqldb:mem:test",
149: "sa", "");
150: }
151: }
|