javafx将数据插入数据库错误

时间:2016-04-14 11:56:51

标签: javafx fxml

我正在使用FXML编写Javafx应用程序,下面是我编写的用于将数据插入到derby数据库中的代码,但它会出现错误消息。请帮助我做错了。

插入日期按钮的代码

package View;
import java.sql.Connection; 
import java.sql.DriverManager; 
import    java.sql.PreparedStatement; 
import java.sql.ResultSet;

import    java.sql.SQLException; 
import javafx.event.ActionEvent;

import    javafx.fxml.FXML; 
import javafx.scene.control.TextField;

/**
  *
  * @author CCB
  */ 
public class ConnectController {    
    @FXML private TextField id;    
    @FXML private TextField surname;   
    @FXML private TextField firstName;
    @FXML private TextField otherNames;
    @FXML private TextField dateOfBirth;
    @FXML private TextField txtsex;
    @FXML private TextField home1;
    @FXML private
    TextField telephone;
    @FXML private TextField email1;
    @FXML
    private TextField staffId;
    @FXML private TextField dofp;
    @FXML
    private TextField doc1;
    @FXML private TextField txtpen;
    @FXML
    private TextField dolp1;
    @FXML private TextField donp1;
    @FXML
    private TextField pos1;
    @FXML private TextField dept1;
    @FXML
    private TextField location1;
    @FXML private TextField fslc;   
    @FXML private TextField sssc;
    @FXML private TextField first;   
    @FXML private TextField addDegree;
    @FXML private TextField
    addDegree2;
    @FXML private TextField date;
    @FXML private
    TextField date1;
    @FXML private TextField date2;
    @FXML private
    TextField date3;
    @FXML private TextField date4;
    @FXML private
    TextField profCert;
    @FXML private TextField profCert1;
    @FXML
    private TextField date5;
    @FXML private TextField date6;

    Connection conn;
    PreparedStatement pstmt=null;
    ResultSet rs=null;

    @FXML
    public void save(ActionEvent action) throws
    SQLException, ClassNotFoundException{      
    Class.forName("org.apache.derby.jdbc.ClientDriver");
               conn=DriverManager.getConnection("jdbc:derby://localhost:1527/Employee","Conduct","ccb");
           try{
               String query ="INSERT INTO Employee(ID,SURNAME,FIRSTNAME,OTHERNAMES,DATEOFBIRTH,SEX,HOMEADDRESS,TELEPHONE,E_MAIL,STAFFID,DATEOFFIRSTAPPOINMTNT,DATEOFCONFIRMATION,DATEOFLASTPROMOTION,LASTOFNEXTPROMOTION,PENNO,POSITION,DEPARTMENT,LOCATION,FIRSTLEAVING,DATE,SENIORCERT,DATE1,FIRSTDEGREE,DATE2,ADDDEGREE,DATE3,ADDDEGREE2,DATE4,PROFCERT,DATE5,PROFCERT1,DATE6)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
               pstmt=conn.prepareStatement(query);
               pstmt.setInt(1,id.getLength());
               pstmt.setString(2,surname.getText());
               pstmt.setString(3,firstName.getText());
               pstmt.setString(4,otherNames.getText());
               pstmt.setString (5,dateOfBirth.getText());
               pstmt.setString (6,txtsex.getText());
               pstmt.setString(7,home1.getText());
               pstmt.setInt(8,telephone.getLength());
               pstmt.setString(9,email1.getText());
               pstmt.setString(10,staffId.getText());
               pstmt.setString(11,dofp.getText());
               pstmt.setString(12,doc1.getText());
               pstmt.setString(13,dolp1.getText());
               pstmt.setString(14,donp1.getText());
               pstmt.setString(15, txtpen.getText());
               pstmt.setString(16,pos1.getText());
               pstmt.setString(17,dept1.getText());
               pstmt.setString(18,location1.getText());
               pstmt.setString(19,fslc.getText());
               pstmt.setString(20,date.getText());
               pstmt.setString(21,sssc.getText());
               pstmt.setString (22,date1.getText());
               pstmt.setString(23,first.getText());
               pstmt.setString(24,date2.getText());
               pstmt.setString(25,addDegree.getText());
               pstmt.setString(26,date3.getText());
               pstmt.setString(27, addDegree2.getText());
               pstmt.setString(28, date4.getText());
               pstmt.setString(29,profCert.getText());
               pstmt.setString(30,date5.getText());
               pstmt.setString(31,profCert1.getText());
               pstmt.setString(32,date6.getText());
               rs=pstmt.executeQuery();

           }
        catch(Exception e){



        }
    } 
}

错误消息

       Exception in thread "JavaFX Application Thread"    java.lang.RuntimeException:   
java.lang.reflect.InvocationTargetException     at   
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at   
javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at    com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at    com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at    com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at    com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at    com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)  at    javafx.event.Event.fireEvent(Event.java:198)  at   
javafx.scene.control.MenuItem.fire(MenuItem.java:462)   at   
com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
    at    com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
    at    com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at    com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at    com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at    com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at    com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at    com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at    com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at    com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at    com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at    com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at    com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at    com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at    com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)  at    javafx.event.Event.fireEvent(Event.java:198)  at   
javafx.scene.Scene$MouseHandler.process(Scene.java:3757)    at   
javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)    at   
javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)  at   
javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)    at
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at    com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)   at   
com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
    at    com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at    com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)    at    com.sun.glass.ui.View.notifyMouse(View.java:937)  at   
com.sun.glass.ui.win.WinApplication._runLoop(Native Method)     at   
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745) Caused by:    java.lang.reflect.InvocationTargetException  at   
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at   
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)     at    sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)    at   
sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)     at   
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)     at    sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)   at   
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 43 more Caused by: javafx.fxml.LoadException:     file:/C:/Users/CCB/Documents/NetBeansProjects/HumanResourceManagementInformationSystem/dist/run1769813246/HumanResourceManagementInformationSystem.jar!/View/Employee.fxml:50

    at    javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)    at    javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)     at   
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)   at   
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)   at   
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)   at   
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)   at   
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)   at   
javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)   at   
View.MainCotroller.add(MainCotroller.java:35)   ... 53 more Caused
by:    java.lang.IllegalArgumentException: Can not set   
javafx.scene.control.TextField field View.ConnectController.txtsex
to    javafx.scene.text.Text    at   
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at    sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)  at    javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)     at   
javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)     at   
javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
    at    javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)    ... 61    more

1 个答案:

答案 0 :(得分:1)

您在控制器中将txtSex定义为TextField,但相应的元素在FXML文件中定义为Text

java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField field View.ConnectController.txtsex to javafx.scene.text.Text