jdbc2.0对批处理sql的支持

jdbc2.0批处理实例:

package com.mldn;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class AddBatch

{

// 连接驱动路径

public static final String DRIVER = “com.mysql.jdbc.Driver”; // “org.gjt.mm.mysql.driver”

// 定义数据源URL:数据库地址

public static final String DBURL = “jdbc:mysql://localhost:3306/school?characterEncoding=utf8”;

// 定义数据库登录的用户名:

public static final String USERNAME = “root”;

// 定义数据库登录的密码:

public static final String PASSWORD = “123456”;

public static void main(String[] args) throws Exception // 抛出所有异常

{

Connection conn = null; // 数据库连接

PreparedStatement pstmt = null; // 预编译语句

ResultSet rs = null; // 结果集

// 加载数据库驱动

Class.forName(DRIVER);

// 获取数据库连接

conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);

String sql = “insert into student(name, age, sex, birthday) values(?, ?, ?, ?)”;

// 获取可滚动的结果集: 对并发更改敏感,并发可更新,这是jdbc2.0的api

pstmt = conn.prepareStatement(sql);

// 分批设置插入值

String[] name = {“www”, “k187”, “com”, “zhangsan”, “lisi”};

int[] age = {10, 20, 50, 60, 99};

String[] sex = {“男”, “女”, “男”, “女”, “男”};

java.util.Date temp = new java.util.Date();

java.sql.Date date = new java.sql.Date(temp.getTime()); // 以今天的时间

java.sql.Date[] birth = {date, date, date, date, date};

for (int x = 0; x < age.length; x++)

{

pstmt.setString(1, name[x]); // 设置name字段

pstmt.setInt(2, age[x]); // 设置

pstmt.setString(3, sex[x]);

pstmt.setDate(4, birth[x]);

pstmt.addBatch(); // 加入批处理,等待执行!

}

// 执行批处理:

int[] inNum = pstmt.executeBatch();

pstmt.close();

String query = “select id, name, age, sex, birthday from student”;

pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

// 查询

rs = pstmt.executeQuery();

rs.afterLast(); // 游标移动最后一行后面

System.out.println(“新添加的行:”);

for (int x = 0; x < inNum.length; x++)

{

rs.previous(); // last –> first

print(rs);

}

rs.close();

pstmt.close();

conn.close();

}

public static void print(ResultSet rs) throws Exception

{

if (rs.getRow() > 0)

{

int id = rs.getInt(1);

String name = rs.getString(2);

int age = rs.getInt(3);

String sex = rs.getString(4);

java.util.Date birth = rs.getDate(5);

System.out.println(“id:” + id + ” 姓名:” + name + ” 年龄:” + age + ” 性别:” + sex + ” 生日:” + birth);

}

}

}

/*

ubuntu@xu-desktop:~$ java com.mldn.AddBatch

新添加的行:

id:40 姓名:lisi 年龄:99 性别:男 生日:2010-04-17

id:39 姓名:zhangsan 年龄:60 性别:女 生日:2010-04-17

id:38 姓名:com 年龄:50 性别:男 生日:2010-04-17

id:37 姓名:k187 年龄:20 性别:女 生日:2010-04-17

id:36 姓名:www 年龄:10 性别:男 生日:2010-04-17

ubuntu@xu-desktop:~$ java com.mldn.AddBatch

新添加的行:

id:45 姓名:lisi 年龄:99 性别:男 生日:2010-04-17

id:44 姓名:zhangsan 年龄:60 性别:女 生日:2010-04-17

id:43 姓名:com 年龄:50 性别:男 生日:2010-04-17

id:42 姓名:k187 年龄:20 性别:女 生日:2010-04-17

id:41 姓名:www 年龄:10 性别:男 生日:2010-04-17

// 每次执行都将添加5行!

*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《jdbc2.0对批处理sql的支持
本文地址:http://www.xiupu.net/archives-163.html
关注公众号:嗅谱网

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!