I want to run an SQL file that has a lot of Insert Statments, and I am trying to run the file, So far i found this code but its not that good as it gives me an error that the table is not found, can you have a look at the code maybe you can find the problem ?
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 for any help you can give 🙂 i am using Xampp and the database is all ready created all i need is to populate it,
maybe you have to append new line
"\n"in this codebecause you append each line right after the before line