来自stackoverflow的答案,注意的是一般为sqlserver中datetime类型传入值的时候,对应的是java.sql.Timestamp类型,别把值传错了,当然以前用hibernate的时候,框架已经把这些类型差异都封装屏蔽好了,现在自己用jdbc写,好多坑坑。。。
You should use an object representation for your parameters, rather than an ArrayList<Object>
of size 2. Also, you don't need your own DataType
enumeration, there is already a java.sql.Types
class. Best of all, there's a setObject()
method on PreparedStatement
that recognizes these types values, so you don't need a switch
statement or calls to type-specific PreparedStatement.set..()
methods.
Here's a solution that has an object representation for the parameters, takes advantage of java.sql.Types
and preparedStatement.setObject()
support for those types, and also insulates you from the type constant values.
First, the object representation for the parameters: