comparison tests/Test_PSgetObject.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_PSgetObject {
12 public static void main(String[] args) throws Exception {
13 Class.forName("nl.cwi.monetdb.jdbc.MonetDriver");
14 final 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 final Statement stmt = con.createStatement();
20 try {
21 System.out.print("1. creating test table...");
22 stmt.executeUpdate("CREATE TABLE table_Test_PSgetObject (ti tinyint, si smallint, i int, bi bigint)");
23 stmt.close();
24 System.out.println(" passed :)");
25 } catch (SQLException e) {
26 System.out.println(e);
27 System.out.println("Creation of test table failed! :(");
28 System.out.println("ABORTING TEST!!!");
29 System.exit(-1);
30 }
31
32 PreparedStatement pstmt;
33 try {
34 System.out.print("2a. inserting 3 records as batch...");
35 pstmt = con.prepareStatement("INSERT INTO table_Test_PSgetObject (ti,si,i,bi) VALUES (?,?,?,?)");
36 pstmt.setShort(1, (short)1);
37 pstmt.setShort(2, (short)1);
38 pstmt.setInt (3, 1);
39 pstmt.setLong(4, (long)1);
40 pstmt.addBatch();
41
42 pstmt.setShort(1, (short)127);
43 pstmt.setShort(2, (short)12700);
44 pstmt.setInt (3, 1270000);
45 pstmt.setLong(4, (long)127000000);
46 pstmt.addBatch();
47
48 pstmt.setShort(1, (short)-127);
49 pstmt.setShort(2, (short)-12700);
50 pstmt.setInt (3, -1270000);
51 pstmt.setLong(4, (long)-127000000);
52 pstmt.addBatch();
53
54 pstmt.executeBatch();
55 System.out.println(" passed :)");
56
57 System.out.print("2b. closing PreparedStatement...");
58 pstmt.close();
59 System.out.println(" passed :)");
60 } catch (SQLException e) {
61 System.out.println("FAILED to INSERT data:( "+ e.getMessage());
62 while ((e = e.getNextException()) != null)
63 System.out.println("FAILED :( " + e.getMessage());
64 System.out.println("ABORTING TEST!!!");
65 }
66
67 try {
68 System.out.print("3a. selecting records...");
69 pstmt = con.prepareStatement("SELECT ti,si,i,bi FROM table_Test_PSgetObject ORDER BY ti,si,i,bi");
70 ResultSet rs = pstmt.executeQuery();
71 System.out.println(" passed :)");
72
73 while (rs.next()) {
74 // test fix for https://www.monetdb.org/bugzilla/show_bug.cgi?id=4026
75 Short ti = (Short) rs.getObject(1);
76 Short si = (Short) rs.getObject(2);
77 Integer i = (Integer) rs.getObject(3);
78 Long bi = (Long) rs.getObject(4);
79
80 System.out.println(" Retrieved row data: ti=" + ti + " si=" + si + " i=" + i + " bi=" + bi);
81 }
82
83 System.out.print("3b. closing ResultSet...");
84 rs.close();
85 System.out.println(" passed :)");
86
87 System.out.print("3c. closing PreparedStatement...");
88 pstmt.close();
89 System.out.println(" passed :)");
90 } catch (SQLException e) {
91 System.out.println("FAILED to RETRIEVE data:( "+ e.getMessage());
92 while ((e = e.getNextException()) != null)
93 System.out.println("FAILED :( " + e.getMessage());
94 System.out.println("ABORTING TEST!!!");
95 }
96
97 System.out.print("4. Rollback changes...");
98 con.rollback();
99 System.out.println(" passed :)");
100
101 System.out.print("5. Close connection...");
102 con.close();
103 System.out.println(" passed :)");
104 }
105 }