0

我在网上找到了这段代码,想知道我将如何让它与我的在线数据库一起使用,而不是在本地主机上使用

public boolean restoreDB(String dbUserName, String dbPassword, String source) {

        String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source "+source};

        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup restored successfully");
                return true;
            } else {
                System.out.println("Could not restore the backup");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

例如

   try{
            Class.forName("java.sql.Driver");
            Connection conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/in1010gp33?user=login&password=password");
            Statement st=conn.createStatement();
            String query="UPDATE jobs SET JobStatus = " + cmbStatus.getSelectedItem() + " where JobID='" + txtJobID.getText() + "';";
            ResultSet rs=st.executeQuery(query);

    rs.close();
    st.close();
    conn.close();
        }
4

0 回答 0