Use Java functions in Sybase

Source: Internet
Author: User
Tags sybase sybase database

Functions are not supported in versions earlier than sybase12.5, but sometimes we need some functions, not just processes. Java can be used here, in addition, writing Java is certainly much better than writing a stored procedure. the ability or type of Java to process data will make Sybase shine a lot. The following is an article on the Internet that describes how to use Java in Sybase. But I want to explain it first. I am using version 12.5.4, and I am also prompted that the Java function is successfully enabled, however, the JAR file cannot be submitted to the database, which may be related to the operating system or database version.

-----------------------------------------------------

I have used 12.5 and downloaded it from the Sybase website (The Development version has a limited number of concurrent threads). I should be able to develop Java storage functions.
1. First, you must allow the database to support Java features.
Sp_configure "Enable Java", 1
1> sp_configure "Enable Java", 1

2> go

Parameter Name default memory used config Value

Run value unit type

----------------------------------------------------------------

-----------------------------------------

Enable Java 0 0 1

0 switch static

 

(1 row affected)

Configuration option changed. Since the option is static, Adaptive Server must

Be rebooted in order for the change to take effect.

Changing the value of 'Enable Java' to '1' increases the amount of memory ase

Uses by 6482 K.

(Return status = 0)

 

2. Restart the Sybase Database. Under nt, You can restart the service through the control panel> Management Tools>.

3 well, below, let's simply develop a Java program, and then look at how Sybase's T-SQL is called.

Java program

Package Sam;

 

Public class helloworld

{

Public String Hello ()

{

Return "helloworld ";

}

 

}

4. Compile and package the above Java program.

Javac SAM/*. Java

Jar CF0 Sam. Jar SAM/*. Class

5. Install the Java package to the database and use the instjava program provided by Sybase. This program will be installed along with Sybase installation. Note that the NT and Unix command names are different. In UNIX, It is installjava, and in Windows NT, It is instjava.

Instjava-F "E:/working directory/Sybase/Java/SAM. Jar"-New-J-s Sam-U sa-p-D northwind

6. Call the hello method in Sybase's transaction-SQL.

1> select (New sam. helloworld ()> Hello ()

2> go

 

--------------------------------------------------

Helloworld

We can see that Sybase uses similar Java syntax, so Java programmers should be easy to use.

7. Next, let's talk about the development and application of the second feature (Java object type.

Package Sam;

 

Public Class address implements java. Io. serializable

{

Private string varcity;

Private string varcountry;

Private string varzip;

Private string varhome;

 

Public Address (string mcity, string mcountry, string mzip, string mhome)

{

Varcity = mcity;

Varcountry = mcountry;

Varzip = mzip;

Varhome = mhome;

}

 

Public String City ()

{

Return varcity;

}

 

Public String country ()

{

Return varcountry;

}

 

Public String homeaddress ()

{

Return varhome;

}

 

Public String zip ()

{

Return varzip;

}

 

Public void modifyaddress (string mcity, string mcountry, string mzip, string mhome)

{

Varcity = mcity;

Varcountry = mcountry;

Varzip = mzip;

Varhome = mhome;

}

 

}

8. Compile, package, and install the SDK. Now, we can directly use this Java object type.

Create a table. The database field type is a Java class.

Note: It can be used as a Java class of the database field type. The java. Io. serializable interface must be implements.

1> Create Table EMPs (

2> empno int,

3> name varchar (30 ),

4> ADDR Sam. Address default New sam. Address

5> ('not known ','','','')

6>)

7> go

1> alter table EMPs add constraint pk_emps primary key (empno)

2> go

Operate and use fields of the Java type.

1> insert into EMPs (empno, name) values (1, 'Tom ')

2> go

(1 row affected)

1> insert into EMPs values (2, 'bob ',

2> New sam. Address ('shanghai', 'China', '000000', '2017, Nanjin road '))

3> go

(1 row affected)

 

1> begin

2> declare @ a Sam. Address

3> select @ A = ADDR from EMPs where empno = 2

4> select @ A> country (), @ A> City (), @ A> homeaddress (), @ A> zip ()

5> end

6> go

 

--------------------------------------------------

--------------------------------------------------

--------------------------------------------------

--------------------------------------------------

China

Shanghai

1169, Nanjin Road

200132

This article is from:Feng Libin's blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.