Friday, 27 September 2013

RESTORE MYSQL DATABASE WITHOUT USING MYSQL COMMAND PROMPT




<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="icon" href="./images/fav.ico"/>
        <link rel="stylesheet" type="text/css" href="tabcontent.css" />
        <title>Indian WildLife</title>
        <script type="text/javascript" src="tabcontent.js"></script>
    </head>
    <body>
       
    <center>
        <div style="border-radius: 25px; width:800px; height:650px;background:#f0D0D0 "><br/>

<h3>Restore Database</h3>
<%

String database = null;
String username = "root";//Mysql User Name
String password = "root";//Mysql Password
String urlmysql = "jdbc:mysql://localhost:9999";//CHANGE PORT NUMBER


String dbtype = null;
Connection connection = null;
Statement statement = null;
ResultSet rset = null;
int result = -1;

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch(ClassNotFoundException e) {
    out.println("Class not found: "+ e.getMessage());
    return;
}
if (request.getParameter("dbtype") != null) { dbtype = request.getParameter("dbtype"); };


try {

    connection = DriverManager.getConnection(urlmysql, username, password);
    statement = connection.createStatement();

    out.println("<b>List of MySQL databases accessible by user " + username + ":</b><br/>");
    rset = statement.executeQuery("SHOW DATABASES");
    while (rset.next())   {
        out.println(rset.getString(1) + "<br/>");
    }
    rset.close();
    statement.close();
    connection.close();
    out.println("<hr>");
  if (request.getParameter("database") != null) {
        database = (String)request.getParameter("database");
        if (request.getParameter("Restore") != null &&
            request.getParameter("Restore").equals("Restore")) {

            String[] executeCmd = new String[]{"mysql", "--user=" + username, "--password=" + password, "-e", "source "+ database + ".sql"};
         
            Process runtimeProcess;
            try {
                runtimeProcess = Runtime.getRuntime().exec(executeCmd);
                int processComplete = runtimeProcess.waitFor();
                if (processComplete == 0) {
                    out.println("Backup restored successfully");
                } else {
                    out.println("Could not restore the backup");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

  }

%>
<form action="restore.jsp" method="post"><table>
<tr><td align="left">Database name to restore: <input type="text" name="database" size="20"></td></tr>
<tr><td><input type="radio" name="dbtype" value="mysql" checked="checked">MySQL<br>
</tr>
<tr><td align="left">
<input type="submit" name="Restore" value="Restore">
<input type="reset" name="Reset" value="Reset"></td></tr>
</table></form>
   
<%
} catch (SQLException e) {
    out.println(e.getMessage());
} finally {
    try {
        if(connection != null) connection.close();
    } catch(SQLException e) {}
}
%>

        </div>
    </center>
     
    </body>
</html>

No comments:

Post a Comment