KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 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): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.driver;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.objectweb.cjdbc.driver.ControllerInfo;
33 import org.objectweb.cjdbc.scenario.templates.Raidb1Template;
34
35 /**
36  * This class defines a PreparedStatementSetStringScenario
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modryzk </a>
39  */

40 public class PreparedStatementSetStringScenario extends Raidb1Template
41 {
42   /**
43    * Test setString of <code>PreparedStatement</code>
44    *
45    * @throws Exception if fails
46    */

47   public void testSetString() throws Exception JavaDoc
48   {
49     Properties JavaDoc props = new Properties JavaDoc();
50     props.put("user", "user");
51     props.put("password", "");
52     props.put("escapeBackslash", "false");
53     props.put("escapeSingleQuote", "true");
54
55     Connection JavaDoc con = getCJDBCConnection(
56         new ControllerInfo[]{new ControllerInfo("localhost", 25322)}, "myDB",
57         props);
58
59     String JavaDoc mygeorge = "" + 'G' + 'e' + '\'' + 'o' + 'r' + '\\' + 'g' + '\\'
60         + 'e';
61     String JavaDoc statement = "INSERT INTO ADDRESS VALUES(50,?,'Fuller','534 - 20th Ave.','Olten')";
62     PreparedStatement JavaDoc ps = con.prepareStatement(statement);
63     ps.setString(1, mygeorge);
64     ps.setEscapeProcessing(true);
65     ps.executeUpdate();
66
67     PreparedStatement JavaDoc ps2 = con
68         .prepareStatement("Select * from ADDRESS where id=?");
69     ps2.setString(1, "50");
70     ResultSet JavaDoc rs = ps2.executeQuery();
71     assertTrue("Empty result set", rs.next());
72     String JavaDoc george = rs.getString("firstname");
73     assertEquals("Wrong value for george (was :" + george + " )", george,
74         mygeorge);
75   }
76 }
Popular Tags