Java Prepared statement not executing

非 Y 不嫁゛ 提交于 2019-12-20 04:33:01

问题


I have created a small 3 tier program, consisting of : front end -> servlet -> database.

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. The DatabaseHelper then connects and inserts these same values into a table.

I know the values are being passed to the servlet class ok, as they are being displayed in the 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| name | email | country | password

all VARCHAR except the id, which is type INT.


回答1:


You should invoke the method executeUpdate() on the statement object.

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)




回答2:


Use executeUpdate for database write operations:

preparedStmt.executeUpdate();



回答3:


Answer: The database ID was not set to auto increment. For some reason this does not allow you to then insert data to table. Thanks to ChadNC for pointing this out.




回答4:


Also, why 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.



来源:https://stackoverflow.com/questions/14385748/java-prepared-statement-not-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!