Results 1 to 4 of 4

Thread: Updates with JdbcTemplate not being commited

  1. #1
    Join Date
    Nov 2007
    Posts
    12

    Default Updates with JdbcTemplate not being commited

    Hi, I'm trying to update a record in a MS SQL 2000 Db using the JdbcTemplate.Update method. While using the JdbcTemplate to query the database works correctly, the update method has no effect. My java code which attempts to update the DB is as follows:

    Code:
        public int updateRank(Object[] statement) {
            try {
                String sql = "UPDATE ValidRank SET ?=? WHERE [Id]=?";
                return this.jdbcTemplate.update(sql, statement);
            } catch (Exception e) {
                MessageLog.logError(e, "Error in updateRank");
                return -1;
            }
        }
    The object being passed contains the relevant values and the Id of the row to be updated. The SQL throws no errors and the same SQL with the relevant values updates the row when entered using MS query browser. The method also returns 1 when called, presumably because it thinks it has updated 1 row in the DB. However no changes are ever committed to the DB. There are no transactional features being used and my connection string is as follows:

    Code:
    <Resource name="jdbc/personnel" 
    url="jdbc:microsoft:sqlserver://somewhere.domain.org:1433;databasename=xxx;user=xxx;password=xxx"
    auth="Container" defaultAutoCommit="true" 
    driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" 
    type="javax.sql.DataSource" />
    I've got no clue as to why this is happening, so I'd appreciate some pointers. This is all happening in Tomcat 6.0 by the way.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Configure transactions. Without transactions nothing is going to be inserted/updated to the database.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    Are you sure "... SET ?=? ..." is valid usage for SQL Server. Looks suspicious to me to pass in a column name as a parameter. Try "... SET " + columnName + "=? ..."
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  4. #4
    Join Date
    Nov 2007
    Posts
    12

    Default

    It appears having the field name within the statement was indeed causing the problem. Changing the update method to the following fixed the issue:

    Code:
        public int updateRank(String fieldName, Object[] statement) {
            try {
                String sql = "UPDATE ValidRank SET " + fieldName + "=? WHERE [Id]=?";
                return this.jdbcTemplate.update(sql, statement);
            } catch (Exception e) {
                MessageLog.logError(e, "Error in updateRank");
                return -1;
            }
        }
    Thanks everyone for the input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •