简体   繁体   中英

java.sql.PreparedStatement.executeUpdate() not working with mysql database

Im using MySQL and decided to write my own database helper class that would give me android-like syntactic sql, however, when i try execute an update statement using a PreparedStatement, when i run the code, no change takes place in the database, but no exception is thrown. I double-checked my connection string, made sure my .jars were in order, triple checked my syntax, but nothing. Code:

DatabaseHelper.java:

public class DatabaseHelper {

String dbHost = "localhost";
String dbPort = "3306";
String dbName = "nakomangdb";
String dbUsername = "root";
String dbPassword = "sqldb";
String connectionString = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
Connection connection;
PreparedStatement preparedStatement;

public DatabaseHelper() {
}

public void closeDatabase() {
    try {
        if (!connection.isClosed())
            connection.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

}

public void openDatabase() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(connectionString, dbUsername, dbPassword);
        connection.setAutoCommit(true);
    } catch (ClassNotFoundException | SQLException ex) {
        ex.printStackTrace();
    }
}

public void update(String table, String[] set, Object[] to, String whereClause, Object[] whereArgs) {
    StringBuilder sql = new StringBuilder("update ");
    sql.append(table);
    sql.append(" set ");

    for (int i = 0; i < set.length; i++) {

        sql.append(set[i]);
        sql.append(" = ?");

        if (i != (set.length - 1)) {
            sql.append(", ");
        }

    }
    sql.append(" ");

    int argCount = to.length;

    try {

        if (whereClause != null) {
            sql.append(whereClause);
            sql.append(";");

            preparedStatement = connection.prepareStatement(sql.toString());

            for (Object s : whereArgs) {
                preparedStatement.setObject(argCount++, s);
            }

        } else {

            sql.append(";");
            preparedStatement = connection.prepareStatement(sql.toString());

        }

        argCount = 0;

        for (Object s : to) {
            preparedStatement.setObject(argCount++, s);
        }

        preparedStatement.executeUpdate();

        preparedStatement.close();

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

}

}

Calling Code:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    DatabaseHelper dbHelper = new DatabaseHelper();

    dbHelper.openDatabase();
    dbHelper.update("customer", 
            new String[]{"fullName"}, 
            new Object[]{"vernon"}, 
            "where ID=?", 
            new Object[]{1});
    dbHelper.closeDatabase();
    response.getWriter().append("Done!");
}

Parameter index out of range (0 < 1 ), which makes no sense because argCount is never 0

Oh yes it is. The exception does not lie.

I checked

Check again. Read your code. I quote:

argCount = 0;

does the order in which the PrepaaredStatement.setXXX methods are called matter?

No.

do i strictly have to set arg positions 1,2,3 etc in that order

No.

or can i set 3,4,1,2?

Yes.

The problem is that argCount should be initialized to 1, not zero, or else you should use ++argCount rather than argCount++ .

Your helper class would be a lot simpler if the database was opened in the constructor. You could also pass the statement-related stuff to the constructor and have it prepared there as well.

autoCommit=true is the default. The Class.forName() line hasn't been needed since 2007.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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