OmniSciDB  04ee39c94c
Batch.java
Go to the documentation of this file.
1 import java.sql.*;
2 
3 public class Batch {
4  // JDBC driver name and database URL
5  static final String JDBC_DRIVER = "com.mapd.jdbc.MapDDriver";
6  static final String DB_URL = "jdbc:mapd:myHost.com:6274:mapd";
7 
8  // Database credentials
9  static final String USER = "myUserName";
10  static final String PASS = "myPassword";
11 
12  public static void main(String[] args) {
13  Connection conn = null;
14  Statement stmt = null;
15  PreparedStatement pstmt = null;
16 
17  try {
18  // STEP 1: Register the JDBC driver
19  Class.forName(JDBC_DRIVER);
20 
21  // STEP 2: Open a connection
22  conn = DriverManager.getConnection(DB_URL, USER, PASS);
23  stmt = conn.createStatement();
24  stmt.executeUpdate(
25  "create table girl_groups(name1 varchar(20), group_rank integer, name2 varchar(20))");
26  stmt.close();
27  conn.setAutoCommit(false);
28 
29  pstmt = conn.prepareStatement("insert into girl_groups values(?,?,?)");
30  pstmt.setString(1, "Orange Caramel");
31  pstmt.setInt(2, 5);
32  pstmt.setString(3, "2NE1");
33  pstmt.addBatch();
34  pstmt.setString(1, "4Minute");
35  pstmt.setInt(2, 6);
36  pstmt.setString(3, "EXID");
37  pstmt.addBatch();
38  pstmt.executeBatch();
39  conn.commit();
40  pstmt.close();
41  conn.close();
42  } catch (SQLException se) {
43  // Handle errors for JDBC
44  se.printStackTrace();
45  } catch (Exception e) {
46  // Handle errors for Class.forName
47  e.printStackTrace();
48  } finally {
49  // finally block used to close resources
50  try {
51  if (pstmt != null) {
52  stmt.close();
53  pstmt.close();
54  }
55  } catch (SQLException se2) {
56  } // nothing we can do
57  try {
58  if (conn != null) {
59  conn.close();
60  }
61  } catch (SQLException se) {
62  se.printStackTrace();
63  } // end finally try
64  } // end try
65  } // end main
66 } // end SampleJDBC
static final String DB_URL
Definition: Batch.java:6
Definition: Batch.java:3
static void main(String[] args)
Definition: Batch.java:12
static final String PASS
Definition: Batch.java:10
static final String USER
Definition: Batch.java:9
static final String JDBC_DRIVER
Definition: Batch.java:5