forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstTry.java
More file actions
158 lines (135 loc) · 4.66 KB
/
FirstTry.java
File metadata and controls
158 lines (135 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//STEP 1. Import required packages
import java.math.BigDecimal;
import java.sql.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FirstTry {
final static Logger logger = LoggerFactory.getLogger(FirstTry.class);
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mapd.jdbc.MapDDriver";
static final String DB_URL = "jdbc:mapd:localhost:9092:mapd:http";
// Database credentials
static final String USER = "mapd";
static final String PASS = "HyperInteractive";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 3: Open a connection
logger.info("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql;
// logger.info("Doing prepared statement");
//
PreparedStatement ps = null;
ResultSet rs = null;
// sql = "INSERT INTO alltypes (b1, s1, i1, b2, f1, d1, c1, v1, t1, t2, t3, d2) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// ps = conn.prepareStatement(sql);
//
// ps.setBoolean(1, true);
// ps.setShort(2, (short)1);
// ps.setInt(3, 20);
// ps.setInt(4, 400);
// ps.setFloat(5, (float)4000.04);
// ps.setBigDecimal(6, new BigDecimal(12.2));
// ps.setString(7, "String1");
// ps.setString(8, "String2");
// ps.setString(9, "String3");
// ps.setTime(10, new Time(0));
// ps.setTimestamp(11, new Timestamp(0));
// ps.setDate(12, new Date(0));
//
// ResultSet rs = ps.executeQuery();
//STEP 4: Execute a query
logger.info("Creating statement...");
stmt = conn.createStatement();
sql = "SELECT uniquecarrier from flights_2008_7M limit 5";
rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while (rs.next()) {
//Retrieve by column name
//int id = rs.getInt("id");
//int age = rs.getInt("age");
String uniquecarrier = rs.getString("uniquecarrier");
//String last = rs.getString("last");
//Display values
logger.info("uniquecarrier: " + uniquecarrier);
//logger.info(", Age: " + age);
//logger.info(", First: " + first);
//logger.info(", Last: " + last);
}
logger.info("Doing prepared statement");
ps = null;
sql = "SELECT uniquecarrier from flights limit 5";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//STEP 5: Extract data from result set
while (rs.next()) {
//Retrieve by column name
//int id = rs.getInt("id");
//int age = rs.getInt("age");
String uniquecarrier = rs.getString("uniquecarrier");
//String last = rs.getString("last");
//Display values
logger.info("PS uniquecarrier: " + uniquecarrier);
//logger.info(", Age: " + age);
//logger.info(", First: " + first);
//logger.info(", Last: " + last);
}
DatabaseMetaData dbmeta = conn.getMetaData();
rs = dbmeta.getSchemas();
while (rs.next()) {
String schema = rs.getString("TABLE_SCHEM");
logger.info("TABLE_SCHEM: " + schema);
}
rs = dbmeta.getTableTypes();
while (rs.next()) {
String schema = rs.getString(1);
logger.info("TYPE: " + schema);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}//end finally try
}//end try
logger.info("Goodbye!");
}//end main
}//end FirstExample