import java.sql.*;
import java.io.*;public class SQLSERVERConnection 
{
   public static void main(String args[]) throws Exception
   {
      String driver      = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
  String url         ="jdbc:microsoft:sqlserver://localhost:1433;databasename=mydb";
      String user        = "sa";
      String password    = "song197937";
  String insertSQL   = "Insert INTO imageTable1 VALUES(?, ?)" ;
      String createTable = "CREATE TABLE imageTable1" +"(NAME VARCHAR(32),"+ "PHOTO image)";
  String dropTable   ="DROP TABLE imageTable1";
  String selectSQL   = "SELECT photo FROM imageTable1 WHERE NAME = ?"  ; 
  Connection con;
  Statement stmt;
  PreparedStatement pstmt;

      try
  {  
 Class.forName(driver);
 con = DriverManager.getConnection(url,user,password);
 System.out.println("Connection OK");
 stmt=con.createStatement();
         if(!con.isClosed())   
 System.out.println("´ò¿ªÊý¾Ý¿âÁ¬½Ó³É¹¦OK!"); 
 try
 {
 stmt.executeUpdate(dropTable);
 System.out.println("Original TABLE imageTable1 is deleted!");

 }catch(Exception e)
 {
             System.out.println(e);
 System.out.println("No existing table to delete!");
 }
 
 
     stmt = con.createStatement();
     stmt.executeUpdate(createTable);  pstmt = con.prepareStatement(insertSQL) ;
 File file = new File("H:\\Program\\SQL SERVER\\pair03.jpg") ;
 FileInputStream fis = new FileInputStream(file);
 pstmt.setString(1, "Zhang Hongbin");
 pstmt.setBinaryStream(2, fis, (int)file.length());
 pstmt.executeUpdate();
 pstmt.close();  stmt.close();
 fis.close();  
 con.close();
 System.out.println("Image Insert Successful") ;
  }catch(Exception e)
  {
         e.printStackTrace();
         System.out.println("´ò¿ªÊý¾Ý¿âÁ¬½Óʧ°Ü!");
  }  
  
   }
}