Mercurial > hg > monetdb-java
comparison example/SQLcopyinto.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 | f1de7262d8d9 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 import java.io.*; | |
11 import java.util.*; | |
12 import nl.cwi.monetdb.mcl.net.*; | |
13 import nl.cwi.monetdb.mcl.io.*; | |
14 | |
15 /** | |
16 * This example demonstrates how the MonetDB JDBC driver can facilitate | |
17 * in performing COPY INTO sequences. This is mainly meant to show how | |
18 * a quick load can be performed from Java. | |
19 * | |
20 * @author Fabian Groffen | |
21 */ | |
22 | |
23 public class SQLcopyinto { | |
24 public static void main(String[] args) throws Exception { | |
25 // make sure the driver is loaded | |
26 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver"); | |
27 // request a connection suitable for Monet from the driver manager | |
28 // note that the database specifier is currently not implemented, for | |
29 // Monet itself can't access multiple databases. | |
30 // turn on debugging | |
31 Connection con = DriverManager.getConnection("jdbc:monetdb://localhost/database", "monetdb", "monetdb"); | |
32 | |
33 // get a statement to execute on | |
34 Statement stmt = con.createStatement(); | |
35 | |
36 String query = "CREATE TABLE example (id int, val varchar(24))"; | |
37 try { | |
38 stmt.execute(query); | |
39 } catch (SQLException e) { | |
40 System.out.println(query + ": " + e.getMessage()); | |
41 System.exit(1); | |
42 } | |
43 | |
44 // now create a connection manually to perform a load, this can | |
45 // of course also be done simultaneously with the JDBC | |
46 // connection being kept connected | |
47 | |
48 MapiSocket server = new MapiSocket(); | |
49 | |
50 server.setDatabase("database"); | |
51 server.setLanguage("sql"); | |
52 | |
53 try { | |
54 List warning = | |
55 server.connect("localhost", 50000, "monetdb", "monetdb"); | |
56 if (warning != null) { | |
57 for (Iterator it = warning.iterator(); it.hasNext(); ) { | |
58 System.out.println(it.next().toString()); | |
59 } | |
60 } | |
61 | |
62 BufferedMCLReader in = server.getReader(); | |
63 BufferedMCLWriter out = server.getWriter(); | |
64 | |
65 String error = in.waitForPrompt(); | |
66 if (error != null) | |
67 throw new Exception(error); | |
68 | |
69 query = "COPY INTO example FROM STDIN USING DELIMITERS ',','\\n';"; | |
70 // the leading 's' is essential, since it is a protocol | |
71 // marker that should not be omitted, likewise the | |
72 // trailing semicolon | |
73 out.write('s'); | |
74 out.write(query); | |
75 out.newLine(); | |
76 for (int i = 0; i < 100; i++) { | |
77 out.write("" + i + ",val_" + i); | |
78 out.newLine(); | |
79 } | |
80 out.writeLine(""); // need this one for synchronisation over flush() | |
81 error = in.waitForPrompt(); | |
82 if (error != null) | |
83 throw new Exception(error); | |
84 // disconnect from server | |
85 server.close(); | |
86 } catch (IOException e) { | |
87 System.err.println("unable to connect: " + e.getMessage()); | |
88 System.exit(-1); | |
89 } catch (Exception e) { | |
90 System.err.println(e.getMessage()); | |
91 System.exit(-1); | |
92 } | |
93 | |
94 query = "SELECT COUNT(*) FROM example"; | |
95 ResultSet rs = null; | |
96 try { | |
97 rs = stmt.executeQuery(query); | |
98 } catch (SQLException e) { | |
99 System.out.println(query + ": " + e.getMessage()); | |
100 System.exit(1); | |
101 } | |
102 if (rs != null && rs.next()) | |
103 System.out.println(rs.getString(1)); | |
104 | |
105 // free resources, close the statement | |
106 stmt.close(); | |
107 // close the connection with the database | |
108 con.close(); | |
109 | |
110 } | |
111 } |