使用PreparedStatement将行插入数据库 [英] Insert row into database with PreparedStatement

查看:102
本文介绍了使用PreparedStatement将行插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PreparedStatement在表中插入一行。该表有3列(int,String,String)。问题是int列是AUTO_INCREMENT,所以我想将该变量留空并让数据库完成该工作(它是一个id)。虽然还没有找到怎么做!

I want to insert a row into a table using PreparedStatement. The table got 3 columns(int, String, String). The thing is that the int column is AUTO_INCREMENT, so I want to leave that variable empty and let the database do that job (It's an id). Haven't found out how to do this though!

这是一个MySQL数据库。

It's an MySQL database.

推荐答案

这个相对简单,只需从列列表中省略自动增量列:

That's relatively simple, just leave out the autoincrement column from the column list:

insert into mytbl(str1, str2) values (?, ?)

准备语句,设置两个参数,然后执行非查询模式。

Prepare the statement, set the two parameters, and execute in non query mode.

这篇关于使用PreparedStatement将行插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆