Java Manipulating SQL Server 2000

Source: Internet
Author: User
Tags int size odbc thread

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.