您的位置:首页 > 大数据

使用JDBC处理大数据

2012-09-08 21:35 169 查看
在实际开发中,程序需要把大文本或二进制数据保存到数据库。

大数据也称之为LOB(Large Objects),LOB又分为:clob和blob

。 clob用于存储大文本。Text

• blob用于存储二进制数据,例如图像、声音、二进制文等。

对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:

• TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT

• TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB

而对于MySQL中的Text类型,可调用如下方法设置:

PreparedStatement.setCharacterStream(index, reader, length);

//注意length长度须设置,并且设置为int型

对MySQL中的Text类型,可调用如下方法获取:

reader = resultSet. getCharacterStream(i);

reader = resultSet.getClob(i).getCharacterStream();

string s = resultSet.getString(i);

对于MySQL中的BLOB类型,可调用如下方法设置:

PreparedStatement. setBinaryStream(i,inputStream, length);

对MySQL中的BLOB类型,可调用如下方法获取:

InputStreamin = resultSet.getBinaryStream(i);//常用

InputStreamin =resultSet.getBlob(i).getBinaryStream();

使用JDBC进行批处理

当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率。

实现批处理有两种方式,第一种方式:

• Statement.addBatch(sql) list

执行批处理SQL语句

• executeBatch()方法:执行批处理命令

• clearBatch()方法:清除批处理命令

例如:

Connectionconn = null;

Statement st = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

String sql1 = "insert intouser(name,password,email,birthday)

values('kkk','123','abc@sina.com','1978-08-08')";

String sql2 = "update user setpassword='123456' where id=3";

st = conn.createStatement();

st.addBatch(sql1); //把SQL语句加入到批命令中

st.addBatch(sql2); //把SQL语句加入到批命令中

st.executeBatch();//执行批处理语句

} finally{

JdbcUtil.free(conn,st, rs);

}

采用Statement.addBatch(sql)方式实现批处理:

• 优点:可以向数据库发送多条不同的SQL语句。

• 缺点:

• SQL语句没有预编译。

• 当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:

Insertinto user(name,password) values(‘aa’,’111’);

Insertinto user(name,password) values(‘bb’,’222’);

Insertinto user(name,password) values(‘cc’,’333’);

Insertinto user(name,password) values(‘dd’,’444’);

实现批处理的第二种方式:

• PreparedStatement.addBatch()

conn = JdbcUtil.getConnection();

String sql = "insert intouser(name,password,email,birthday) values(?,?,?,?)";

st = conn.prepareStatement(sql);

for(inti=0;i<50000;i++){

st.setString(1, "aaa" + i);

st.setString(2, "123" + i);

st.setString(3, "aaa" + i +"@sina.com");

st.setDate(4,new Date(1980, 10,10));

st.addBatch();

if(i%1000==0){

st.executeBatch();

st.clearBatch();

}

}

st.executeBatch();

采用PreparedStatement.addBatch()实现批处理

• 优点:发送的是预编译后的SQL语句,执行效率高。

• 缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。

获得数据库自动生成的主键

Connectionconn = JdbcUtil.getConnection();

String sql = "insert into user(name,password,email,birthday)

values('abc','123','abc@sina.com','1978-08-08')";

PreparedStatement st = conn.

prepareStatement(sql,Statement.RETURN_GENERATED_KEYS );

st.executeUpdate();

ResultSet rs = st.getGeneratedKeys(); //得到插入行的主键

if(rs.next())

System.out.println(rs.getObject(1));

编写存储过程

得到CallableStatement,并调用存储过程:

CallableStatement cStmt =conn.prepareCall("{call demoSp(?, ?)}");

设置参数,注册返回值,得到输出

cStmt.setString(1,"abcdefg");

cStmt.registerOutParameter(2,Types.VARCHAR);

cStmt.execute();

System.out.println(cStmt.getString(2));

对于JDBC不管再怎么谈,最关键的还是要自己多加练习,通过练习,达到熟练地对数据库进行CRUD及其他一些操作处理。这是最重要的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: