处理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(); } }
时间: 2024-11-05 03:09:42