开发者

syntax to update all fields in a table in one statement in sql

Can anyone suggest a Syntax to update all field开发者_运维知识库s in a table in one statement. I want to use it with prepared statement in jdbc. can anyone suggest a example


Using a prepared statement is not that complicated:

PreparedStatement pstmt = connection.prepareStatement(
      "UPDATE my_table SET column_1 = ?, column_2 = ?, column_3 = ?");
// assuming table has columns named as column_1,column_2,column_3 of type int,String,BigDecimal respectively

/* putting the values at runtime */
pstmt.setInt(1, 42); // use value 42 for column_1
pstmt.setString(2, "foo"); // use value 'foo' for column_2
pstmt.setBigDecimal(3, new BigDecimal("123.456")); // use 123.456 for column_3

pstmt.executeUpdate();
connection.commit();

Of course you will need to add error handling to this example.

More examples can be found in the Java Tutorial:

http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html


UPDATE your_table_name SET field1 = 'value1', field2 = 'value2'

Note : I haven't specified WHERE clause, so these changes will be applied to every single row in a table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜