Mercurial > hg > monetdb-java
comparison tests/Test_PSgeneratedkeys.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 | 04fbf3655452 |
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 public class Test_PSgeneratedkeys { | |
12 public static void main(String[] args) throws Exception { | |
13 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver"); | |
14 Connection con = DriverManager.getConnection(args[0]); | |
15 con.setAutoCommit(false); | |
16 // >> false: auto commit was just switched off | |
17 System.out.println("0. false\t" + con.getAutoCommit()); | |
18 | |
19 try { | |
20 Statement stmt = con.createStatement(); | |
21 stmt.executeUpdate( | |
22 "CREATE TABLE psgenkey (" + | |
23 " id serial," + | |
24 " val varchar(20)" + | |
25 ")"); | |
26 stmt.close(); | |
27 } catch (SQLException e) { | |
28 System.out.println(e); | |
29 System.out.println("Creation of test table failed! :("); | |
30 System.out.println("ABORTING TEST!!!"); | |
31 System.exit(-1); | |
32 } | |
33 | |
34 try { | |
35 PreparedStatement pstmt = con.prepareStatement( | |
36 "INSERT INTO psgenkey (val) VALUES ('this is a test')", | |
37 Statement.RETURN_GENERATED_KEYS); | |
38 | |
39 System.out.print("1. inserting a record..."); | |
40 pstmt.executeUpdate(); | |
41 System.out.println("success :)"); | |
42 | |
43 // now get the generated keys | |
44 System.out.print("2. getting generated keys..."); | |
45 ResultSet keys = pstmt.getGeneratedKeys(); | |
46 if (keys == null || !keys.next()) { | |
47 System.out.println("there are no keys! :("); | |
48 System.out.println("ABORTING TEST!!!"); | |
49 System.exit(-1); | |
50 } | |
51 | |
52 System.out.println("generated key index: " + keys.getInt(1)); | |
53 while (keys.next()) { | |
54 System.out.println("generated key index: " + keys.getInt(1)); | |
55 } | |
56 | |
57 if (keys.getStatement() == null) { | |
58 System.out.println("ResultSet.getStatement() should never return null!"); | |
59 } | |
60 | |
61 keys.close(); | |
62 pstmt.close(); | |
63 } catch (SQLException e) { | |
64 System.out.println("FAILED :( "+ e.getMessage()); | |
65 System.out.println("ABORTING TEST!!!"); | |
66 } | |
67 | |
68 con.rollback(); | |
69 con.close(); | |
70 } | |
71 } |