简体   繁体   English

PreparedStatement - 如何指定使用列的默认值

[英]PreparedStatement - how specify to use default value of column

I have a table created like this: 我有一个像这样创建的表:

CREATE TABLE [dbo].[LogInfo](
    [date_current] [datetime] NULL,
    [classname] [varchar](500) NULL,
    [output] [varchar](500) NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[LogInfo] ADD  DEFAULT (getdate()) FOR [date_current]
GO

so the 'date_current' column defaults to the current date and time. 所以'date_current'列默认为当前日期和时间。

I have a prepared statement like this: 我有这样一个准备好的声明:

PreparedStatement psInsert_ = conn.prepareStatement("INSERT INTO LogInfo VALUES(?,?,?)");
psInsert_.setTimestamp(1, ????????);
psInsert_.setString(2, "test text1");
psInsert_.setString(3, "test text2");

I'm at a bit of a loss as how to specify the first parameter of the prepared statement and can't find an example of anything like it anywhere. 我有点不知道如何指定预准备语句的第一个参数,并且无法在任何地方找到类似的任何示例。

If I try leave out the default parameter: 如果我尝试省略默认参数:

PreparedStatement psInsert_ = conn.prepareStatement("INSERT INTO LogInfo ('classname','output') VALUES(?,?)");
psInsert_.setString(1, "test text1");
psInsert_.setString(2, "test text2");

I get an error saying that 'classname' and 'output' are invalid column names. 我收到一条错误消息,说'classname'和'output'是无效的列名。

How should I do this? 我该怎么做? Thanks. 谢谢。

From the API: 来自API:

void setDate(int parameterIndex, Date x)

Make sure your date is a Date object. 确保您的日期是Date对象。

Reference: 参考:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setDate(int,%20java.sql.Date http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setDate(int,%20java.sql.Date

Your second query fails because the column names are in single quotes. 您的第二个查询失败,因为列名称是单引号。 Try: 尝试:

INSERT INTO LogInfo (classname,output) VALUES(?,?) INSERT INTO LogInfo(classname,output)VALUES(?,?)

Don't quote the columns. 不要引用列。 You can optionally use the DEFAULT keyword: 您可以选择使用DEFAULT关键字:

INSERT INTO LogInfo (date_current, classname, output) VALUES(DEFAULT, ?, ?)

I believe your error lies in the fact that you have single quotes around classname and output. 我相信你的错误在于你在classname和output周围有单引号。 Remove the single quotes, and your second solution should work. 删除单引号,您的第二个解决方案应该工作。

The issue in your query is that you use single quotes ' ' which are used for string literals and not object names, you want to use brackets [ ] or nothing at all. 您的查询中的问题是您使用单引号' '用于字符串文字而不是对象名称,您想要使用方括号[ ]或什么都不使用。 Since you have a default defined you can skip that column in the insert statement: 由于您已定义默认值,因此可以在insert语句中跳过该列:

"INSERT INTO LogInfo ([classname],[output]) VALUES(?,?)"

or 

"INSERT INTO LogInfo (classname,output) VALUES(?,?)"

Using delimited identifiers is really only needed when your column name would be invalid for some reason (it might be a reserved keyword, or start with an invalid character, or contain a space etc). 实际上只有当您的列名由于某种原因无效时才需要使用分隔标识符(它可能是保留关键字,或者以无效字符开头,或者包含空格等)。 Your columns does not need any, so you should/could skip them. 您的列不需要任何列,因此您应该/可以跳过它们。

See MSDN: Database Identifiers for more information on the naming rules for SQL Server. 有关SQL Server命名规则的详细信息,请参阅MSDN: 数据库标识符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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