package com.haitaiinc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import oracle.sql.CLOB; public class JDBCtest { /** * 更新大字段 * @param xml */ public void testOracle(String xml){ Connection conn = null;// 创建一个数据库连接 PreparedStatement ps = null;// 创建预编译语句对象,一般都是用这个而不用Statement try{ Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序 System.out.println("开始尝试连接数据库!"); String url = "jdbc:oracle:" + "thin:@ip:port:htdb"; String user = "user";// 用户名,系统默认的账户名 String password = "pwd";// 你安装时选设置的密码 conn = DriverManager.getConnection(url, user, password);// 获取连接 System.out.println("连接成功!"); ps = conn.prepareStatement("update inpcase.HOSPITAL_RECORD set XML_CONT=xmltype(?) where ipid=?"); ps.setObject(1, getCLOB(conn,xml)); ps.setString(2, "889686"); ps.executeUpdate(); conn.commit(); }catch (Exception e){ e.printStackTrace(); }finally{ try{ if (ps != null) ps.close(); if (conn != null) conn.close(); System.out.println("数据库连接已关闭!"); }catch (Exception e){ e.printStackTrace(); } } } public void testDb2(String xml){ Connection conn = null;// 创建一个数据库连接 PreparedStatement ps = null;// 创建预编译语句对象,一般都是用这个而不用Statement try{ Class.forName("com.ibm.db2.jcc.DB2Driver");// 加载Oracle驱动程序 System.out.println("开始尝试连接数据库!"); String url = "jdbc:db2://ip:port/htdb"; String user = "user";// 用户名,系统默认的账户名 String password = "pwd";// 你安装时选设置的密码 conn = DriverManager.getConnection(url, user, password);// 获取连接 System.out.println("连接成功!"); ps = conn.prepareStatement("update inpcase.HOSPITAL_RECORD set XML_CONT=XMLPARSE (DOCUMENT cast(? as clob(10M)) PRESERVE WHITESPACE) where ipid=?"); ps.setObject(1, getCLOB(conn,xml)); ps.setString(2, "889686"); ps.executeUpdate(); conn.commit(); }catch (Exception e){ e.printStackTrace(); }finally{ try{ if (ps != null) ps.close(); if (conn != null) conn.close(); System.out.println("数据库连接已关闭!"); }catch (Exception e){ e.printStackTrace(); } } } private static CLOB getCLOB(Connection conn, String xmlData) throws SQLException { CLOB tempClob = CLOB.createTemporary(conn, true, 10); tempClob.putChars(1L, xmlData.toCharArray()); return tempClob; } }