CUBRID学习笔记 38 net调用java的函数过程

首先要设置java_stored_procedure 为yes  该配置项在cubrid.conf中

书写并编译java代码

public class SpCubrid{

     public static String HelloCubrid() {
         return "Hello, Cubrid !!";
     }
     public static int SpInt(int i) {
         return i + 1;
     }
     public static void outTest(String[] o) {
         o0 = "Hello, CUBRID";
     }
 }
 
%javac SpCubrid.java
加载java到数据库
 
% loadjava demodb
 
在数据库中发布java方法
create function hello() return string
as language java
name 'SpCubrid.HelloCubrid() return java.lang.String';
 
在net中调用
2
3
call Hello() into :HELLO;
call Sp_int(3) into :i;
call phone_info('Tom','016-111-1111');

下面看个完整的代码

创建java方法

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using CUBRID.Data.CUBRIDClient;
  
 namespace Sample
 {
     class ProcedureSample
     {
         /* conection string */
         /* Please modify before using. */
         static readonly string _connString = "server=127.0.0.1;database=demodb;port=33000;user=public;password=";
  
                 /*
         * create a new java stored function
         */
        public void create_function()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
                string sql = "CREATE FUNCTION unit_hello(a int) RETURN int AS LANGUAGE JAVA NAME " +
                    "'SpCubrid.SpInt(int) return java.lang.Integer;'";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
 
        /*
         * create a new java stored procedure
         */
        public void create_procedure()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
 
                string sql = "create PROCEDURE UNIT_PHONE_INFO(name varchar, phoneno varchar)" +
                             "as language java name 'SpCubrid.AddPhone(java.lang.String,java.lang.String)';";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
     }
 }

调用

using System.Data;

using CUBRID.Data.CUBRIDClient;
 
namespace Sample
{
    class ProcedureSample
    {
        /* conection string */
        /* Please modify before using. */
        static readonly string _connString = "server=127.0.0.1;database=demodb;port=33000;user=public;password=";
 
        /*
         * call procedure
         */
        public void call_procedure()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
                string sql = "CALL UNIT_PHONE_INFO('Tom', '03556315315');";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
 
        /*
         * call function, the function has a return value
         */
        public void call_function()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
                string sql = "? = CALL unit_hello(10)";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
 
                    CUBRIDParameter p1 = new CUBRIDParameter("?p1", CUBRIDDataType.CCI_U_TYPE_INT);
                    p1.Direction = ParameterDirection.Output;   // output
                    cmd.Parameters.Add(p1);
 
                    cmd.ExecuteNonQuery();
 
                    // do something with p1.value
                }
            }
        }
    }
}

删除

using CUBRID.Data.CUBRIDClient;

 
namespace Sample
{
    class ProcedureSample
    {
        /* conection string */
        /* Please modify before using. */
        static readonly string _connString = "server=127.0.0.1;database=demodb;port=33000;user=public;password=";
 
        /*
         * drop java stored function
         */
        public void drop_function()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
                string sql = "drop function unit_hello";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
 
        /*
         * drop java stored procedure
         */
        public void drop_procedure()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = ProcedureSample._connString;
                conn.Open();
 
                string sql = "DROP PROCEDURE UNIT_PHONE_INF";
                using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
上一篇:C-printf/sprintf/snprintf中的类型转换详解


下一篇:2、初探 ZooKeeper 技术内幕