技术开发 频道

我与Db2 9新特性的零距离体验



    插入操作
    执行插入操作的类为insertxml.java,该程序读取一个XML文件,该XML文件包括对客户的描述信息,如:客户的地址信息、电话、传真等。并将该XML文件的内容插入到CUSTOMER表中的XML类型的字段。
insertxml.java其代码如下:
package xml; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; public class Test2 { /** * @param args */ /** private members **/ private static Connection conn; static Properties db2ConnProps = new Properties(); static Properties fileinputProps = new Properties(); public static void main(String[] args) throws FileNotFoundException, IOException, SQLException { String db, userName, passwd, host, port; host=port=db=userName=passwd=null; /* load the contents of properties file in case of missing arguments*/ db2ConnProps.load(new FileInputStream ("F:\\eclipse\\WorkPlace\\DB2XML\\src\\db2Conn.properties")); db=db2ConnProps.getProperty("databaseName"); userName=db2ConnProps.getProperty("userName"); passwd=db2ConnProps.getProperty("password"); host=db2ConnProps.getProperty("hostName"); port=db2ConnProps.getProperty("portNumber"); /** connect to the database **/ conn=db2Conn.get(db,userName,passwd,host,port); int id = 99;//由于id是主键,所以该id不能相同,如连续插入多次,可以修改id的值或者在数据库中将CUSTOMER表中的id字段设置为自动增长。 String fn = "F:\\eclipse\\WorkPlace\\DB2XML\\src\\Client8877.xml"; String sqls = "insert into customer ( cid, info) values (?, ?)"; File file = new File(fn); try { PreparedStatement insertStmt = conn.prepareStatement(sqls); conn.setAutoCommit(true); insertStmt.setInt(1, id); insertStmt.setBinaryStream(2, new FileInputStream(file), (int) file .length()); if (insertStmt.executeUpdate() == 1) { System.out.println("insertBinStream:: No record inserted."); } insertStmt.executeUpdate(); } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { conn.close(); } } }
0
相关文章