KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > driver > PreparedStatementScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.driver;
26
27 import java.io.File JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31
32 import org.objectweb.cjdbc.scenario.templates.Raidb1Template;
33 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
34
35 /**
36  * This class test repeated PreparedStatement usage
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  */

40 public class PreparedStatementScenario extends Raidb1Template
41 {
42
43   // Number of loops in each test
44
private static final int N = 50;
45
46   /**
47    * Test repeated create statement
48    *
49    * @throws Exception if fails
50    */

51   public void testLoopCreateStatement() throws Exception JavaDoc
52   {
53     Connection JavaDoc con = getCJDBCConnection();
54     String JavaDoc tableName = "testblob" + System.currentTimeMillis();
55     PreparedStatement JavaDoc stmt = null;
56     for (int i = 0; i < N; i++)
57     {
58       stmt = con.prepareStatement("create table " + tableName + i
59           + " (id INTEGER,name VARCHAR)");
60       stmt.executeUpdate();
61     }
62   }
63
64   /**
65    * Test repeated create statement and close the statement after each execution
66    *
67    * @throws Exception if fails
68    */

69   public void testLoopCreateStatementAndClose() throws Exception JavaDoc
70   {
71     Connection JavaDoc con = getCJDBCConnection();
72     String JavaDoc tableName = "testblob" + System.currentTimeMillis();
73     for (int i = 0; i < N; i++)
74     {
75       PreparedStatement JavaDoc stmt = null;
76       stmt = con.prepareStatement("create table " + tableName + i
77           + " (id INTEGER,name VARCHAR)");
78       stmt.executeUpdate();
79       stmt.close();
80     }
81   }
82
83   /**
84    * Test repeated blob insert
85    *
86    * @throws Exception if fails
87    */

88   public void testLoopInsertBlobStatementAndClose() throws Exception JavaDoc
89   {
90     String JavaDoc imageFile = "/image/logo-noel.jpg";
91     File JavaDoc fis = new File JavaDoc(getClass().getResource(imageFile).getFile());
92     Connection JavaDoc con = getCJDBCConnection();
93
94     String JavaDoc tableName = "testblob" + System.currentTimeMillis();
95     PreparedStatement JavaDoc stmt = con.prepareStatement("create table " + tableName
96         + " (id INTEGER,name VARCHAR)");
97
98     stmt.executeUpdate();
99     stmt.close();
100
101     String JavaDoc query = "Insert into " + tableName + " values(1,?)";
102     PreparedStatement JavaDoc ps1 = con.prepareStatement(query);
103     Blob JavaDoc bob = new org.objectweb.cjdbc.driver.Blob(ScenarioUtility
104         .readBinary(fis));
105     ps1.setBlob(1, bob);
106     ps1.executeUpdate();
107     ps1.close();
108   }
109
110 }
Popular Tags