package sqlserver; import java.util.Date; import java.util.UUID; import java.text.SimpleDateFormat; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.sql.*; public class sqlserver { static String uuid=null; ////////////////////////////////////////////////////// public static void main(String[] args) { FileOutputStream bos = null; try { bos = new FileOutputStream("F:/output.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setOut(new PrintStream(bos)); System.out.println(insertData()); // getData(); } public static Connection MSSQLConnection(){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动 String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=filesdata"; // 连接服务器和数据库test String userName = "sa"; // 默认用户名 String userPwd = "sa"; // 密码 Connection dbConn = null; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(df.format(new Date())+" "+"数据库连接成功。"); // 如果连接成功 return dbConn; // 控制台输出Connection // Successful! } catch (Exception e) { e.printStackTrace(); } return null; } // read the file and insert into the video table; static String insertData() { int isFilesFound=0; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date2 = new Date(); Connection dbConn = null; dbConn=MSSQLConnection(); //UUID uuid = UUID.randomUUID(); String f_id = UUID.randomUUID().toString();//用来生成数据库的主键id非常不错.. uuid=f_id; File file = new File("F:/工作文件/PSD.rar"); System.out.println(df.format(new Date())+" "+"正在处理文件:"+file.getName()+" UUID:"+uuid); FileInputStream fis = null; try{ fis = new FileInputStream(file); isFilesFound=1; } catch(FileNotFoundException e){ System.out.println(df.format(new Date())+" "+"未找到文件。"); } if(isFilesFound==1){ try { System.out.println(df.format(new Date())+" "+"开始向数据库写入文件。"); PreparedStatement ps = dbConn.prepareStatement("Insert into files (f_id,f_name,f_content,f_date) values (?,?,?,?)"); ps.setString(1,f_id); ps.setString(2,file.getName()); ps.setBinaryStream(3,fis,(int)file.length()); ps.setDate(4,new java.sql.Date(date2.getTime())); System.out.println(df.format(new Date())+" "+"正在写入..。"); ps.executeUpdate(); ps.close(); try{ fis.close(); System.out.println(df.format(new Date())+" "+"文件已写入数据表且数据流已关闭。"); }catch(IOException e){ System.out.println(df.format(new Date())+" "+"数据流无法关闭。"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { dbConn.close(); System.out.println(df.format(new Date())+" "+"关闭数据库连接."); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return df.format(new Date())+" 插入数据完成。"; } // get the data file from database; static String getData(){ Connection dbcon = null; SimpleDateFormat df = new SimpleDateFormat("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=new File("d:/1.zip"); FileOutputStream fo = new FileOutputStream(file); BufferedOutputStream bo = new BufferedOutputStream(fo); byte[] buff = new byte[1024]; System.out.println(df.format(new Date())+" "+"正在从数据库写入本地..."); while((fi.read(buff))>0) { bo.write(buff); } System.out.println(df.format(new Date())+" "+"写入完毕."); bo.close(); fo.close(); fi.close(); System.out.println(df.format(new Date())+" "+"关闭读写流."); } }catch(Exception ex){ ex.printStackTrace(); }finally{ if (dbcon!=null){ try{ dbcon.close(); System.out.println(df.format(new Date())+" "+"关闭数据库连接."); }catch(Exception ex){ ex.printStackTrace(); } } dbcon = null; } return null; } ////////////////////////////////////////////////////// }
时间: 2024-10-05 14:30:26