Import java.io.*;
import java.sql.*;
public class Db {
public static void Main (string[] args) {
db AC = new db ();
String blobname = "D:\\test1.jpg"; BLOB filename
String in = ' insert INTO ';
String in1 = "(Id,data) VALUES (´0012´,?)";
String tablename = "Ss";
String sqlstr = ""; SQL statement
sqlstr = in + tablename + in1;
threaduseextends thread = new Threaduseextends (BLOBNAME,SQLSTR);
Thread.insert ();
Thread.getcover ();
}
}
class Threaduseextends {
String filename1;//blob filename
String str;
//readfiles r1 = new Readfiles ();
//constructors should have (BLOB file name, CLOB filename, SQL statement)
public threaduseextends (String name1,string sqlstr)
{
filename1 = name1;
str = SQLSTR;
System.out.println ("I carry out of this");
}
public void Insert ()
{
try{
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:odbc:LW";
Connection con = drivermanager.getconnection (URL);
//string Testlong = R1. ReadFile (filename1);
file File=new file (filename1);
inputstream fin=new fileinputstream (file);
int filelength= (int) file.length ();
//byte[] ba = Testlong.getbytes ();
System.out.println ("str=" + str);
PreparedStatement stm = con.preparestatement (str);
Stm.setbinarystream (1,fin,filelength);
Stm.execute ();
Stm.close ();
Con.close ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
//This function is used to read the contents of a database from a database and save it in a file.
public void Getcover () {
InputStream In=null;
Connection cn = NULL;
PreparedStatement PST = NULL;
byte buf[]=new byte[50000];
DataOutputStream Output=null;
int size;
System.out.println ("have carry out of this");
Try
{
output=new DataOutputStream (New FileOutputStream ("d:\\test2.jpg"));
}
catch (IOException e)
{
System.err.println ("File not opened \ n" +e.tostring ());
system.exit (1);
}
try {
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
String url = "Jdbc:odbc:LW";
cn = drivermanager.getconnection (URL);
pst=cn.preparestatement ("Select data from Ss where ID =´0012´");
Pst.setstring (1,id);
ResultSet rs=pst.executequery ();
while (Rs.next ())
{
in=rs.getbinarystream ("data");
while ((Size=in.read (buf,0,50000))!=-1) {
Output.write (buf,0,size);
}
}
}
catch (SQLException Sqle) {
System.err.println ("Error in Coverservlet:getcover ()-" + Sqle);
Sqle.printstacktrace ();
}
catch (Exception e)
{
E.printstacktrace ();
}
finally {
try {
Pst.close ();
Cn.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
}//threaduseextends class