面向对象存取在Sybase ASE15.0中的解决方案

本文为iihero原创,如若转载,请务必注明出处。

<!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- [if !mso]><style> <!-- st1/:*{behavior:url(#ieooui) } --><!-- [endif]--><!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:2109228193; mso-list-type:hybrid; mso-list-template-ids:-260291210 104487470 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-number-format:japanese-counting; mso-level-text:%1、; mso-level-tab-stop:42.0pt; mso-level-number-position:left; margin-left:42.0pt; text-indent:-21.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --><!-- [if gte mso 10]><style> <!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]--> </style>

数据库发展到今天,RDBMS(关系型数据库管理系统)ODBMS(对象数据库管理系统)的界限已经变得模糊了,取而代之的是ORDBMS(对象关系型数据库管理系统),对此,OracleDB2早年前都给出了自己的解决方案,即采用类似以下的抽象语法:

create typePoint is object

(

x int,

y int,

......

)

SybaseASE数据库,发展到现在15.0.3,在语法上,似乎并没有对这种抽象语法的直接支持。它采取的是另一种解决方案,即可以由用户直接定义一个Java类,让这个Java类直接成为一种抽象类型。那就是Sybase ASE数据库的Java服务选件,它是一种功能非常强大的组件。

比如,我们要提供一个解决方案,直接支持二维点Point类型的存取。怎么实现呢?

<!-- [if !supportLists]-->
  • 环境:
<!-- [endif]-->

确定你的ASE数据库安装并拥有JavaOptions选项,如何得知呢?从SybaseCentral,连接到ASE服务器,右键à属性à版本à详细信息,可以得到如下信息:ASE_JAVA,有相应的版本和有效期信息,示例如下图,它支持Java,有效期到2020120

面向对象存取在Sybase ASE15.0中的解决方案

<!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- [if !mso]><style> <!-- st1/:*{behavior:url(#ieooui) } --><!-- [endif]--><!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --><!-- [if gte mso 10]><style> <!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]--> </style>

除此以外,还得让服务器启动时也支持Java

D:/ASE150>isql-Usa-Psybase1

1>sp_configure "enable java", 1

2> go

(1 row affected)

配置选项改变.因为选项是静态的,AdaptiveServer必须重启,以使改动生效.

如果将 'enable java' 更改为 '1'ASE 使用的内存量就能增加 580K

(return status =0)

然后重启Sybase ASE数据库,即可生效,开始完全支持Java

  • <!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:2109228193; mso-list-type:hybrid; mso-list-template-ids:-260291210 104487470 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-number-format:japanese-counting; mso-level-text:%1、; mso-level-tab-stop:42.0pt; mso-level-number-position:left; margin-left:42.0pt; text-indent:-21.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --><!-- [if gte mso 10]><style> <!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]--><span style="font-family: 宋体;">实现</span> </style>
<!-- [if !supportLists]--><!-- [endif]-->

首先,实现Point类。ASE支持Java对象类型,有一个条件,它必须实现了序列化接口。同时,编译为class文件时,编译的目标版本最好是1.1(为了保持兼容)

Point类的一个简单实现如下:

public class Point implements java.io.Serializable

{

privatestatic final long serialVersionUID = -2550063311147977493L;

publicdouble _x;

publicdouble _y;

publicPoint()

{

_x = 0;

_y = 0;

}

publicPoint(double x, double y)

{

_x = x;

_y = y;

}

publicObject clone()

{

returnnew Point(_x, _y);

}

publicString toString()

{

return"(" + _x + ", " + _y + ")";

}

publicstatic double distance(Point a, Point b)

{

return(double)Math.sqrt((a._x - b._x)*(a._x - b._x)

+ (a._y - b._y)*(a._y - b._y));

}

publicstatic void main(String[] args)

{

Point a= new Point(1.0, 1.0);

Point b= new Point(5.0, 4.0);

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println(Point.distance(a, b));

}

}

设定好jdk路径和classpath之后, 编译打包:

javac -g -target1.1 *.java

jar -cvf0Point.jar *.class

  • <!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- [if gte mso 10]><style> <!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]--><p class="MsoNormal" style="margin-left: 42pt; text-indent: -21pt;"><!-- [if !supportLists]--><span lang="EN-US"><span>一、</span></span><!-- [endif]--><span style="font-family: 宋体;">装载</span><span lang="EN-US">java</span><span style="font-family: 宋体;">包到目标数据库</span></p> <p class="MsoNormal" style="margin-left: 21pt;"><span style="font-family: 宋体;">如下图所示:进入具体的数据库</span><span lang="EN-US">iihero</span><span style="font-family: Wingdings;" lang="EN-US"><span>à</span></span><span lang="EN-US">Java</span><span style="font-family: 宋体;">对象</span><span style="font-family: Wingdings;" lang="EN-US"><span>à</span></span><span lang="EN-US">Jar</span><span style="font-family: 宋体;">文件,右键</span><span style="font-family: Wingdings;" lang="EN-US"><span>à</span></span><span style="font-family: 宋体;">新建,即可把刚才打好的</span><span lang="EN-US">java</span><span style="font-family: 宋体;">包</span><span lang="EN-US">Point.jar</span><span style="font-family: 宋体;">装载到数据库</span><span lang="EN-US">iihero</span><span style="font-family: 宋体;">当中。</span></p> </style>

面向对象存取在Sybase ASE15.0中的解决方案

<!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- [if !mso]><style> <!-- st1/:*{behavior:url(#ieooui) } --><!-- [endif]--><!-- [if gte mso 10]><style> <!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --><!-- [endif]--> </style>

或者直接使用命令行:

D:/ASE150>instjava-D iihero -S SEANLAPTOP

-fE:/MyDocument/MYBOOKS/ASE/code/JavaSQL/Point.jar-update -j"Point" -Usa-P

其中-D指定目标数据库,-S指定服务名,-f指定已经编译好的java包,-j指定该包在数据库中的包名,-U指定连接数据库的用户名,-P指定密码。

<!-- [if !supportLists]-->一、<!-- [endif]-->面向对象应用的实际效果

D:/ASE150>isql-Uspring -Pspring1

1> use iihero

2> go

创建含对象类型Point列的表:

1> createtable testpoint(id int primary key, p Point)

2> go

使用对象特性插入数据并查询:

1> insertinto testpoint values (1, new Point(1.0, 1.0))

2> insertinto testpoint values (2, new Point(5.0, 4.0))

3> insertinto testpoint values (3, new Point(6.0, 13.0))

4> go

(1 row affected)

(1 row affected)

(1 row affected)

1> select *from testpoint

2> go

idp

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

1 (1.0, 1.0)

2 (5.0, 4.0)

3 (6.0, 13.0)

(3 rowsaffected)

直接取属性值:

1> select id,p>>_x, p>>_y from testpoint

2> go

id

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

1 1.000000 1.000000

2 5.000000 4.000000

3 6.000000 13.000000

(3 rowsaffected)

查询两点间的距离:

1> selecta.id, b.id, Point.distance(a.p, b.p) from testpoint a, testpoint b whe

re a.id <b.id

2> go

idid

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

1 2 5.000000

1 3 13.000000

2 3 9.055385

(3 rowsaffected)

以距离作为条件查询点(查询两点间距离小于6.0的点对)

1> selecta.id, b.id from testpoint a, testpoint b where Point.distance(a.p, b.p

) <6.0 anda.id > b.id

2> go

idid

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

21

(1 row affected)

<!-- [if !supportLists]-->二、<!-- [endif]-->总结

在到ASE数据库服务器内部,它对Java类作了如下映射:

<!-- [if !supportLists]-->l<!-- [endif]-->select出对象列的值,就是Java对象的toString()值。

<!-- [if !supportLists]-->l<!-- [endif]-->静态方法可以直接在select语句里当作函数进行调用,如上例中的Point.distance()方法

<!-- [if !supportLists]-->l<!-- [endif]-->直接可以取类的成员值,如果那个成员是公有成员,并且有相应的SQL类型映射,如上例中的p>>_x,p>>_y,使用”>>”操作符来提取。

按照上述原理,用户完全使用内置的Java包实现非常复杂的逻辑,然后在SQL语句中直接加以利用。尤其是那种接近于面向对象数据库的应用。

总之,使用内置Java支持对对象类型的存取,是一种很直观易用的方法,也便于用户进行扩充。从这个角度来看,Sybase ASE也可以算是支持面向对象的数据存取了。


<script type="text/javascript"><!-- google_ad_client = "ca-pub-7104628658411459"; /* wide1 */ google_ad_slot = "8564482570"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

你可能感兴趣的:(Sybase)