Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.3.0 |  CUBRID 8.3.1 |  CUBRID 8.4.0 |  CUBRID 8.4.1 |  CUBRID 8.4.3 |  CUBRID 9.0.0 | 

Calling from Java Application

To call a Java stored function/procedure from a Java application, use a CallableStatement object.

Create a phone class in the CUBRID database.

CREATE TABLE phone(

     name varchar(20),

     phoneno varchar(20)

)

Compile the following PhoneNumber.java file, load the Java class file into CUBRID, and publish it.

import java.sql.*;

import java.io.*;

public class PhoneNumber{    public static void Phone(String name, String phoneno) throws Exception{

        String sql="INSERT INTO PHONE(NAME, PHONENO)"+ "VALUES (?, ?)";

        try{

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

            Connection conn = DriverManager.getConnection("jdbc:default:connection:");

            PreparedStatement pstmt = conn.prepareStatement(sql);

       

            pstmt.setString(1, name);

            pstmt.setString(2, phoneno);

            pstmt.executeUpdate();

 

            pstmt.close();

            conn.commit();

            conn.close();

        } catch (SQLException e) {

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

        }

    }

}

create PROCEDURE phone_info(name varchar, phoneno varchar)

as language java

name 'PhoneNumber.Phone(java.lang.String, java.lang.String)';

Create and run the following Java application.

import java.sql.*;

public class StoredJDBC{

    public static void main(){

        Connection conn = null;

        Statement stmt= null;

        int result;

        int i;

 

        try{

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

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

 

            CallableStatement cs;

            cs = conn.prepareCall("call PHONE_INFO(?, ?)");

 

            cs.setString(1, "Jane");

            cs.setString(2, "010-1111-1111");

            cs.executeUpdate();

 

            conn.commit();

            cs.close();

            conn.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Retrieve the phone class after executing the program above; the following result would be displayed.

SELECT * from phone;

name                  phoneno

============================================

    'Jane'                '010-111-1111'