@Action(value = "beifen") public void beifen() throws Exception, IOException, SQLException { SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//設置日期格式 取月份 日期 String riqi=df2.format(new Date()); File dir = new File(this.imgFileStore.getRootFile().getPath()+ "/sqlbeifen/"+riqi); if (!dir.exists()) { dir.mkdirs(); } String weburl=this.imgFileStore.getRootFile().getPath()+ "/sqlbeifen/"+riqi; String fileFileName="123.bak"; String name = new Date().getTime()+ fileFileName.substring(fileFileName.lastIndexOf("."));// 生成隨機文件名 File file = new File(weburl); String path = file.getPath() + "\\" + name;// name文件名 String bakSQL = "backup database hoosee_ydoa to disk=? with init";// SQL語句 PreparedStatement bak = DataBaseUtil.getConnection().prepareStatement(bakSQL); bak.setString(1, path);// path必須是絕對路徑 bak.execute(); // 備份數據庫 bak.close(); String data=imgFileStoreHttp.getRootFilePath()+ "/sqlbeifen/"+riqi+"/"+name; printWriter(data); } /* * 輸出頁面 */ public void printWriter(String msgs) throws Exception, IOException { // 指定輸出內容類型和編碼 ServletActionContext.getResponse().setContentType( "text/html;charset=utf-8"); // 獲取輸出流,而後使用 PrintWriter out = ServletActionContext.getResponse().getWriter(); // 直接進行文本操做 out.print(msgs); out.flush(); out.close(); }
package com.hoosee.actions.Family; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DataBaseUtil { /** * 獲取數據庫鏈接 * @return Connection 對象 */ public static Connection getConnection() { Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=hoosee_ydoa"; String username = "sa"; String password = "123"; conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void closeConn(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }