我是JFrame Jtable的新手。一直在尝试执行插入查询,但是即时通讯收到“此处不允许列”错误。
private void AddActionPerformed(java.awt.event.ActionEvent evt) {
try{
theQuery("INSERT INTO CONFERENCE(CONFERENCEID,NAME,YEAR,STARTDATE,ENDDATE,COUNTRY,CITY,VENUE,EMAIL)"
+"values('"+cid.getText()+"','"+cname.getText()+"','"+cyear.getYear()+"','"
+csd.getDate()+"','"+ced.getDate()+"','"+country.getText()+"','"+city.getText()
+"','"+venue.getText()+"',"+email.getText()+")");
} catch(Exception ex){}
}
public void theQuery(String query){
Connection con = null; Statement st = null;
try{ con = DriverManager.getConnection("jdbc:oracle:thin:@example:123", "123", "student");
st = con.createStatement();
st.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Query Executed"); }
catch(Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } }
最佳答案
我认为您应该首先使您的连接系统像这样。
public void theQuery()
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/yourBDD";
String sqlLogin = "login";
String sqlPassword = "password";
Connection conn = DriverManager.getConnection(url, sqlLogin, sqlPassword);
String query = " insert into CONFERENCE(CONFERENCEID, NAME, YEAR, COUNTRY, CITY, VENUE, EMAIL) values (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStmt = cnx.prepareStatement(query);
preparedStmt.setString(1, cid.getText());
preparedStmt.setString(2, cname.getText());
preparedStmt.setString(3, cyear.getYear());
preparedStmt.setString(4, csd.getDate());
preparedStmt.setString(5, ced.getDate());
preparedStmt.setString(6, country.getText());
preparedStmt.setString(7, city.getText();
...
preparedStmt.execute();
conn.close();
}