返回首页

gbase数据、南大通用产品文档:GBase8a程序验证

更新日期:2024年09月11日

编写如下servlet,然后把项目部署到GlassFish 上。
package com.gbase.glassfish;


GBase UP 产品手册 6 应用开发指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 1065
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class TestServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* The doGet method of the servlet.

*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

Connection conn = null;
ResultSet rs = null;
Statement st = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("gbase2");
conn = ds.getConnection();
st = conn.createStatement();
rs = st.executeQuery("select id,name,age,department from emp");
while(rs.next()){

GBase UP 产品手册 6 应用开发指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 1066
System.out.println("员工id:"+rs.getInt(1)+" 员工姓名:"+rs.getString(2)+"
员工年龄:"+rs.getInt(3)+" 员工部门:"+rs.getString(4));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(rs != null){
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
if(st != null){
try {
st.close();
st = null;
} catch (SQLException e) {
e.printStackTrace();
}
if(conn != null){
try {







conn.close();







conn = null;
} catch (SQLException e) {







e.printStackTrace();
}
}
}
}
}
}

/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client

GBase UP 产品手册 6 应用开发指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 1067
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

this.doGet(request, response);
}
}
web.xml 文件如下:


xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet
com.gbase.glassfish.TestServlet



TestServlet
/TestServlet


index.jsp


部署成功后,启动glassfish 服务器,访问如下url:
http://localhost:8088/TestGBase/TestServlet
该测试用例会打印出数据库为test 表为emp 中的内容。

GBase UP 产品手册 6 应用开发指南
文档版本04(2021-04-21) 南大通用数据技术股份有限公司 1068

incvasc() 函数将 INTERVAL 值的符合 ANSI SQL 标准的字符串转换为 interval 值。


语法
mint incvasc(inbuf, invvalue)
char *inbuf;
intrvl_t *invvalue;
inbuf
指向包含 ANSI 标准 INTERVAL 字符串的缓冲区的指针。
invvalue
指向初始化了的 interval 变量的指针。

用法

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 824 -

您必须以您想要此变量拥有的限定符来初始化 invvalue 中的 interval 变量。

inbuf 中的字符串可有开头的和结尾的空格。然而,从第一个有效数字到最后一个,
inbuf 仅可包含字符,它们是适合于 interval 变量的限定符字段的数字和定界符。

如果该字符串为空字符串,则 incvasc() 函数将 invvalue 中的值设置为空。如果该字
符串是可接受的,
则该函数设置 interval 变量中的值,
并返回零。
否则,
该函数将 interval
值中的值设置为空。

返回代码
0
转换成功。
-1260
不可能在指定的类型之间转换。
-1261
datetime 或 interval 的第一个字段中的数字太多。
-1262
datetimeinterval 中的非数值字符。
-1263
datetime 或 interval 值中的字段出界或不正确。
-1264
在 datetime 或 interval 值的末尾有额外的字符。
-1265
在 datetime 或 interval 操作上发生溢出。
-1266
datetime 或 interval 值与该操作不兼容。
-1267
datetime 计算的结果出界。
-1268
参数包含无效的 datetime 或 interval 限定符。

示例

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 825 -

demo 目录在文件 incvasc.ec 中包含此样例程序。
/*
* incvasc.ec *

The following program converts ASCII strings into interval (intvl_t) structure. It also
illustrates error conditions involving invalid qualifiers for interval values.
*/

#include

EXEC SQL include datetime;

main()
{
mint x;

EXEC SQL BEGIN DECLARE SECTION;
interval day to second in1;
EXEC SQL END DECLARE SECTION;

printf("INCVASC Sample ESQL Program running.\n\n");

printf("Interval string #1 = 20 3:10:35\n");
if(x = incvasc("20 3:10:35", &in1))
printf("Result = failed with conversion error:%d\n",x);
else
printf("Result = successful conversion\n");

/*
* Note that the following literal string has a 26 in the hours field
*/
printf("\nInterval string #2 = 20 26:10:35\n");
if(x = incvasc("20 26:10:35", &in1))

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 826 -

printf("Result = failed with conversion error:%d\n",x);
else
printf("Result = successful conversion\n");

/*
* Try to convert using an invalid qualifier (YEAR to SECOND)
*/
printf("\nInterval string #3 = 2007-02-11 3:10:35\n");
in1.in_qual = TU_IENCODE(4, TU_YEAR, TU_SECOND);
if(x = incvasc("2007-02-11 3:10:35", &in1))
printf("Result = failed with conversion error:%d\n",x);
else
printf("Result = successful conversion\n");

printf("\nINCVASC Sample Program over.\n\n");
}
输出
INCVASC Sample ESQL Program running.
Interval string #1 = 20 3:10:35
Result = successful conversion

Interval string #2 = 20 26:10:35
Result = failed with conversion error:-1263

Interval string #3 = 2007-02-11 3:10:35
Result = failed with conversion error:-1268

缺省时,当PL/SQL 函数执行过程中发生错误时退出函数执行,并且周围的事务也会回
滚。可以用一个带有EXCEPTION 子句的BEGIN 块捕获错误并且从中恢复。其语法是正常
的BEGIN 块语法的一个扩展:
[<