PackageSQL Server;Importjava.util.Date;ImportJava.util.UUID;ImportJava.text.SimpleDateFormat;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.PrintStream;ImportJava.sql.*; Public classSQL Server {StaticString uuid=NULL;////////////////////////////////////////////////////// Public Static voidMain (string[] args) {fileoutputstream Bos=NULL;Try{BOS=NewFileOutputStream ("F:/output.txt");} Catch(FileNotFoundException e) {//TODO auto-generated Catch blocke.printstacktrace ();} System.setout (NewPrintStream (BOS)); System.out.println (InsertData ());//GetData (); } Public StaticConnection mssqlconnection () {String drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//loading the JDBC driverString Dburl = "jdbc:sqlserver://localhost:1433; Databasename=filesdata ";//Connect server and database testString userName = "sa";//Default User nameString userpwd = "sa";//PasswordConnection dbconn =NULL; SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");Try{class.forname (drivername);d bconn=drivermanager.getconnection (Dburl, UserName, userpwd); System.out.println (Df.format (NewDate ()) + "+" database connection succeeded. ");//If the connection is successfulreturnDbconn;//Console Output Connection//successful!}Catch(Exception e) {e.printstacktrace ();}return NULL;}//read the file and insert into the video table;StaticString InsertData () {intIsfilesfound=0; SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate date2=NewDate (); Connection Dbconn=NULL;d Bconn=mssqlconnection ();//uuid uuid = Uuid.randomuuid ();String f_id = Uuid.randomuuid (). toString ();//The primary key ID used to generate the database is very good .Uuid=f_id; File File=NewFile ("f:/working file/psd.rar"); System.out.println (Df.format (NewDate ()) + "" + "Processing file:" +file.getname () + "UUID:" +uuid); FileInputStream FIS=NULL;Try{FIS=Newfileinputstream (file); Isfilesfound=1;} Catch(FileNotFoundException e) {System.out.println (Df.format (NewDate ()) + "" + "File not found. ");}if(isfilesfound==1){Try{System.out.println (Df.format (NewDate ()) + "+" begins writing files to the database. "); PreparedStatement PS= Dbconn.preparestatement ("Insert into files (f_id,f_name,f_content,f_date) VALUES (?,?,?,?)");p s.setstring (1, f_id); Ps.setstring (2, File.getname ());p S.setbinarystream (3,fis, (int) file.length ());p s.setdate (4,Newjava.sql.Date (Date2.gettime ())); System.out.println (Df.format (NewDate ()) + "+" is writing ... "); Ps.executeupdate (); Ps.close (); Try{fis.close (); System.out.println (Df.format (NewDate ()) + "+" file is written to the data table and the data flow is closed. "); }Catch(IOException e) {System.out.println (Df.format (NewDate ()) + "+" data stream cannot be closed. "); } } Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}}Try{dbconn.close (); System.out.println (Df.format (NewDate ()) + "+" to close the database connection. ");} Catch(SQLException e) {//TODO auto-generated Catch blocke.printstacktrace ();}returnDf.format (NewDate ()) + "Insert data complete. "; }//get the data file from database;StaticString GetData () {Connection Dbcon=NULL; SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");Try{Dbcon=mssqlconnection ();//OutputStream out = new FileOutputStream ("D:/1.exe");Statement st =dbcon.createstatement (); ResultSet RS= St.executequery ("select * from files where f_id = ' F7453B56-92F2-4AA9-8781-6CA85AB3F0CE '"); while(Rs.next ()) {Java.io.InputStream fi=rs.getbinarystream ("F_content"); File File=NewFile ("D:/1.zip"); FileOutputStream fo=Newfileoutputstream (file); Bufferedoutputstream Bo=NewBufferedoutputstream (FO);byte[] Buff =New byte[1024]; System.out.println (Df.format (NewDate ()) + "" + "writing locally from Database ..."); while(Fi.read (Buff)) >0) {bo.write (buff);} System.out.println (Df.format (NewDate ()) + "" + "write complete."); Bo.close (); Fo.close (); Fi.close (); System.out.println (Df.format (NewDate ()) + "+" closes the read-write stream. ");}}Catch(Exception ex) {ex.printstacktrace ();}finally{if(dbcon!=NULL){Try{dbcon.close (); System.out.println (Df.format (NewDate ()) + "+" to close the database connection. ");}Catch(Exception ex) {ex.printstacktrace ();}} Dbcon=NULL;}return NULL;}//////////////////////////////////////////////////////}
Java inserts file data into SQL Server