comparison example/PreparedExample.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children 073ee535234b b9b35ca2eec2
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
7 */
8
9 import java.sql.*;
10
11 /**
12 * This example shows the use of the PreparedStatement
13 *
14 * @author Fabian Groffen
15 */
16 public class PreparedExample {
17 public static void main(String[] args) throws Exception {
18 // make sure the driver is loaded
19 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
20 Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/notused", "monetdb", "monetdb");
21 PreparedStatement st = con.prepareStatement("SELECT ? AS a1, ? AS a2");
22 ResultSet rs;
23
24 st.setString(1, "te\\s't");
25 st.setInt(2, 10);
26
27 rs = st.executeQuery();
28 // get meta data and print columns with their type
29 ResultSetMetaData md = rs.getMetaData();
30 for (int i = 1; i <= md.getColumnCount(); i++) {
31 System.out.print(md.getColumnName(i) + ":" +
32 md.getColumnTypeName(i) + "\t");
33 }
34 System.out.println("");
35
36 while (rs.next()) {
37 for (int j = 1; j <= md.getColumnCount(); j++) {
38 System.out.print(rs.getString(j) + "\t");
39 }
40 System.out.println("");
41 }
42
43 con.close();
44 }
45 }