简体   繁体   中英

SQLServer - Is it possible to update a geometry column using a jdbc resultset?

Is it possible to update a geometry column in a SQLServer database using a "updatable" jdbc resultset?

I've tried both the updateString() and updateObject() methods to no avail. For example:

String point = "geometry::STGeomFromText('POINT (30 -20)',4326)";
rs.updateString("COORDINATE", point);
rs.update();

Throws exception:

com.microsoft.sqlserver.jdbc.SQLServerException: The string is not in a valid hex format.

I haven't been able to find what this error means. Do I need to escape certain characters? Am I allowed to pass a function as a string? Or should I use a different update method like updateBlob() or updateNCharacterStream()?

BTW, I can update the geometry using a prepared statement but I prefer not to. Example:

String sql = "UPDATE MY_TABLE SET COORDINATE=geometry::STGeomFromText(?,4326) WHERE ID=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "POINT (30 -20)");

Again, I don't want to use prepared statements. I would like to update records using the resultset (eg updateString).

Your current code is the equivalent of trying to set the value of the column to the string "geometry::STGeomFromText('POINT (30 -20)',4326)" which obviously doesn't work as a string value of a function call is not the same as a function call itself. UpdateString can only be used to set a value, not call a function.

Based on the error message, you need to use a hex string with the already encoded coordinate. I don't know if there is a method available in Java to calculate this, so I fear your existing workaround using UPDATE might be the only solution available to you.

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