返回首页

gbase数据、南大通用产品文档:GBase8a指定数据文件中TIMESTAMP 列的格式

更新日期:2024年09月11日

建表语句:
create table ttimestamp(a timestamp default '2014-01-01 12:25:36' ,b int);
数据文件:
2014-01-01 12:01:01|1
|2
|3
|4
|5
2014-01-02 12:03:03|6
加载过程:
gbase> LOAD DATA INFILE 'http://10.10.120.226/timestamp.txt' INTO TABLE test.ttime
stamp DATA_FORMAT 3 FIELDS TERMINATED BY '|' TIMESTAMP FORMAT '%Y-%m-%d %H:%i:%s';
Query OK, 6 rows affected
Task 2 finished, Loaded 6 records, Skipped 0 records
查询入库数据:
gbase> select * from ttimestamp order by b
+---------------------+------+
| a | b |
+---------------------+------+
| 2014-01-01 12:01:01 | 1 |
| 2014-01-01 12:25:36 | 2 |
| 2014-01-01 12:25:36 | 3 |
| 2014-01-01 12:25:36 | 4 |
| 2014-01-01 12:25:36 | 5 |
| 2014-01-02 12:03:03 | 6 |
+---------------------+------+

字符串由零个或多个字符组成的序列,用单引号括起来,可以参照GBase8s语法。全
部除空字符串('')外的字符串文本具有数据类型CHAR。
'Hello, world!'
'XYZ Corporation'
'10-NOV-91'
'He said "Life is like licking honey from a thorn."'
'$1,000,000'
PL/SQL在字符串变量中区分大小写。例如,'baker'和'Baker'不同的。
要表示字符串中的撇号,可以写两个单引号,即不等于写双引号:
'I''m a string, you''re a string.'

在此示例中,使用客户端上的 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("------------------");

}