[英]Import sql file from Java
我正在嘗試使用Java將.sql文件上傳到本地主機服務器。 問題是我沒有加載.sql但它確實向我顯示了消息“備份正常。”。
實用程序:Windows 10、Netbeans 8.2、JDK 1.8、Xampp/phpmyadmin
類/方法:
public class Conexiones {
static String bbdd = "agenda";
static String login = "root";
static String password = "";
static String url = "jdbc:mysql://localhost:3306/";
static Connection c = null;
public static String getUsuario() {
return usuario;
}
public static String getPassword() {
return password;
}
public static String getBBDD() {
return bbdd;
}
public static Connection conexion_a_BBDD(String bbdd) {
Connection c = null;
try {
c = DriverManager.getConnection(url + bbdd, login, password);
return c;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getStackTrace(), "Admin", JOptionPane.WARNING_MESSAGE);
System.exit(0);
e.printStackTrace();
} catch (NullPointerException n) {
JOptionPane.showMessageDialog(null, "¡MySQL failed!", "Admin", JOptionPane.WARNING_MESSAGE);
n.printStackTrace();
}
return null;
}
}
JFileChooser:
private void button_seleccionarActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file= new JFileChooser();
FileNameExtensionFilter extension = new FileNameExtensionFilter("SQL", "sql");
file.setFileFilter(extension);
int se = file.showSaveDialog(null);
if(se == JFileChooser.APPROVE_OPTION){
String ruta = archivo.getSelectedFile().getPath();
textfield_ubicacion.setText(ruta);
}
}
ActionPerformed(按鈕)- Import.SQL:
private void button_cargarActionPerformed(java.awt.event.ActionEvent evt) {
String ruta = textfield_ubicacion.getText();
String backup = "";
if(ruta.trim().length()!=0){
try{
backup = "mysqldump -u"+Conexiones.getUsuario()+" -p"+Conexiones.getPassword()+" "+Conexiones.getBBDD()+" < "+ruta+"";
Runtime r = Runtime.getRuntime();
r.exec(backup);
JOptionPane.showMessageDialog(null, "Backup OK!");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
運行程序:
JFileChooser
-> OKSQL
文件並將其放入textfield_ubicacion
-> OKbutton
顯示“備份確定”。 但不加載它。 在phpmyadmin
BBDD 中,我看不到它。 (此處為錯誤)。在CMD命令控制台中,我使用以下命令:
mysql -u root -p < C:\Users\omaza\OneDrive\Desktop\bbddclientes.sql
在.sql文件中,有數據庫創建行:
CREATE DATABASE agenda;
USE agenda;
在 Java 中,符號<
無法識別。 所以我不得不嘗試另一種恢復方法:
private void button_cargarActionPerformed(java.awt.event.ActionEvent evt) {
String ruta = textfield_ubicacion.getText();
String[] restoreCmd = new String[]{"mysql ", "--user=" + Conexiones.getUsuario(), "--password=" + Conexiones.getPassword(), "-e", "source " + ruta};
try {
Process runtimeProcess = Runtime.getRuntime().exec(restoreCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup OK!");
} else {
System.out.println("Failed.");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
ex.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.