总结:
- 处理CLOB数据(文本)
【例17.16】写入大文本数据(pstmt写入) 【例17.17】读取大文本字段(直接ResultSet) 【例17.18】使用Clob读取内容(使用Clob) 2.17.9.2 处理BLOB数据(视频,图片) 【例17.19]将图片写入到数据表中(pstmt存入) 【例17.20】读取内容,并将图片信息保存(直接ResultSet) 【例17.21】使用Blob读取内容(使用Blob)
17.9.1 处理CLOB数据
【例17.16】写入大文本数据(pstmt写入)
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
Class.forName(DBDRIVER);
String sql = "insert into userclob(name,note) values(?,?)";
File f = new File("d:"+File.separator+"mldn.txt");
InputStream input = new FileInputStream(f);
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "文本");
pstmt.setAsciiStream(2,input,(int)f.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
因为现在的内容是保存在文件中,所以使用FilelnputStream类将文本文件读取进来,然后直接通过PreparedStatement对象将其写入到对应的大文本字段中。
写入完成后,下面直接使用ResultSet 将其读取进来,因为写入的时候是按照输入流的方式写入的,所以此时也需要按照输入流的方式读取进来.
【例17.17】(直接ResultSet)读取大文本字段
InputStream input = rs.getAsciiStream(2);
Scanner scan = new Scanner(input);
scan.useDelimiter("\r\n");
while(scan.hasNext())
{
XXXX;
}
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 1;
Class.forName(DBDRIVER);
String sql = "select name,note from userclob where id=?";
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs =pstmt.executeQuery();
while(rs.next())
{
String name = rs.getString(1);
StringBuffer buf = new StringBuffer();
System.out.println("name:"+name);
InputStream input = rs.getAsciiStream(2);
Scanner scan = new Scanner(input);
scan.useDelimiter("\r\n");
while(scan.hasNext())
{
buf.append(scan.next()).append("\n");
}
System.out.println("内容"+buf);
input.close();
}
rs.close();
pstmt.close();
conn.close();
}
}
【例17.18】使用Clob读取内容
Clob c =rs.getClob(2);
String str = c.getSubString(1,(int)c.length());
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 1;
Class.forName(DBDRIVER);
String sql = "select name,note from userclob where id=?";
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs =pstmt.executeQuery();
while(rs.next())
{
String name = rs.getString(1);
Clob c =rs.getClob(2);
String str = c.getSubString(1,(int)c.length());
System.out.println("全部:"+str);
System.out.println("---------------------------------------------");
c.truncate(100);
System.out.println("部分:"+c.getSubString(1,(int)c.length()));
}
rs.close();
pstmt.close();
conn.close();
}
}
17.9.2 处理BLOB数据
【例17.19]将图片写入到数据表中(pstmt存入)
File f = new File("d:"+File.separator+"mldn.gif");
InputStream input = new FileInputStream(f);
pstmt.setString(1, name);
pstmt.setBinaryStream(2, input,(int)f.length());
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
String name ="gif图";
Class.forName(DBDRIVER);
String sql = "insert into userblob(name,photo) values(?,?)";
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
File f = new File("d:"+File.separator+"mldn.gif");
InputStream input = new FileInputStream(f);
pstmt.setString(1, name);
pstmt.setBinaryStream(2, input,(int)f.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
【例17.20】读取内容,并将图片信息保存
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 1;
Class.forName(DBDRIVER);
String sql = "select name,photo from userblob where id=?";
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
String name = rs.getString(1);
System.out.println("name:"+name);
InputStream input = rs.getBinaryStream(2);
File f = new File("d:"+File.separator+"loadmldn.gif");
OutputStream output = new FileOutputStream(f);
int temp = 0;
while((temp=input.read())!=-1)
{
output.write(temp);
}
input.close();
output.close();
}
rs.close();
pstmt.close();
conn.close();
}
}
【例17.21】使用Blob读取内容
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class demo
{
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/java_mysql";
public static final String DBUSER = "root";
public static final String DBPASS = "dyh20011022";
public static void main(String[] args) throws Exception
{
int id = 1;
Class.forName(DBDRIVER);
String sql = "select name,photo from userblob where id=?";
Connection conn =DriverManager.getConnection(DBURL, DBUSER, DBPASS);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
String name = rs.getString(1);
System.out.println("name:"+name);
Blob b = rs.getBlob(2);
File f = new File("d:"+File.separator+"loadmldn.gif");
OutputStream output = new FileOutputStream(f);
output.write(b.getBytes(1, (int)f.length()));
output.close();
}
rs.close();
pstmt.close();
conn.close();
}
}
|