Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.4.3 |  CUBRID 9.0.0 | 

JDBC Sample Program

The following sample shows how to connect to CUBRID by using the JDBC driver, and retrieve and insert 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 automatically created during the installation.

Loading JDBC Driver

To connect to CUBRID, load the JDBC driver by using the forName() method of the Class. For more information, see Overview of the JDBC driver.

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

Connecting to Database

After loading the JDBC driver, use the getConnection() method of the DriverManager to connect to the database. To create a Connection object, you must specify information such as the URL which indicates the location of a database, user name, password, etc. For more information, see Connection Configuration.

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

String userid = "dba";

String password = "";

 

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

Manipulating Database (Executing Queries and Processing ResultSet)

To send a query statement to the connected database and execute it, create Statement, PrepardStatement, and CallableStatemen objects. After the Statement object is created, execute the query statement by using executeQuery() or executeUpdate() method of the Statement object. You can use the next() method to process the next row from the ResultSet that has been returned as a result of executing the executeQuery() method.

Note If you execute commit after query execution, ResultSet will be automatically closed. Therefore, you must not use ResultSet after commit. Generally CUBRID is executed in auto-commit mode; if you do not want for CUBRID being executed in auto-commit mode, you should specify conn.setAutocommit(false); in the code.

Disconnecting from Database

You can disconnect from a database by executing the close() method for each object.

Example 1

The following example shows how to connect to the demodb database, create a table, execute a query statement with the prepared statement, and roll back the query statement. You can also practice it yourself by appropriately modifying argument values of the getConnection() method.

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:33000: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();

      }

   }

}

Example 2

The following example shows how to execute the SELECT statement by connecting to demodb which is automatically created when installing CUBRID.

import java.sql.*;

public class SelectData {

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

       Connection conn = null;

       Statement stmt = null;

       ResultSet rs = null;

       try {

       // CUBRID에 Connect

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

       conn = DriverManager.getConnection("jdbc:cubrid:localhost:33000: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(" ========================================= ");

       }

       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();

       }

   }

}

Example 3

The following example shows how to execute the INSERT statement by connecting to demodb which is automatically created when installing CUBRID. You can delete or update data the same way as you insert data so you can reuse the code below by simply modifying the query statement in the code.

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:33000: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 entered.");

           stmt.close();

       } catch ( SQLException e ) {

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

       } catch ( Exception e ) {

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

       } finally {

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

       }

   }

}

Note To get the latest information about JDBC driver, click http://www.cubrid.org/wiki_apis/entry/cubrid-jdbc-driver.