惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
F
Full Disclosure
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
C
Cisco Blogs
M
MIT News - Artificial intelligence
T
Tenable Blog
G
GRAHAM CLULEY
月光博客
月光博客
Recent Announcements
Recent Announcements
V
Visual Studio Blog
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy International News Feed
P
Proofpoint News Feed
I
Intezer
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
The Cloudflare Blog
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog RSS Feed

博客园 - wasd

pear安装步骤 linux定时任务的设置 C语言字符串拆分,打开关闭文件 - wasd - 博客园 js-tips 用optgroup 禁用select中的option方法 - wasd - 博客园 DB2常用语句记录 addslashes、get_magic_quotes_gpc函数、stripslashes函数(转来记录一下) - wasd - 博客园 Linux 强制卸载软件 - wasd - 博客园 Setup locally visual host - wasd B:X星球的身份证系统 B:有道搜索框 B:有道饭团 A:另类的异或 C:Sibonacci - wasd - 博客园 CakePHP支持DB2 PHP 的变量 - wasd - 博客园 DB2 SQLSTATE 消息异常 并行计算简介 PB3编译adobe的例子photoviewer时错误处理
JDBC中Preparedstatement使用小结 及JDBC插入数据后获得Last insert ID
wasd · 2009-05-19 · via 博客园 - wasd

编写SQL语句, 尤其是要插入多个Filed时, 是一件非常BT的事情, 至少让我很不爽, 使用preparedstatement可以使代码变得更优雅一些, 虽然会有些长, 但会更条理, 而且preparedstatement更高于SQL语句 - 相对来说不太容易随着数据库版本改变而变动. 另外preparedstatement效率更高, 安全性更好

下面是恐怖的SQL:

        Statement statement = (Statement) conn.createStatement();
        String sql 
= "INSERT INTO `DEP`.`employee` " +
                
"( `name` , `age` , `address` , `isMale` , `level` , `Department_ID` ) " +
                
"VALUES (" +
                
"'" + emp.getName() + "', " +
                emp.getAge() 
+ "" +
                JdbcUtilities.getSqlForString(emp.getAddress()) 
+ "" +
                emp.getIsMale() 
+ "" +
                emp.getLevel() 
+ "" +
                emp.getDepartment_ID() 
+ ");";

        log.debug(
"Executing sql :" + sql);
        statement.executeUpdate(sql);

        ResultSet rs 
= statement.executeQuery("SELECT last_insert_id();");// .execute("SELECT last_insert_id();");
        if (rs.next()) {
            System.out.println(rs.getInt(
"1"));
        }

这里是使用PreparedStatement后的语句:

Code2

插入一行

  1. 声明 PreparedStatement。

    PreparedStatement prepStmt;
  2. 将一条 SQL 语句指派给 PreparedStatement 对象。

    prepStmt = conn.prepareStatement(
        "INSERT INTO MyTable(MyColumn) values (?)");
  3. 为该语句指派输入参数值。

    以下代码显示一个字符串参数。

    String newValue;
        // assign value
        prepStmt.setStringParameter(1, newValue);
  4. 执行该语句。

    返回值表示受该语句影响的行数。

    long rowsInserted = prepStmt.executeStatement();
  5. 如果禁用了 [自动提交],请提交更改。

    conn.commit();

更新一行

  1. 声明 PreparedStatement。

    PreparedStatement prepStmt;
  2. 将一条语句指派给 PreparedStatement 对象。

    prepStmt = conn.prepareStatement(
        "UPDATE MyTable SET MyColumn1 = ? WHERE MyColumn2 = ?");
  3. 为该语句指派输入参数值。

    String newValue;
        String oldValue;
        // assign values
        prepStmt.setStringParameter( 1, newValue );
        prepStmt.setStringParameter( 2, oldValue );
  4. 执行该语句。

    long rowsUpdated = prepStmt.executeStatement();
  5. 如果禁用了 [自动提交],请提交更改。

    conn.commit();

删除一行

  1. 声明 PreparedStatement。

    PreparedStatement prepStmt;
  2. 将一条语句指派给 PreparedStatement 对象。

    prepStmt = conn.prepareStatement(
        "DELETE FROM MyTable WHERE MyColumn = ?");
  3. 为该语句指派输入参数值。

    String deleteValue;
        prepStmt.setStringParameter(1, deleteValue);
  4. 执行该语句。

    long rowsDeleted = prepStmt.executeStatement();
  5. 如果禁用了 [自动提交],请提交更改。

    conn.commit();
 
转自:http://liguoliang.com/2009/02/773/
http://www.ianywhere.com/developer/product_manuals/sqlanywhere/0902/zh/html/uljvzh9/00000034.htm