OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FirstTry.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 // STEP 1. Import required packages
18 
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
22 import java.math.BigDecimal;
23 import java.sql.*;
24 
25 public class FirstTry {
26  final static Logger logger = LoggerFactory.getLogger(FirstTry.class);
27 
28  // JDBC driver name and database URL
29  static final String JDBC_DRIVER = "com.omnisci.jdbc.OmniSciDriver";
30  static final String DB_URL = "jdbc:omnisci:localhost:6273:omnisci:http";
31 
32  // Database credentials
33  static final String USER = "admin";
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 2: Register JDBC driver
41  Class.forName(JDBC_DRIVER);
42 
43  // STEP 3: Open a connection
44  logger.info("Connecting to database...");
45  conn = DriverManager.getConnection(DB_URL, USER, PASS);
46 
47  String sql;
48 
49  // logger.info("Doing prepared statement");
50  //
51  PreparedStatement ps = null;
52  ResultSet rs = null;
53  // sql = "INSERT INTO alltypes (b1, s1, i1, b2, f1, d1, c1, v1, t1, t2, t3, d2)
54  // VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; ps =
55  // conn.prepareStatement(sql);
56  //
57  // ps.setBoolean(1, true);
58  // ps.setShort(2, (short)1);
59  // ps.setInt(3, 20);
60  // ps.setInt(4, 400);
61  // ps.setFloat(5, (float)4000.04);
62  // ps.setBigDecimal(6, new BigDecimal(12.2));
63  // ps.setString(7, "String1");
64  // ps.setString(8, "String2");
65  // ps.setString(9, "String3");
66  // ps.setTime(10, new Time(0));
67  // ps.setTimestamp(11, new Timestamp(0));
68  // ps.setDate(12, new Date(0));
69  //
70 
71  // ResultSet rs = ps.executeQuery();
72 
73  // STEP 4: Execute a query
74  logger.info("Creating statement...");
75  stmt = conn.createStatement();
76 
77  sql = "SELECT uniquecarrier from flights_2008_7M limit 5";
78  rs = stmt.executeQuery(sql);
79 
80  // STEP 5: Extract data from result set
81  while (rs.next()) {
82  // Retrieve by column name
83  // int id = rs.getInt("id");
84  // int age = rs.getInt("age");
85  String uniquecarrier = rs.getString("uniquecarrier");
86  // String last = rs.getString("last");
87 
88  // Display values
89  logger.info("uniquecarrier: " + uniquecarrier);
90  // logger.info(", Age: " + age);
91  // logger.info(", First: " + first);
92  // logger.info(", Last: " + last);
93  }
94  logger.info("Doing prepared statement");
95 
96  ps = null;
97  sql = "SELECT uniquecarrier from flights limit 5";
98  ps = conn.prepareStatement(sql);
99 
100  rs = ps.executeQuery();
101 
102  // STEP 5: Extract data from result set
103  while (rs.next()) {
104  // Retrieve by column name
105  // int id = rs.getInt("id");
106  // int age = rs.getInt("age");
107  String uniquecarrier = rs.getString("uniquecarrier");
108  // String last = rs.getString("last");
109 
110  // Display values
111  logger.info("PS uniquecarrier: " + uniquecarrier);
112  // logger.info(", Age: " + age);
113  // logger.info(", First: " + first);
114  // logger.info(", Last: " + last);
115  }
116 
117  DatabaseMetaData dbmeta = conn.getMetaData();
118 
119  rs = dbmeta.getSchemas();
120  while (rs.next()) {
121  String schema = rs.getString("TABLE_SCHEM");
122  logger.info("TABLE_SCHEM: " + schema);
123  }
124 
125  rs = dbmeta.getTableTypes();
126  while (rs.next()) {
127  String schema = rs.getString(1);
128  logger.info("TYPE: " + schema);
129  }
130 
131  // STEP 6: Clean-up environment
132  rs.close();
133  stmt.close();
134  conn.close();
135  } catch (SQLException se) {
136  // Handle errors for JDBC
137  se.printStackTrace();
138  } catch (Exception e) {
139  // Handle errors for Class.forName
140  e.printStackTrace();
141  } finally {
142  // finally block used to close resources
143  try {
144  if (stmt != null) {
145  stmt.close();
146  }
147  } catch (SQLException se2) {
148  } // nothing we can do
149  try {
150  if (conn != null) {
151  conn.close();
152  }
153  } catch (SQLException se) {
154  se.printStackTrace();
155  } // end finally try
156  } // end try
157  logger.info("Goodbye!");
158  } // end main
159 } // end FirstExample
static final String JDBC_DRIVER
Definition: FirstTry.java:29
static final String USER
Definition: FirstTry.java:33
static void main(String[] args)
Definition: FirstTry.java:36
static final Logger logger
Definition: FirstTry.java:26
static final String DB_URL
Definition: FirstTry.java:30
static final String PASS
Definition: FirstTry.java:34