Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page:

JDBC Sample

The following is a simple example that connects to CUBRID by using the JDBC driver and retrieves and inserts data. To run the sample program, make sure that the database you are trying to connect to and the CUBRID Broker are running. In the sample, you will use the demodb database that is created automatically during the installation.

JDBC Driver Load

To connect to CUBRID, load the JDBC driver using the for Name() method provided in the class. For more information, see the CUBRID JDBC Driver.

Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

How to Make the Connection to Database

When the JDBC driver is loaded, use the getConnection() method provided in the DriverManager to connect to the database. To create a Connection object, you must specify the url for describing the location of the database, database user name, password, etc. For more information, see the Connection Configuration.

String url = "jdbc:cubrid:localhost:30000:demodb:::";

String userid = "dba";

String password = "";

 

Connection conn = DriverManager.getConnection(url,userid,password);  

Manipulating database (executing queries and processing the ResultSet)

To send a query statement to the connected database and execute it, create the Statement, PrepardStatement, and CallableStatement objects. When a statement object has been created, execute the query using the executeQuery() method or the executeUpdate() method for the statement object. The next() method can process the following row from the ResultSet that is returned from the executeQuery() method. For more information, see the BRID JDBC Driver.

Note If you execute commit after query execution, ResultSet is automatically closed. Therefore, you must not use ResultSet after commit. CUBRID is, in general, executed in auto-commit mode. If you does not want auto-commit mode, you must state conn.setAutocommit(false); in the code.

Disconnecting from the database

Each method can be disconnected from the database by executing the close() method.

JDBC Sample 1

The sample code shown below creates a table, executes a query with a prepared statement, and then rolls back the query. Modify the parameter value of the getConnection() method for practice.

import java.util.*;

import java.sql.*;

 

public class Basic {

   public static Connection connect() {

      Connection conn = null;

      try {

           Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

           conn = DriverManager.getConnection("jdbc:cubrid:localhost:30000:demodb:::","dba","");

           conn.setAutoCommit (false) ;

      } catch ( Exception e ) {

           System.err.println("SQLException : " + e.getMessage());

      }

      return conn;

   }

 

   public static void printdata(ResultSet rs) {

      try {

          ResultSetMetaData rsmd = null;

 

          rsmd = rs.getMetaData();

          int numberofColumn = rsmd.getColumnCount();

 

          while (rs.next ()) {

              for(int j=1; j<=numberofColumn; j++ )  

                  System.out.print(rs.getString(j) + "  " );

              System.out.println("");

          }

      } catch ( Exception e ) {

           System.err.println("SQLException : " + e.getMessage());

      }

   }

 

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

      Connection conn = null;

      Statement stmt = null;

      ResultSet rs = null;

      PreparedStatement preStmt = null;

 

      try {

           conn = connect();

 

           stmt = conn.createStatement();

           stmt.executeUpdate("create class xoo ( a int, b int, c char(10))");

 

           preStmt = conn.prepareStatement("insert into xoo values(?,?,''''100'''')");

           preStmt.setInt (1, 1) ;

           preStmt.setInt (2, 1*10) ;

           int rst = preStmt.executeUpdate () ;

 

           rs = stmt.executeQuery("select a,b,c from xoo" );

 

           printdata(rs);

 

           conn.rollback();

           stmt.close();

           conn.close();

      } catch ( Exception e ) {

           conn.rollback();

           System.err.println("SQLException : " + e.getMessage());

      } finally {

           if ( conn != null ) conn.close();

      }

   }

}

 JDBC Sample 2

The following is an example of executing SELECT statement by connecting to demodb that is provided by CUBRID during installation.

import java.sql.*;

public class SelectData {

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

       Connection conn = null;

       Statement stmt = null;

       ResultSet rs = null;

       try {

       // Connect to CUBRID

       Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

       conn = DriverManager.getConnection("jdbc:CUBRID:localhost:30000:demodb:::","dba","");

       String sql = "select name, players from event";

       stmt = conn.createStatement();

       rs = stmt.executeQuery(sql);

       while(rs.next()) {

           String name = rs.getString("name");

           String players = rs.getString("players");

           System.out.println("name ==> " + name);

           System.out.println("Number of players==> " + players);

           System.out.println("\n=========================================\n");

       }

       rs.close();

       stmt.close();

       conn.close();

       } catch ( SQLException e ) {

           System.err.println(e.getMessage());

       } catch ( Exception e ) {

           System.err.println(e.getMessage());

       } finally {

           if ( conn != null ) conn.close();

       }

   }

}

JDBC Example 3

The following is an example of executing INSERT statement by connecting to demodb that is provided by CUBRID during installation. You can delete or modify data the same way as you insert data. This means that you can reuse the code below by simply changing the query statements.

import java.sql.*;

public class insertData {

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

       Connection conn = null;

       Statement stmt = null;

       try {

           // CUBRID Connect

           Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

           conn = DriverManager.getConnection("jdbc:cubrid:localhost:30000:demodb:::","dba","");

           String sql = "insert into olympic(host_year, host_nation, host_city, opening_date,            closing_date) values (2008, 'China', 'Beijing', to_date('08-08-2008','mm-dd-yyyy'),            to_date('08-24-2008','mm-dd-yyyy'))";

           stmt = conn.createStatement();

           stmt.executeUpdate(sql);

           System.out.println("Data is inserted.");

           stmt.close();

       } catch ( SQLException e ) {

           System.err.println(e.getMessage());

       } catch ( Exception e ) {

           System.err.println(e.getMessage());

       } finally {

           if ( conn != null ) conn.close();

       }

   }

}