KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > commands > sqlconsole > CallStoredProcedure


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): Jeff Mesnil.
22  * Contributor(s): ______________________.
23  */

24 /**
25  * C-JDBC: Clustered JDBC.
26  * Copyright (C) 2002-2004 French National Institute For Research In Computer
27  * Science And Control (INRIA).
28  * Contact: c-jdbc@objectweb.org
29  *
30  * This library is free software; you can redistribute it and/or modify it
31  * under the terms of the GNU Lesser General Public License as published by the
32  * Free Software Foundation; either version 2.1 of the License, or any later
33  * version.
34  *
35  * This library is distributed in the hope that it will be useful, but WITHOUT
36  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
38  * for more details.
39  *
40  * You should have received a copy of the GNU Lesser General Public License
41  * along with this library; if not, write to the Free Software Foundation,
42  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
43  *
44  * Initial developer(s): Jeff Mesnil
45  * Contributor(s): ______________________.
46  */

47
48 package org.objectweb.cjdbc.console.text.commands.sqlconsole;
49
50 import java.io.IOException JavaDoc;
51 import java.sql.CallableStatement JavaDoc;
52 import java.sql.Connection JavaDoc;
53 import java.sql.ResultSet JavaDoc;
54
55 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
56 import org.objectweb.cjdbc.console.text.ConsoleException;
57 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
58 import org.objectweb.cjdbc.console.text.formatter.ResultSetFormatter;
59 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseConsole;
60
61 /**
62  * This class defines a "timeout" sql command
63  *
64  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
65  */

66 public class CallStoredProcedure extends ConsoleCommand
67 {
68
69   /**
70    * Creates a new <code>SetTimeout</code> object
71    *
72    * @param module the command is attached to
73    */

74   public CallStoredProcedure(VirtualDatabaseConsole module)
75   {
76     super(module);
77   }
78
79   /**
80    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#parse(java.lang.String)
81    */

82   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
83   {
84     callStoredProcedure(getCommandName() + " " + commandText);
85   }
86
87   /**
88    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandName()
89    */

90   public String JavaDoc getCommandName()
91   {
92     return "{call";
93   }
94
95   /**
96    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandParameters()
97    */

98   public String JavaDoc getCommandParameters()
99   {
100     return ConsoleTranslate.get("sql.command.call.stored.procedure.params");
101   }
102
103   /**
104    * @see org.objectweb.cjdbc.console.text.commands.ConsoleCommand#getCommandDescription()
105    */

106   public String JavaDoc getCommandDescription()
107   {
108     return ConsoleTranslate
109         .get("sql.command.call.stored.procedure.description");
110   }
111
112   /**
113    * Call a store procedure.
114    *
115    * @param proc the stored procedure to call
116    */

117   private synchronized void callStoredProcedure(String JavaDoc proc)
118   {
119     Connection JavaDoc connection = ((VirtualDatabaseConsole) module).getConnection();
120     int fetchsize = ((VirtualDatabaseConsole) module).getFetchsize();
121     CallableStatement JavaDoc cs = null;
122     try
123     {
124       cs = connection.prepareCall(proc);
125       cs.setQueryTimeout(((VirtualDatabaseConsole) module).getTimeout());
126
127       long start = System.currentTimeMillis();
128       long end;
129       ResultSet JavaDoc rs = cs.executeQuery();
130       end = System.currentTimeMillis();
131       ResultSetFormatter.formatAndDisplay(rs, fetchsize, console);
132       console.println(ConsoleTranslate.get("sql.display.query.time",
133           new Long JavaDoc[]{new Long JavaDoc((end - start) / 1000),
134               new Long JavaDoc((end - start) % 1000)}));
135     }
136     catch (Exception JavaDoc e)
137     {
138       console.printError(ConsoleTranslate.get(
139           "sql.command.call.stored.procedure.failed", e), e);
140     }
141     finally
142     {
143       try
144       {
145         cs.close();
146       }
147       catch (Exception JavaDoc ignore)
148       {
149       }
150     }
151   }
152 }
Popular Tags