public long getNextValue(String seqfunctionname, String sequence, Connection con, String dbname) throws SQLException { long curValue = 0; PreparedDBUtil dbutil = new PreparedDBUtil(); try { // if() String sql = "select " + sequence + ".nextval from dual"; dbutil.preparedSelect(dbname, sql); dbutil.executePrepared(con); if (dbutil.size() <= 0) { // System.out.println("select " + this.generator + ".nextval from dual"); throw new SQLException( "[select " + sequence + ".nextval from dual] from [" + dbname + "] failed:retrun records is 0."); } curValue = dbutil.getInt(0, 0); return curValue; } catch (SQLException e) { throw e; } }
/** 针对oracle Clob字段的插入操作 */ @Test public void testBigClobWrite() { PreparedDBUtil dbUtil = new PreparedDBUtil(); TransactionManager tm = new TransactionManager(); try { // 启动事务 tm.begin(); // 先插入一条记录,blob字段初始化为empty_lob dbUtil.preparedInsert("insert into test(id,clobname) values(?,?)"); String id = DBUtil.getNextStringPrimaryKey("test"); dbUtil.setString(1, id); dbUtil.setClob(2, CLOB.empty_lob()); // 先设置空的blob字段 dbUtil.executePrepared(); // 查找刚才的插入的记录,修改blob字段的值为一个文件 dbUtil = new PreparedDBUtil(); dbUtil.preparedSelect("select clobname from test where id = ?"); dbUtil.setString(1, id); dbUtil.executePrepared(); CLOB clob = (CLOB) dbUtil.getClob(0, "clobname"); if (clob != null) { DBUtil.updateCLOB(clob, new java.io.File("d:\\route.txt")); } tm.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); try { tm.rollback(); } catch (RollbackException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally { tm = null; dbUtil = null; } }
private boolean exist(Connection con, long curValue) throws SQLException { String select = "select count(1) from " + this.tableName + " where " + this.primaryKeyName + "=?"; PreparedDBUtil dbUtil = new PreparedDBUtil(); try { // String update = "update tableinfo set table_id_value=" + this.curValue +" where // table_name='"+ tableName.toLowerCase() + "' and table_id_value <" + this.curValue ; dbUtil.preparedSelect(this.dbname, select); if (this.metaType.equals("int") || this.metaType.equals("java.lang.Integer") || this.metaType.equals("java.lang.integer") || this.metaType.equalsIgnoreCase("integer")) { dbUtil.setInt(1, (int) curValue); } else if (this.metaType.equals("java.lang.Long") || this.metaType.equals("java.lang.long") || this.metaType.equalsIgnoreCase("long")) { dbUtil.setLong(1, curValue); } else if (this.metaType.equals("java.lang.String") || this.metaType.equalsIgnoreCase("string")) { dbUtil.setString(1, this.prefix + curValue + ""); } else { dbUtil.setString(1, this.prefix + curValue + ""); } dbUtil.executePrepared(con); if (dbUtil.getInt(0, 0) > 0) return true; } catch (SQLException e1) { e1.printStackTrace(); throw e1; } catch (Exception e1) { e1.printStackTrace(); throw new SQLException(e1.getMessage()); } finally { dbUtil.resetPrepare(); } return false; }
/** 大字段的读取 */ @Test public void testBlobRead() { PreparedDBUtil dbUtil = new PreparedDBUtil(); try { // 查询大字段内容并且将大字段存放到文件中 dbUtil.preparedSelect("select id,blobname from test"); dbUtil.executePrepared(); for (int i = 0; i < dbUtil.size(); i++) { dbUtil.getFile(i, "blobname", new java.io.File("e:/dominspector.rar")); // 将blob字段的值转换为文件 // Blob blob = dbUtil.getBlob(i, "blobname");//获取blob字段的值到blob变量中。 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } }
/** clob字段的读取 */ @Test public void testClobRead() { PreparedDBUtil dbUtil = new PreparedDBUtil(); try { // 查询大字段内容并且将大字段存放到文件中 dbUtil.preparedSelect("select id,clobname from test"); dbUtil.executePrepared(); for (int i = 0; i < dbUtil.size(); i++) { dbUtil.getFile(i, "clobname", new java.io.File("d:/route" + i + ".txt")); // 读取clob字段到文件中 // String clobvalue = dbUtil.getString(i, "clobname");//获取clob字段到字符串变量中 // Clob clob = dbUtil.getClob(i, "clobname");//获取clob字段值到clob类型变量中 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { dbUtil = null; } }