简体   繁体   English

Java Prepared语句未执行

[英]Java Prepared statement not executing

I have created a small 3 tier program, consisting of : front end -> servlet -> database. 我创建了一个3层小程序,其中包括:前端-> servlet->数据库。

Front end I enter some details into a form. 在前端,我将一些详细信息输入表单。 They are passed to a servlet, which will render some HTML and display the values entered into the form, while also calling a class DatabaseHelper. 它们被传递到servlet,该servlet将呈现一些HTML并显示输入到表单中的值,同时还调用类DatabaseHelper。 The DatabaseHelper then connects and inserts these same values into a table. 然后,DatabaseHelper连接并将这些相同的值插入表中。

I know the values are being passed to the servlet class ok, as they are being displayed in the HTML. 我知道这些值将被传递给servlet类,因为它们正在HTML中显示。 So the problem must lie within the prepared statement. 因此,问题必须出在准备好的语句之内。 Problem is, I cannot see any fault with the statement itself. 问题是,我看不到语句本身有任何错误。 When I query the table itself, there is no data there. 当我查询表本身时,那里没有数据。

Database connectivity is functional, as I can insert values into a database using hardcoded statements, just not a prepared statement. 数据库连通性是有效的,因为我可以使用硬编码的语句而不是准备好的语句将值插入数据库。

Here is a look at the statement Im using. 这里是我正在使用的语句。 Any advice is much appreciated. 任何建议深表感谢。

    public void addRegisterDetails(String name, String email, String country, String password, ){
    try{ 
        String driver = "com.mysql.jdbc.Driver";    
        Class.forName(driver).newInstance();
        // Make db connection
        con = DriverManager.getConnection(url, USERNAME, PASSWORD);   
        st  = con.createStatement();


        String query = " INSERT INTO user_information (name, email, country, password)" + " VALUES (?, ?, ?, ?)";
        PreparedStatement preparedStmt = con.prepareStatement(query);
        preparedStmt.setString (1, name);
        preparedStmt.setString (2, email);
        preparedStmt.setString (3, country);
        preparedStmt.setString (4, password);
        preparedStmt.execute();

    }catch(ClassNotFoundException ex) {
        System.out.println(ex);
    }catch(Exception e){
         e.printStackTrace();
    }
}

Table definition 表定义


id| id | name | 名称| email | 电邮| country | 国家| password 密码

all VARCHAR except the id, which is type INT. 除id(类型为INT)外的所有VARCHAR。

You should invoke the method executeUpdate() on the statement object. 您应该在语句对象上调用方法executeUpdate()

Also, I don't see any call to commit the data, any transaction handling. 另外,我看不到任何提交数据的调用,任何事务处理。 It's fine if you skipped that piece of code for the purpose of this question; 如果出于这个问题的目的跳过了那段代码,那是很好的。 otherwise it's quite an important step ( commit if all goes well, rollback for exception scenarios) 否则,这是非常重要的一步(如果一切顺利,则提交,对于异常情况回滚)

使用executeUpdate进行数据库写操作:

preparedStmt.executeUpdate();

Answer: The database ID was not set to auto increment. 答:数据库ID未设置为自动递增。 For some reason this does not allow you to then insert data to table. 由于某种原因,这不允许您随后将数据插入表中。 Thanks to ChadNC for pointing this out. 感谢ChadNC指出这一点。

Also, why st = con.createStatement(); 另外,为什么st = con.createStatement(); ?

And why do you have a leading space in your query? 为什么在查询中有领先的空间?

String query = " INSERT INTO user_information (name, email, country, password)" 
                 + " VALUES (?, ?, ?, ?)";

This leading space may or may not matter... 这个领先的空间可能或可能不重要...

Lastly, you should be closing your connection when you're through with it, using try-with-resources or a finally block. 最后,您应该在使用完连接后使用try-with-resources或finally块关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM