我想运行一个包含大量Insert Statments的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);
}
}
对于你可以提供的任何帮助thx我正在使用Xampp并且数据库已经准备就绪了我只需要填充它,
答案 0 :(得分:2)
也许您必须在此代码中添加新行"\n"
while ((s = br.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
因为您在前一行
之后追加每一行