String 转Clob mysql springMVC
1个回答
展开全部
既然你是从数据库取出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();
}
}
}
如果从数据库里面取出的是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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询