我在网上找到了这个代码,想知道如何使用我的在线数据库,而不是一个本地主机
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();
}