之后
我想运行一个SQL文件,有很多插入语句,我正试图运行文件,到目前为止,我发现这段代码,但它不是那么好,因为它给了我一个错误,表没有找到,你能看看代码也许你能找到问题?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
URL = jTextField1.getText();
username = jTextField3.getText();
password = jTextField2.getText();
String s = new String();
StringBuilder sb = new StringBuilder();
Connection conn = (Connection) DriverManager.getConnection(URL, username, password);
Class.forName("com.mysql.jdbc.Driver");
try {
Statement st = conn.createStatement();
String query = "DELETE FROM player;";
st.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Delete Compleated");
}catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
////////////////////////////////////////////////////////////////////////////////
try {
FileReader fr = new FileReader(new File("player.sql"));
// be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
sb.append(s);
}
br.close();
// here is our splitter ! We use ";" as a delimiter for each request
// then we are sure to have well formed statements
String[] inst = sb.toString().split(";");
Statement st = conn.createStatement();
for (int i = 0; i < inst.length; i++) {
// we ensure that there is no spaces before or after the request string
// in order to not execute empty statements
if (!inst[i].trim().equals("")) {
st.executeUpdate(inst[i]);
System.out.println(">>" + inst[i]);
}
}
JOptionPane.showMessageDialog(this,"Loaded Compleat");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
try {
Statement st = conn.createStatement();
String query = "Select * from player;";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
int d1 = rs.getInt(1);
String d2 = rs.getString("PlayerName");
String d3 = rs.getString("PlayerSurname");
String d4 = rs.getString("PlayingPosition");
double d5 = rs.getDouble("PlayerRating");
model.addRow(new Object[]{
d1, d2, d3, d4, d5
});
}
rs.close();
st.close();
conn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(Football.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Football.class.getName()).log(Level.SEVERE, null,ex);
}
}
谢谢你的任何帮助你可以给:)我正在使用Xampp和数据库都准备好了,所有我需要的是填充它,
也许你需要在这段代码中添加新的"n"
行
while ((s = br.readLine()) != null) {
sb.append(s);
sb.append("n");
}
因为你把每一行都附加在前一行