Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据插入SQL DB-Java时操作按钮不起作用_Java_Mysql_Swing - Fatal编程技术网

将数据插入SQL DB-Java时操作按钮不起作用

将数据插入SQL DB-Java时操作按钮不起作用,java,mysql,swing,Java,Mysql,Swing,我正在尝试将数据插入我的SQL表中。我有一个Jframe: 表单的目的是将在这些文本字段中写入的内容输入数据库。 我的问题是,当我按Send Request时,什么也没有发生。。我错过什么了吗 这是我的“发送请求”按钮代码: 在e.printStackTrace()中是: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manu

我正在尝试将数据插入我的SQL表中。我有一个Jframe:

表单的目的是将在这些文本字段中写入的内容输入数据库。 我的问题是,当我按Send Request时,什么也没有发生。。我错过什么了吗

这是我的“发送请求”按钮代码:

e.printStackTrace()中是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the 
right syntax to use near ''PatientID','MedicalRole','Date','Time','Purpose') VALUES (Nurse,7,2016-04-13,12' at line 1
我认为这是日期语法,它与SQL不同。因此,我将其更改为与SQL表格式匹配。但还是犯了同样的错误


现在我想是关于时间的问题了?

好吧,我听了你的一些评论和反馈。非常感谢你。我还在学习Java,我知道我还有很长的路要走

但不管怎样,多亏了你的指导,我的表格终于开始工作了

PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection)
        DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass");    

        String sql = "INSERT INTO Orders (PatientID, MedicalRole, Date, Time, Purpose) VALUES (?,?,?,?,?)";    

        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, Integer.parseInt(ofPatientIDField.getText()));
        pstmt.setString(2, ofMedRoleComboBox.getSelectedItem().toString());
        pstmt.setObject(3, ofDateField.getValue());
        pstmt.setObject(4, ofTimeField.getText());
        pstmt.setString(5, ofPurposeComboBox.getSelectedItem().toString());

        pstmt.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }
所以是的,我用我以前的try替换了这个,它成功了

谢谢你告诉我使用:)
@MadProgrammer

此方法与
ActionListener
有何关联?另外,打印您可能捕获的异常的堆栈跟踪。请将e.printStrackTrace()添加到您的捕获块中,这样您就可以得到您正在分阶段的错误(如果有),并将其发布到这里,我们都会帮助您从中解脱出来。。!!请参阅“请学习通用Java命名法”(命名约定-例如,
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是一个
大写常量
),并一致地使用它。您确实应该使用它。您还忽略了一条信息,它会告诉您问题的可能原因。添加
e.printStackTrace()到你的
捕获
块的主体哦和e。printStackTrace()也是一个非常好的技巧!帮我弄清楚我错过了什么
PreparedStatement pstmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection)
        DriverManager.getConnection("jdbc:mysql://localhost:3306/database","my-user","my-pass");    

        String sql = "INSERT INTO Orders (PatientID, MedicalRole, Date, Time, Purpose) VALUES (?,?,?,?,?)";    

        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, Integer.parseInt(ofPatientIDField.getText()));
        pstmt.setString(2, ofMedRoleComboBox.getSelectedItem().toString());
        pstmt.setObject(3, ofDateField.getValue());
        pstmt.setObject(4, ofTimeField.getText());
        pstmt.setString(5, ofPurposeComboBox.getSelectedItem().toString());

        pstmt.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }