OmniSciDB  04ee39c94c
SampleJDBC.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.sql.Connection;
18 import java.sql.DriverManager;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.sql.Statement;
22 import java.util.Properties;
23 
24 public class SampleJDBC {
25  // JDBC driver name and database URL
26  static final String JDBC_DRIVER = "com.omnisci.jdbc.OmniSciDriver";
27  static final String DB_URL = "jdbc:omnisci:localhost:6274:mapd";
28 
29  // Connection protocol http|https|binary
30  static final String PROTOCOL = "binary";
31 
32  // Database credentials
33  static final String USER = "mapd";
34  static final String PASS = "HyperInteractive";
35 
36  public static void main(String[] args) {
37  Connection conn = null;
38  Statement stmt = null;
39  try {
40  // STEP 1: Register JDBC driver
41  Class.forName(JDBC_DRIVER);
42 
43  // STEP 2: Open a connection
44  Properties pt = new Properties();
45  pt.setProperty("user", USER);
46  pt.setProperty("password", PASS);
47  pt.setProperty("protocol", PROTOCOL);
48  conn = DriverManager.getConnection(DB_URL, pt);
49 
50  // STEP 3: Execute a query
51  stmt = conn.createStatement();
52 
53  String sql = "SELECT uniquecarrier from flights_2008_10k"
54  + " GROUP BY uniquecarrier limit 5";
55  ResultSet rs = stmt.executeQuery(sql);
56 
57  // STEP 4: Extract data from result set
58  while (rs.next()) {
59  String uniquecarrier = rs.getString("uniquecarrier");
60  System.out.println("uniquecarrier: " + uniquecarrier);
61  }
62 
63  // STEP 5: Clean-up environment
64  rs.close();
65  stmt.close();
66  conn.close();
67  } catch (SQLException se) {
68  // Handle errors for JDBC
69  se.printStackTrace();
70  } catch (Exception e) {
71  // Handle errors for Class.forName
72  e.printStackTrace();
73  } finally {
74  // finally block used to close resources
75  try {
76  if (stmt != null) {
77  stmt.close();
78  }
79  } catch (SQLException se2) {
80  } // nothing we can do
81  try {
82  if (conn != null) {
83  conn.close();
84  }
85  } catch (SQLException se) {
86  se.printStackTrace();
87  } // end finally try
88  } // end try
89  } // end main
90 } // end SampleJDBC
static final String PASS
Definition: SampleJDBC.java:34
static final String PROTOCOL
Definition: SampleJDBC.java:30
auto sql(const std::string &sql_stmts)
static final String USER
Definition: SampleJDBC.java:33
static final String DB_URL
Definition: SampleJDBC.java:27
static final String JDBC_DRIVER
Definition: SampleJDBC.java:26
static void main(String[] args)
Definition: SampleJDBC.java:36