本文通过简单的JAVA程序来说明如何操作(读取、复制表、写文件)GBase 8t大对象数据类型。GBase 8t大对象数据类型包括:简单大对象类型(byte,text)和智能大对象(blob,clob)类型。
数据库环境变量
export INFORMIXSERVER=gbaseserver
export INFORMIXDIR=/home/gbase
export ONCONFIG=onconfig.gbaseserver
export INFORMIXSQLHOSTS=$INFORMIXDIR/etc/sqlhosts.gbaseserver
export PATH=$INFORMIXDIR/bin:/usr/bin:${PATH}:.
export GL_DATE="%m/%d/%iy"
export DATETIME="%iY-%m-%d %H:%M:%S"
export DB_LOCALE=zh_CN.GB18030-2000
export CLIENT_LOCALE=zh_CN.GB18030-2000
数据库实例创建sbspace空间。
创建测试表:
drop table demo1_1;
create table demo1_1(c1 int,col1 byte,col2 text,col3 blob, col4 clob);
create index idx_demo1_1 on demo1_1(c1);
drop table demo1_2;
create table demo1_2(c1 int,col1 byte,col2 text,col3 blob, col4 clob);
create index idx_demo1_2 on demo1_2(c1);
先运行 java demo1程序向表demo1_1插入记录。
http://www.informixchina.net/Article/38149
本示例程序读取表demo1_1记录,将记录复制到表demo1_2,并将读取的文件写入到文件中。
GBase 8t JDBC驱动提供标准的处理API接口。
JAVA环境变量
export JAVA_HOME=/home/informix/jdk1.8.0_111
export PATH=${PATH}:/home/informix/jdk1.8.0_111/bin
export CLASSPATH=.:/home/informix/jdk1.8.0_111/lib/tools.jar:/home/informix/jdk1.8.0_111/lib/dt.jar
export CLASSPATH=${CLASSPATH}:/home/gbase/jdbc/lib/ifxjdbc.jar
vi demo2.java
import java.util.*;
import java.sql.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
public class demo2{
private static String URL_STRING = "jdbc:informix-sqli://192.168.18.110:9088/demodb:INFORMIXSERVER=gbaseserver;CLIENT_LOCALE=zh_CN.GB18030-2000;DB_LOCALE=zh_CN.GB18030-2000;";
private static String user = "informix";
private static String password = "informix";
static Statement stmt;
static PreparedStatement pstmt = null;
static Connection conn = null;
public static void main(String args[])
{
try {
Class.forName("com.informix.jdbc.IfxDriver");
} catch (Exception e) {
System.out.println("FAILED: failed to load Informix JDBC driver.");
return;
}
try {
conn = DriverManager.getConnection(URL_STRING,user, password);
if (!conn.isClosed())
System.out.println("connect successfully");
conn.setAutoCommit(true);
} catch (SQLException e) {
System.out.println("FAILED: failed to connect: " + e.toString());
return;
}
System.out.println("doing ........");
System.out.println("Reading from demo1_1 and copying to demo1_2 ..\\n");
System.out.println("Writing to files ..\\n");
try {
int row = 0;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select c1,col1 ,col2 ,col3 , col4 from demo1_1");
pstmt = conn.prepareStatement("insert into demo1_2(c1,col1 ,col2 ,col3 , col4) values (?, ?, ?, ?, ?)");
while (rs.next()) {
row++;
int c1 = rs.getInt(1);
byte[] b = null;
b =rs.getBytes(2);
Blob col1 = rs.getBlob(2);
Clob col2 = rs.getClob(3);
Blob col3 = rs.getBlob(4);
Clob col4 = rs.getClob(5);
pstmt.setInt(1,c1);
pstmt.setBlob(2, col1);
pstmt.setClob(3, col2);
pstmt.setBlob(4, col3);
pstmt.setClob(5, col4);
pstmt.executeUpdate();
//begin to write to files
byteToFile(b,"test"+row+".jpg");
}
rs.close();
stmt.close();
pstmt.close();
} catch (SQLException e) {
System.out.println("FAILED: Select Failed ...\\n");
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
}
}
static void byteToFile(byte buf[] ,String filename){
try{
FileOutputStream fos = null;
BufferedOutputStream bos = null;
File file = new File(filename);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!
赞0
添加新评论0 条评论