2015-03-31 15 views
1

我試圖將我的java服務在debian服務器上重新啓動的時間節省到MYSQL服務器中。 Java應用程序是一個dropwizard項目。我嘗試過從Java開始,但它需要創建對象,數據訪問對象,休眠配置文件和映射文件。這似乎是一個過度的,有沒有更簡單的方法來實現這一目標?什麼是堅持在MYSQL數據庫中重新啓動服務的時間的最佳方法

+0

也許你可以使用原始JDBC? – 2015-03-31 19:30:43

+0

感謝Stefaan,這似乎很合理。 – 2015-04-01 12:54:18

回答

1

是的,如果你想使用Java,你不需要使用休眠。所有你需要的是一個類似於如下:

公共類ServiceRestartServlet延伸的HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException, ServletException { 
    Connection conn = null; 
    try { 
    conn = DriverManager.getConnection(System.getProperty("jdbc.url"), System.getProperty("jdbc.user"), System.getProperty("jdbc.password")); 
    conn.setAutocommit(false); 
    PreparedStatement insertStatement = conn.prepareStatement("insert into serviceRestartLog (serviceName, restartTime) values (?, now())"); 
    insertStatement.setString(1, req.getParameter("serviceName")); 
    insertStatement.execute(); 
    conn.commit(); 
    } catch (SQLException e) { 
    System.err.println(new java.util.Date()+" Save failed -- "+e.getMessage()); 
    } finally { 
    try { 
     conn.close(); 
    } catch (Throwable e) { 
     System.err.println(new java.util.Date()+" connection failed to close -- "+e.getMessage()); 
    } 
    } 
    public void init(ServletContext ctx) { 
    Class.forName(System.getProperty("jdbc.drivers")).newInstance(); 
    } 
} 

只要啓動這個servlet在web.xml和創建表後,發放與應用名稱的請求,你應該是金色的。

0

使用「MySQL的」命令行

mysql -u USERNAME -p PASSWORD -e 'update WHATEVER set DATE_RESTARTED=NOW();' 

搜索幫助,如果你不想在命令行上傳遞密碼。

+0

這會在我的Java程序?這與原始JDBC有何不同? – 2015-03-31 19:42:49

相關問題