帮人整db2的xml类型字段导入的sample
import java.io.*;
import java.sql.*;
public class sample {
public static void main(String[] args) {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url = "jdbc:db2:HH";
//DB2用的是系统用户名和密码
Connection con = DriverManager.getConnection (url,"yoursysusername","password");
//创建表
//Statement stmn=con.createStatement();
//stmn.execute("CREATE TABLE hh(id INT NOT NULL PRIMARY KEY,hh_info XML)");
//insert 示范
String sql = "INSERT INTO hh VALUES(?, ?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, 5000);
File binFile = new File("e:\\hh\\hh1.xml");
InputStream inBin = new FileInputStream(binFile);
stmt.setBinaryStream(2, inBin, (int) binFile.length());
stmt.execute();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
db2在我单位的机器上跑得慢如蜗牛 读取的慢慢再说
补充
这里是带读写的
import java.io.*;
import java.sql.*;
public class db2sample {
/**
* @param args
*/
public static void main(String[] args) {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url = "jdbc:db2:HH";
//DB2用的是系统用户名和密码
Connection con = DriverManager.getConnection(url, "user","password");
//创建表
//Statement stmn=con.createStatement();
//stmn.execute("CREATE TABLE hh(id INT NOT NULL PRIMARY KEY,hh_info XML)");
//insert/update 示范
//String sql = "INSERT INTO hh VALUES(?, ?)";
String sql = "update hh set hh_info = ? where id = ? ";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(2, 5000);
File binFile = new File("e:\\hh\\hh1.xml");
InputStream inBin = new FileInputStream(binFile);
stmt.setBinaryStream(1, inBin, (int) binFile.length());
stmt.execute();
String sql2 = "SELECT id, hh_info from hh where id = ?";
PreparedStatement stmt2 = con.prepareStatement(sql2);
stmt2.setInt(1, 5000);
ResultSet resultSet = stmt2.executeQuery();
if (resultSet.next()) {
String xml = resultSet.getString("hh_info");
// also possible
//InputStream inputStream = resultSet.getBinaryStream("hh_info");
// also possible
//Reader reader = resultSet.getCharacterStream("hh_info");
System.out.println(xml);
} else {
System.out.println("no record");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
posted on 2008-04-22 12:57
zarra 阅读(449)
评论(2) 编辑 收藏