Book Contents

Book Index

Next Topic

Home

Sample Application

This sample application assumes that it is running on the same machine as the Vertica instance and that your username is devel.

import java.sql.*;

// Create a table, create a projection, insert a row,

// query the table, and drop the table (including the projection)

class jdbc_test

{

// Static SQL statements

static String create_table =

"CREATE TABLE VTEST (COLUMN_1 CHAR(50));";

static String create_projection =

"CREATE PROJECTION VTEST_PROJ (COLUMN_1) AS SELECT COLUMN_1 FROM VTEST;";

static String insert_row =

"INSERT INTO VTEST VALUES ('Testing Vertica');";

static String select_row =

"SELECT * FROM VTEST;";

static String drop_table =

"DROP TABLE VTEST CASCADE;";

public static void main(String args[]) throws Exception

{

//try to load the class

Class.forName("com.vertica.Driver");

//get a connection to the database

Connection db = DriverManager.getConnection

("jdbc:vertica://localhost:5433/testdb", "devel", "");

//create a statement object

Statement st = db.createStatement();

//execute SQL statements

st.executeUpdate(create_table);

st.executeUpdate(create_projection);

st.executeQuery(insert_row);

// print out the result set

ResultSet rs = st.executeQuery(select_row);

while(rs.next())

{

System.out.println(rs.getObject(1));

}

//clean up

st.executeUpdate(drop_table);

rs.close();

st.close();

db.close();

}

}