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:\\ project \ \ description file \ \ information. 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 ();
}
}
Images, large files for MySQL import and read storage to local