返回首页

gbase数据、南大通用产品文档:GBase8s不需现有的 Java 类创建不透明类型

更新日期:2024年09月11日

在此示例中,使用客户端上的 Java™ 类 MyCircle 在数据库服务器中创建一个名
为 ACircle 的固定长度的不透明类型。ACircle 不透明类型使用数据库服务器提供的缺省支
持函数:
import java.sql.*;

public class MyCircle
{
String dbname = "test";
String url = null;
Connection conn = null;

public static void main (String args[])
{
new MyCircle(args);
}

MyCircle(String args[])
{
System.out.println("----------------");
System.out.println("- Start - Demo 3");
System.out.println("----------------");

// -----------
// Getting URL

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 208 -

// -----------
if (args.length == 0)
{
System.out.println("\n***ERROR: connection URL must be provided " +
"in order to run the demo!");
return;
}
url = args[0];

// --------------
// Loading driver
// --------------
try
{
System.out.print("Loading JDBC driver...");
Class.forName("com.gbasedbt.jdbc.Driver");
System.out.println("ok");
}
catch (java.lang.ClassNotFoundException e)
{
System.out.println("\n***ERROR: " + e.getMessage());
e.printStackTrace();
return;
}

// ------------------
// Getting connection
// ------------------
try
{
System.out.print("Getting connection...");
conn = DriverManager.getConnection(url);
System.out.println("ok");
}
catch (SQLException e)
{
System.out.println("URL = '" + url + "'");

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 209 -

System.out.println("\n***ERROR: " + e.getMessage());
e.printStackTrace();
return;
}
// -------------------
// Setup UDT meta data
// -------------------
UDTMetaData mdata = null;
try
{
mdata = new UDTMetaData();
System.out.print("Setting fields in mdata...");
mdata.setSQLName("acircle");
mdata.setLength(24);
mdata.setFieldCount(3);
mdata.setFieldName(1, "x");
mdata.setFieldName(2, "y");
mdata.setFieldName(3, "radius");
mdata.setFieldType(1, com.gbasedbt.lang.IfxTypes.IFX_TYPE_INT);
mdata.setFieldType(2, com.gbasedbt.lang.IfxTypes.IFX_TYPE_INT);
mdata.setFieldType(3, com.gbasedbt.lang.IfxTypes.IFX_TYPE_INT);
// set class name if don't want to use the default name
// .class
mdata.setClassName("ACircle");
mdata.setJarFileSQLName("ACircleJar");
mdata.keepJavaFile(true);
System.out.println("ok");
}
catch (SQLException e)
{
System.out.println("***ERROR: " + e.getMessage());
return;
}

// --------------------------------------------------------
// create java file for UDT and install UDT in the database
// --------------------------------------------------------

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 210 -

UDTManager udtmgr = null;
try
{
udtmgr = new UDTManager(conn);

System.out.println("Creating .class/.java files - " +
"createUDTClass()");
String classname = udtmgr.createUDTClass(mdata); // generated
//java file is kept
System.out.println(" classname = " + classname);

System.out.println("\nCreating .jar file - createJar()");
String jarfilename = udtmgr.createJar(mdata,
new String[]{"ACircle.class"}); // jarfilename is
// .jar
// ie. acircle.jar

System.out.println("\nsetJarTmpPath()");
udtmgr.setJarTmpPath("/tmp");

System.out.print("\ncreateUDT()...");
udtmgr.createUDT(mdata,
"/vobs/jdbc/demo/tools/udtudrmgr/" + jarfilename, "ACircle", 0);
System.out.println("ok");
}
catch (SQLException e)
{
System.out.println("\n***ERROR: " + e.getMessage());
return;
}
System.out.println();


// ---------------
// Now use the UDT
// ---------------
try

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 211 -

{
String s = "drop table tab";
System.out.print(s + "...");
Statement stmt = conn.createStatement();
int count = stmt.executeUpdate(s);
stmt.close();
System.out.println("ok");
}
catch ( SQLException e)
{
// -206 The specified table (%s) is not in the database.
if (e.getErrorCode() != -206)
{
System.out.println("\n***ERROR: " + e.getMessage());
return;
}
System.out.println("ok");
}

executeUpdate("create table tab (c acircle)");

// test DEFAULT Input function
executeUpdate("insert into tab values ('10 10 10')");

// test DEFAULT Output function
try
{
String s = "select c::lvarchar from tab";
System.out.println(s);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(s);
if (rs.next())
{
String c = rs.getString(1);
System.out.println(" acircle = '" + c + "'");
}
rs.close();

GBase 8s JDBC Driver 程序员指南
南大通用数据技术股份有限公司
- 212 -

stmt.close();
}
catch (SQLException e)
{
System.out.println("***ERROR: " + e.getMessage());
}
System.out.println();

executeUpdate("drop table tab");

// ------------------
// Closing connection
// ------------------
try
{
System.out.print("Closing connection...");
conn.close();
System.out.println("ok");
}
catch (SQLException e)
{
System.out.println("\n***ERROR: " + e.getMessage());
}

System.out.println("------------------");
System.out.println("- End - UDT Demo 3");
System.out.println("------------------");

}

case
24/case
4
HRO
football



sword GCIRowidToChar (
GCIRowid *rowidDesc,
GCIText *outbfp,
ub2 *outbflp,
GCIError *errhp
);

将 ROWID 数据类型转换为字符型数据。

参数
输入
/输出


rowidDesc
输入
GCIDescriptorAlloc()分配并由先前执行的SQL 语句填充的
ROWID 描述符
outbfp
输出
成功执行此调用后,指向存储字符表示的缓冲区的指针
outbflp
输入/输出
指向输出缓冲区长度的指针。在执行之前,缓冲区长度包含
outbfp 的大小。执行后,它包含转换的字节数。如果在转换过
程中有截断,outbfp 包含使转换成功所需的长度。还会返回一
个错误
errhp
输入
错误信息句柄,该接口调用失败时,错误信息会存在该句柄上

如果执行成功,则返回charset_id,否则返回GCI_ERROR。



GBase 8s GCI 接口使用指南

南大通用数据技术股份有限公司 124
8 附录