package conn;
import java.io.*;
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.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
import com.sun.corba.se.spi.orbutil.fsm.Input;
public class Conn {
public Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "gjl");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void setlob(){
Connection conn = null;
PreparedStatement pre = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = getConn();
String sql = "insert into t_lob value (?,?,?)";
pre = conn.prepareStatement(sql);
File txt = new File("E:\\项目\\说明文件\\信息.txt");
File img = new File("G:\\1.jpg");
InputStream inTxt = new FileInputStream(txt);
InputStream inImg = new FileInputStream(img);
pre.setString(1, "1");
pre.setAsciiStream(2, inTxt, (int)txt.length());
pre.setBinaryStream(3, inImg, (int)img.length());
pre.executeUpdate();
conn.close();
pre.close();
System.out.println("read-ok");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getlob(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from t_lob");
while(rs.next()){
Clob clob = rs.getClob(2);
Blob blob = rs.getBlob(3);
InputStream txt = clob.getAsciiStream();
InputStream img = blob.getBinaryStream();
FileOutputStream OutTxt= new FileOutputStream(new File("F:\\T.txt"));
FileOutputStream OutImg= new FileOutputStream(new File("F:\\I.jpg"));
int c1 = -1;
int c2 = -1;
while(( c1 = txt.read())!= -1)
OutTxt.write(c1);
OutTxt.flush();
OutTxt.close();
while(( c2 = img.read())!= -1)
OutImg.write(c2);
OutImg.flush();
OutImg.close();
}
System.out.println("write-ok");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[]args){
Conn conn = new Conn();
// conn.getlob();
// conn.setlob();
}
}