Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2537456
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-27 16:05:31

这篇文章主要介绍怎样通过JDBC Statement来向数据库表中插入记录。
通过调用statement.execute()方法来执行sql语句,语法如下:
  1. stmt = conn.createStatement();
  2. stmt.executeUpdate(sql);

代码如下:
  1. private static void insertRecord() throws SQLException{
  2.         Connection conn = null;
  3.         Statement stmt = null;
  4.         
  5.         String sql = "INSERT INTO t_user(username,password) values"
  6.             +"('Dendi','password')";
  7.         
  8.         try{
  9.             conn = getDBConnection();
  10.             stmt = conn.createStatement();
  11.             System.out.println(sql);
  12.             stmt.executeUpdate(sql);
  13.             System.out.println("A Record has been inserted!");
  14.         }catch(SQLException e){
  15.             System.out.println(e.getMessage());
  16.         }finally{
  17.             if(stmt!=null){
  18.                 stmt.close();
  19.             }
  20.             if(conn!=null){
  21.                 conn.close();
  22.             }
  23.         }
  24.     }

阅读(1105) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~