/**********************************************************************************
* Created on Nov, 2010 Copyright(c) http://vigilance.co.in All Rights Reserved.
**********************************************************************************
*/
package com.vigilance.java.sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.driver.OraclePreparedStatement;
/**
* This class contains the code logic for inserting the XML as XMLType in database.
*
* @author http://Vigilance.co.in
*
*/
public class InsertXMLType {
/**
* Constructor for this class
*
*/
public InsertXMLType() {
super();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
String JDBC_STRING = "jdbc:odbc:thin:@DBNAME:PORTNUMBER:SID";
String USER_NAME = "USER_NAME";
String PASSWD = "PASSWORD";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
OraclePreparedStatement pStmt = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(JDBC_STRING, USER_NAME, PASSWD);
stmt = conn.createStatement();
String insertQuery = "INSERT INTO TABLENAME (ID, NAME, STUDENTXML)"+
" VALUES(?,?,XMLTYPE(?))";
pStmt = (OraclePreparedStatement) conn.prepareStatement(insertQuery);
pStmt.setObject(1, "001");
pStmt.setObject(2, "VIGILANCE");
pStmt.setStringForClob(3, "studentXMLString");
pStmt.executeUpdate();
}catch(SQLException sqlEx){
if(pStmt!=null){
pStmt.close();
pStmt=null;
}
if(conn!=null){
conn.rollback();
conn.close();
conn = null;
}
throw new Exception(sqlEx);
}catch(Exception ex){
if(pStmt!=null){
pStmt.close();
pStmt=null;
}
if(conn!=null){
conn.rollback();
conn.close();
conn = null;
}
throw new Exception(ex.getMessage());
}finally{
if(pStmt!=null){
pStmt.close();
pStmt=null;
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn = null;
}
}
}
}
Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/7Oq2r7P_7rk/12733