Oracle的CLOB类型数据处理
发布时间:2020-12-12 15:46:51 所属栏目:百科 来源:网络整理
导读:Oracle菜鸟之grant授权: http://www.2cto.com/database/201408/322396.html oracle 查看用户所在的表空间 : http://www.jb51.cc/article/p-nmijckny-bdz.html oracle grant 详解: http://www.jb51.cc/article/p-rwegsjxt-hs.html Oracle中表列由VARCHAR2类型
Oracle菜鸟之grant授权:
http://www.2cto.com/database/201408/322396.html
oracle 查看用户所在的表空间 : http://www.52php.cn/article/p-nmijckny-bdz.html oracle grant 详解: http://www.52php.cn/article/p-rwegsjxt-hs.html Oracle中表列由VARCHAR2类型改成CLOB : http://www.52php.cn/article/p-bgtkrfwe-bct.html 创建表test 测试主类 public class testClob { public static void main(String[] args){ testOracleConnection(); } public static String ClobToString(CLOB clob) throws SQLException,IOException { String reString = ""; Reader is = clob.getCharacterStream();// 得到流 BufferedReader br = new BufferedReader(is); String s = br.readLine(); StringBuffer sb = new StringBuffer(); // 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING while (s != null) { sb.append(s); s = br.readLine(); } reString = sb.toString(); return reString; } public static void testOracleConnection() { Connection con = null;// 创建一个数据库连接 PreparedStatement preStm = null;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result = null;// 创建一个结果集对象 try { Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序 System.out.println("开始尝试连接数据库!"); //连接Oracle数据库提供了两中方式OCI方式和thin方式, //OCI方式是通过本地动态连接库和Oracle进行套接字通讯, //速度和安全性比较好,thin方式是通过远程访问Oracle // 127.0.0.1是本机地址,efss是精简版Oracle的默认数据库名 String url = "jdbc:oracle:thin:@192.168.126.128:1521:XE"; String user = "donald";// 用户名,系统默认的账户名 String password = "123456";// 你安装时选设置的密码 long startTime = System.currentTimeMillis(); con = DriverManager.getConnection(url,user,password);// 获取连接 con.setAutoCommit(false); preStm = con.prepareStatement("insert into test values(?,?)"); preStm.setInt(1,1); String name = "jamel"; Reader clobReader = new StringReader(name); // 将 text转成流形式 preStm.setCharacterStream(2,clobReader,name.length());// 替换sql语句中的? preStm.execute(); con.commit(); preStm = con.prepareStatement("select name from test where id = ?"); preStm.setInt(1,1); preStm.execute(); con.commit(); result = preStm.getResultSet(); while(result.next()){ CLOB clob = (oracle.sql.CLOB) result.getClob(1);// 获得CLOB字段str // 注释: 用 rs.getString("str")无法得到 数据 ,返回的 是 NULL; String content = ClobToString(clob); System.out.println("name:"+ content); } long endTime = System.currentTimeMillis(); System.out.println("============time:"+ (endTime-startTime)); System.out.println("============hashCode:"+ con.hashCode()); if(!con.isClosed()){ System.out.println("============连接成功!"); } } catch (Exception e) { System.out.println("=============连接失败:"+e.getMessage()); e.printStackTrace(); } finally { try { // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源 // 注意关闭的顺序,最后使用的最先关闭 if (result != null) result.close(); if (preStm != null) preStm.close(); if (con != null) con.close(); System.out.println("数据库连接已关闭!"); } catch (Exception e) { e.printStackTrace(); } }} } 控制台输出: 开始尝试连接数据库! name:jamel ============time:191 ============hashCode:28890871 ============连接成功! 数据库连接已关闭! 问题:ORA-00942: 表或视图不存在, 如果用navicat客户端创建表,会默认给表名加引号, 比如创建表test,则实际为表加引号"test" 或者用navicat创建表的时候,给表明添加引号: create table “test” 如果加引号不行,查看是不是用户表空间的问题: 查看用户表空间 select username,default_tablespace from user_users; 改变用户默认表空间: alter user donald default tablespace users; 总结: oracle 创建表的时候,最好不要用客户端,用sqlplus,跑创建表脚本。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |