我正在做一个简单的公共汽车票管理项目。为此,我想在数据库中添加系统用户数据。但我的问题是,这表明netbeans中的代码在下面的行中有一个错误。
int executedQuery=statement.executeUpdate();
如果代码编译时出错,则显示
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.sql.Statement.executeUpdate
at airlinessoft1301d.User_New.add_jButton1ActionPerformed(User_New.java:367)
at airlinessoft1301d.User_New.access$100(User_New.java:21)
at airlinessoft1301d.User_New$2.actionPerformed(User_New.java:255)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编译器错误输出:
warning: [options] bootstrap class path not set in conjunction with -source 1.7
D:\Farhad_BusTicketManagement\src\airlinessoft1301d\User_New.java:369: error: no suitable method found for executeUpdate(no arguments)
int executedQuery=statement.executeUpdate();
method Statement.executeUpdate(String) is not applicable
(actual and formal argument lists differ in length)
method Statement.executeUpdate(String,int) is not applicable
(actual and formal argument lists differ in length)
method Statement.executeUpdate(String,int[]) is not applicable
(actual and formal argument lists differ in length)
method Statement.executeUpdate(String,String[]) is not applicable
(actual and formal argument lists differ in length)
Note: D:\Farhad_BusTicketManagement\src\airlinessoft1301d\User_New.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
1 warning
D:\Farhad_BusTicketManagement\nbproject\build-impl.xml:946: The following error occurred while executing this line:
D:\Farhad_BusTicketManagement\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
整个源代码如下所示。虽然我已经做了一些其他的数据库插入工作,以同样的方式,他们的工作。请任何人告诉我哪里犯了错误。
我正在使用netbeans gui builder创建用户界面。
NetBeans版本是8.1
JDK 1.8欧60。
以前我使用的是netbeans 7.3。在互联网上看到,netbean 7有一个bug,它可能会显示这样的错误。然后我安装了netbeans 8.1。但问题是一样的。
计算机生成的代码尚未给出。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Cybertrons
*/
public class User_New extends javax.swing.JFrame {
public User_New()
{
initComponents();
}
//start of loadingDriver(). This method will be called where driver loading is needed.
public void loadingDriver()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex)
{
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
}
//End of loadingDriver(). This method will be called where driver loading is needed
@SuppressWarnings("unchecked")//computer generated code not given.
private void name_jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void add_jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Just for checking that add button is working.
JOptionPane.showMessageDialog(rootPane,"Clicked...!");
// Start of Varriable Decaliration.
String userID;
String name;
String birthDate;
String phone;
String email;
String gender;
String userName;
String password;
String position;
// End of Varriable Decaliration.
// Strat of Reading vlaue form text fields.
userID =userId_jTextField1.getText();
name =name_jTextField1.getText();
birthDate =birth_Date_jTextField2.getText();
phone =phone_jTextField3.getText();
email =email_jTextField4.getText();
gender =Gender_jComboBox1.getSelectedItem().toString();
userName =userName_jTextField5.getText();
password =pass_word_jPasswordField1.getText().toString();
position =position_jTextField7.getText();
// End of Reading vlaue form text fields.
Connection connection = null;
Statement statement = null;
//The driver will loaded here by calling the method loadingDriver(), That has defined above.
loadingDriver();
// start of inserting data into database.
try //inseritng try block.
{
// connecting to databse.
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/busticket", "root", "");
// Insering to database.
statement = (PreparedStatement)
connection.prepareStatement("insert into newuser (userID,name,birthDate,phone,email,gender,userName,password,position) values"
+ "(NULL ,'"+userID+"','"+name+"','"+birthDate+"','"+phone+"','"+email+"','"+gender+"','"+userName+"',"
+ " '"+password+"','"+position+"')");
//To ensure that the query has executed successfully.
int executedQuery=statement.executeUpdate();
// Start of making the text fields Empty
if(executedQuery==1)
{
JOptionPane.showMessageDialog(rootPane, "Inserted Success fully....!");
//start of setting emplty string at the text fields.
userId_jTextField1.setText("");
name_jTextField1.setText("");
birth_Date_jTextField2.setText("");
phone_jTextField3.setText("");
email_jTextField4.setText("");
userName_jTextField5.setText("");
pass_word_jPasswordField1.setText("");
position_jTextField7.setText("");
//end of setting emplty string at the text fields.
}
// End of making the text fields Empty
// Start of Closing Databae Connection.
if(statement!=null)
{
statement.close();
}
if(connection!=null)
{
connection.close();
}
// end of Closing Databae Connection.
}
catch(SQLException ex)
{
Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
}
// end of inserting data into database.
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(User_New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(User_New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(User_New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(User_New.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new User_New().setVisible(true);
}
});
}
最佳答案
aStatement
没有execute()
方法。另一方面,是的。您的PreparedStatement
变量实际上是一个statement
(在运行时),但是您将它存储在一个PreparedStatement
变量中,所以代码不会编译。只要把它放在Statement
里,你就应该没事了:
PreparedStatement statement = null;
请注意,在准备语句时显式强制转换也是多余的:
statement = connection.prepareStatement("insert into ... etc");