1. 如何把clob类型数据变成string类型的数据
给你段参考代码,读取clob数据
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestClobOut {
public static void main(String args[]){
String data;
Reader inStream=null;
//获得数据库连接
Connection con = ConnectionFactory.getConnection();//ConnectionFactory类是另外定义的,不必纠结
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
if (rs.next())
{
java.sql.Clob clob = rs.getClob("CLOBATTR");
inStream = clob.getCharacterStream();
char[] c = new char[(int) clob.length()];
inStream.read(c);
//data是读出并需要返回的数据,类型是String
data = new String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();
}
}
2. oracle数据库CLOB类型怎么转换为String
给你段参考代码,读取clob数据
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestClobOut {
public static void main(String args[]){
String data;
Reader inStream=null;
//获得数据库连接
Connection con = ConnectionFactory.getConnection();//ConnectionFactory类是另外定义的,不必纠结
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
if (rs.next())
{
java.sql.Clob clob = rs.getClob("CLOBATTR");
inStream = clob.getCharacterStream();
char[] c = new char[(int) clob.length()];
inStream.read(c);
//data是读出并需要返回的数据,类型是String
data = new String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();
}
}
3. oracle clob类型不能再select查询的时候就给转换成String类型吗
clob不可以直接insert。。。。。。。使用java.sql.Clob/Blob导入
4. java将string插入到类型为clob的ORACLE库中
1:首先:写个连接数据库的类,里面有返回mysq, oracle连接的方法
public Connection getConn(String flag){
Connection con=null;
try
{
if(flag.equals("1"))
{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con = DriverManager.getConnection(“jdbc:oracle:thin:@IP:1521:数据库名字”,"name","password");
}
if(flag.equals("2"))
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/数据库名?user=用户名&password=密码&useUnicode=true&characterEncoding=GBK");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
2:执行插入操作
public void setData() {
conn = new Conn();
try {
String sqlfrom = "select p.id,p.content from table p order by p.id ";
String sqlinsert = "insert into table values(?,?)";
con = conn.getConn("2");
stmt = con.createStatement(); //从mysql取出大字段
rs = stmt.executeQuery(sqlfrom);
con = conn.getConn("1");
PreparedStatement pstmt = con.prepareStatement(sqlinsert); //向oracle中插入大字段
int i = 0;
while (rs.next()) {
pstmt.setInt(1, rs.getInt(1));
pstmt.setClob(2, oracle.sql.CLOB.empty_lob());
pstmt.executeUpdate(); //插入时将大字段设为空
this.updateOne(con,rs.getInt(1),rs.getString(2)); // 这里调用然后更新这个大字段
}
rs.close(); //关闭相关连接
pstmt.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滚出现异常!");
e1.printStackTrace();
}
}
}
3:该方法实现对应大字段记录的更新
public void updateOne(Connection con,int id, String content) {
String str = "select t.content from table t where t.id=" + id+ " for update";
try {
// 注意:存取操作开始前,必须用setAutoCommit(false)取消自动提交,否则Oracle将抛出“读取违反顺序”的错误。
con.setAutoCommit(false);
stmt = con.createStatement();
ResultSet rs_clob = stmt.executeQuery(str);
while ( rs_clob .next()) {
/* 取出clob数据*/
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs_clob .getClob(1);
/* 向clob中写入数据*/
clob.putString(1, content);
}
stmt.close();
con.commit();
con.setAutoCommit(true);
con.close();
} catch (Exception e) {
e.printStackTrace();
try
{
con.rollback();
} catch (Exception e1) {
System.out.println("回滚出现异常!");
e1.printStackTrace();
}
}
}
现在就完成了一行记录的更新。
4:读clob字段以String 的形式返回(当然也可以将读到的内容写入文件,大家改一下就可以了)
/**
* 读clob字段
* @param con
* @param id
* @return
*/
public String readClob(Connection con,int id)
{
String content="";
try
{
con.setAutoCommit(false);
stmt=con.createStatement();
ResultSet rs_clob=stmt.executeQuery("select t.content from table t where t.id="+id);
oracle.sql.CLOB contents=null;
while (rs_clob.next())
{ // 取出CLOB对象
contents= (oracle.sql.CLOB) rs_clob.getClob(1);
}
BufferedReader a = new BufferedReader(contents.getCharacterStream()); //以字符流的方式读入BufferedReader
String str = "";
while ((str = a.readLine()) != null) {
content = content.concat(str); //最后以String的形式得到
}
con.commit();
/*
BufferedWriter out = new BufferedWriter(new FileWriter("e:/test.txt"));
out.write(content); //写入文件
out.close(); */
con.setAutoCommit(true);
con.close();
}catch(Exception e)
{
System.out.println("出现异常");
e.printStackTrace();
try
{
con.rollback();
}
catch (Exception e1)
{
System.out.println("回滚出现异常!");
e1.printStackTrace();
}
}
return content;
}
5. String 转化为 java.io.Clob 对象
要先将原来的数据的clob字段置为空,然后更新,用这个:
conn = DAOFactory.getInstance().getConnection();
// 事务处理
conn.setAutoCommit(false);
// 先把clob字段content置空,content为clob字段
String sql = "update " + table + " set content=empty_clob()";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
// 从数据库中取出插入的clob字段
sql = "select content from " + table + " for update";
rs = stmt.executeQuery(sql);
if(rs.next()) {
// 给clob数据重新赋值,然后更新到数据库中
Clob clob = rs.getClob(1);
clob.setString(1, content);
sql = "update " + table + " set content=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setClob(1, clob);
pstmt.setString(2, id);
pstmt.executeUpdate();
}
// 事务提交
conn.commit();
conn.setAutoCommit(true);
补充:
Clob有点特殊,它是一个数据库相关的接口,没法实例化, 你可以通过SerialClob来实例化,这个我没用过。
api上这样写到:
public SerialClob(char[] ch)
throws SerialException,
SQLException按照给定 char 数组的序列化形式构造一个 SerialClob 对象。
新的 SerialClob 对象使用 char 数组中的数据进行初始化,因此允许未连接 RowSet 对象无需接触数据源即可建立序列化的 Clob 对象。
参数:
ch - 表示要序列化的 Clob 对象的 char 数组
你试试吧
6. JAVA读取Oracle数据库Clob字段转换成String问题
如果使用oracle的话,直接将clob字段读取为string;也就是下面这一行:
clob
=
(oracle.sql.clob)
rs.getobject(1);
可以直接写成
string
str
=
rs.getstring(1);
7. 数据库中字段的类型是blob,页面传入的类型是string,怎么转换才能插入进去
存入:
byte[] buffer = System.Text.Encoding.Default.GetBytes(你的数据);
OracleCommand cmd = new OracleCommand(你的sql语句, 你的连接);
cmd.Parameters.Add("你的字段", OracleType.Blob);
cmd.Parameters[0].Value = buffer;
cmd.ExecuteNonQuery();
读取:
OracleLob clob = OracleLob.Null;
OracleCommand command = new OracleCommand(你的select语句, 你的连接);
OracleDataReader reader = command.ExecuteReader();
while(reader.Read())
{
clob = reader.GetOracleLob(你的列名);
byte[] buffer = (byte[])reader["你的字段"];
string format = System.Text.Encoding.Default.GetString(buffer);//转换成string
//以后想怎么处理format就看你了,你也可以把buffer转换成你想要的类型
}
去网上下相关的包吧,在JSP里面写的也一样的
8. 怎么把一个String转成CLOB
既然你是从数据库取出CLOB字段,那么不用resultset那是用什么取出的。
如果从数据库里面取出的是CLOB字段,为什么会变成String类型呢。
String转CLOB,下面是个例子
public class TestDB {
public static void main(String[] args) {
try {
/** Loading the driver*/
Class.forName("com.oracle.jdbc.Driver");
/** Getting Connection*/
Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test");
PreparedStatement pstmt = con.prepareStatement("insert into Emp(id,name,description)values(?,?,?)");
pstmt.setInt(1,5);
pstmt.setString(2,"Das");
// Create a big CLOB value...AND inserting as a CLOB
StringBuffer sb = new StringBuffer(400000);
sb.append("This is the Example of CLOB ..");
String clobValue = sb.toString();
pstmt.setString(3, clobValue);
int i= pstmt.executeUpdate();
System.out.println("Done Inserted");
pstmt.close();
con.close();
// Retrive CLOB values
Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test");
PreparedStatement pstmt = con.prepareStatement("select * from Emp where id=5");
ResultSet rs = pstmt.executeQuery();
Reader instream = null;
int chunkSize;
if(rs.next()){
String name = rs.getString("name");
java.sql.Clob clob = result.getClob("description")
StringBuffer sb1 = new StringBuffer();
chunkSize = ((oracle.sql.CLOB)clob).getChunkSize();
instream = clob.getCharacterStream();
BufferedReader in = new BufferedReader(instream);
String line = null;
while ((line = in.readLine()) != null) {
sb1.append(line);
}
if(in != null){
in.close();
}
String clobdata = sb1.toString(); // this is the clob data converted into string
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
9. java String类型和blob类型转换
如果你的数据真的是 String ,那按理就是用 Clob 嘛。
Blob 主要用于二进制内容,比如图片,附件。
如果保持数据库表结构不变的话,用 blob 也行,但你需要在读取和写入两头明确地指定相同的字符集,否则读取这个还原过程会得到不到期望的结果。只要我们用支持这种字符的字符集理论上来说,只要编码和解码的过程使用相同的字符集就不会失真,如果字符集本身不支持这个字符(比如你拿一个只有康熙字典中才有的古汉字用 GB2312 字符集去处理就会失真,而用 UTF8 就可能正常,因为只要这个字符真的能被输入法录入到电脑中基本上它就已经表示有办法能处理它)。
PreparedStatement 中有 setBlob (JDBC 4.0) 或 setBinaryStream (早期) 方法。但你需要测试你使用的驱动程序是什么版本的与数据库是否匹配。
10. Spring+Mybatis类型转换的问题,oracle数据库中有一个clob类型,怎样在查询以后转换为String类型
首先你的思路就错的,不可能转换成string
把大对象读进byte[]
public byte[] function(Connection connection,所需参数) throws EMPException{
PreparedStatement ps = null;
ResultSet rs = null;
byte[] data = null;
try {
.....省略
while (rs.next()) {
oracle.sql.CLOB clob= (oracle.sql.CLOB) rs.getClob("大对象的字段名");
InputStream inStream =clob.getBinaryStream();
long nLen = clob.length();
int nSize = (int) nLen;
data = new byte[nSize];
inStream.read(data);
inStream.close();
connection.commit();
}
} catch (SQLException e) {
EMPLog.log(this.getClass().getName(), EMPLog.INFO, 0, e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
} catch (SQLException e) {
EMPLog.log(this.getClass().getName(), EMPLog.INFO, 0, e
.toString());
}
}
return data;
}
2.直接在页面上将对象读到页面上
<form action="">
<%
response.setContentType("image/jpg");
response.setHeader("Content-Transfer-Encoding","base64");
ServletOutputStream toClient = response.getOutputStream();
out.clear();
out = pageContext.pushBody();
ByteArrayInputStream in = new ByteArrayInputStream(data);
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
toClient.write(buf, 0, len);
}
toClient.flush();
toClient.close();
%>
</form>