標籤:
處理CLOB大資料對象
import java.sql.SQLException ;import java.sql.DriverManager ;import java.sql.ResultSet ;import java.sql.Connection ;import java.sql.PreparedStatement ;import java.io.File ;import java.io.FileInputStream;import java.io.InputStream ;class Tester{ public static final String DBURL = "jdbc:mysql://localhost:3306/student" ;public static final String DBUSER = "root" ;public static final String DBPASS = "include" ;public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;public static void main(String args[]) throws Exception{ Connection con = null; PreparedStatement ps = null; String name="王呆萌" ; String sql = "insert into text(name,note) values(?,?)" ; Class.forName(DBDRIVER) ; con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);//這句每次都會手殘寫錯 ps=con.prepareStatement(sql) ; File f =new File("d:"+File.separator+"in.txt") ; InputStream in =new FileInputStream(f); ps.setString(1,name) ; ps.setAsciiStream(2,in,(int)f.length()); ps.executeUpdate(); in.close(); ps.close(); con.close(); System.out.println("操作成功!"); }}
讀取CLOB大資料對象
import java.sql.Connection ;import java.sql.SQLException ;import java.sql.DriverManager ;import java.sql.PreparedStatement;import java.sql.ResultSet ;import java.io.InputStream ;import java.io.File ;import java.io.FileInputStream ;import java.util.Scanner ;class Tester{ public static final String DBURL = "jdbc:mysql://localhost:3306/student" ;public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;public static final String DBUSER = "root" ;public static final String DBPASS = "include" ;public static void main(String args[]) throws Exception{ Connection con = null;PreparedStatement ps = null ;ResultSet rs = null;int id=1;String sql ="select name,note from text where id=?";con = DriverManager.getConnection(DBURL,DBUSER,DBPASS);ps=con.prepareStatement(sql) ;ps.setInt(1,id);rs=ps.executeQuery();if(rs.next()){ String name = rs.getString(1);StringBuffer sb = new StringBuffer() ;System.out.println("名字 :"+name);InputStream in = rs.getAsciiStream(2) ;Scanner scan = new Scanner(in) ; //點睛之筆scan.useDelimiter("\r\n");while(scan.hasNext()){ sb.append(scan.next()).append("\n") ;}System.out.println(sb);in.close();}rs.close();ps.close();con.close();}}
CLOB大資料對象