(JavaFx + PHPMyAdmin)连续执行两个方法时的SQL异常(SELECT然后INSERT)

时间:2016-04-14 06:44:21

标签: mysql javafx

虽然我想连续两个方法,它给了我一个SQLException。抛出异常的是goNext()

我做了verifyID()然后goNext()

红线是我想从数据库中选择并从所选数据中创建一条新记录。

private void verifyID() {

    nameTxtFld.setDisable(false);
    instNameTxtFld.setDisable(false);
    addTxtArea.setDisable(false);
    nextButton.setDisable(false);
    KonekDB.createConnection();
    try {

        String sql = "SELECT * FROM privateguest WHERE idNumber='" + idNumTxtFld.getText() + "' AND idType= '" + idType.getSelectionModel().getSelectedIndex() + "'";
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        if (rs.next()) {
            String NM = rs.getString("name");
            nameTxtFld.setText(NM);
            String INS = rs.getString("institution");
            instNameTxtFld.setText(INS);
            String ADD = rs.getString("address");
            addTxtArea.setText(ADD);

        }
    } catch (SQLException ex) {
        System.out.println("SQL Exception (verify)");
    }
    KonekDB.closeConnection();
}


private void goNext() {

    if (nameTxtFld.getText() == null || nameTxtFld.getText().trim().isEmpty()
            || instNameTxtFld.getText() == null || instNameTxtFld.getText().trim().isEmpty()
            || addTxtArea.getText() == null || addTxtArea.getText().trim().isEmpty()) {
        alertDialog.showAndWait();
    } else {
        String satu = idNumTxtFld.getText();
        String dua = nameTxtFld.getText();
        String tiga = addTxtArea.getText();
        String empat = instNameTxtFld.getText();
        int delapan = idType.getSelectionModel().getSelectedIndex();
        String sembilan = timeStamp.getText();
        try {
            KonekDB.createConnection();
            String sql = "INSERT INTO privateguest"
                    + "(idNumber, name, address, institution, idType, startTime) "
                    + "VALUES "
                    + "('" + satu + "','" + dua + "','" + tiga + "','" + empat + "','" + delapan + "','" + sembilan + "')";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);

            if (rs.next()) {

                Frame1 frame1 = new Frame1(english);
                this.getScene().setRoot(frame1);
            }

        } catch (SQLException ex) {
            System.out.println("SQL Exception (next)");
        }
        Frame3Private frame3 = new Frame3Private(english);
        this.getScene().setRoot(frame3);
    }

}

这是我的数据库连接类

package ClientSide;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class KonekDB {

private static String path = null;
private static String url_db = null;
private static String username = null;
private static String password = null;
public static Connection conn = null;

public static Connection createConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        path = "localhost/admtamudki";
        url_db = "jdbc:mysql://" + path;
        username = "root";
        password = "";
        conn = DriverManager.getConnection(url_db, username, password);
        System.out.println("Koneksi Database Sukses");
    } catch (ClassNotFoundException e) {
        System.out.println("ClassNotFoundException<Wanna Create>");
        System.exit(0);
    } catch (SQLException e) {
        System.out.println("SQLException<Wanna Create>");
        System.exit(0);
    }
    return conn;
}

public static Connection getConnection() {
    return conn;
}

public static void closeConnection() {
    try {
        conn.close();
        System.out.println("Database closed");
    } catch (SQLException e) {
        System.out.println("SQLException<Wanna Close>");
        System.exit(0);
    }
}
}

这是堆栈跟踪。我只是通过printStackTrace找到它。

java.sql.SQLException: Field 'targetedDiv' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469)
at ClientSide.Frame2Private.goNext(Frame2Private.java:349)
at ClientSide.Frame2Private.lambda$EventHandler$3(Frame2Private.java:401)
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.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.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.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
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.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)

仅供参考:如果我直接在PHPMyAdmin DBMS SQL菜单中执行它,SQL字符串运行良好。

1 个答案:

答案 0 :(得分:0)

ALHAMDULILLAH

现在是晚上10点,我终于找到了要解决的问题。问题是我忘记在PHPMyAdmin中设置我的字段的默认值。

案件结束,感谢您的关注。睡个好觉。