大数据的存储和批量语句执行
2013-07-10 23:43
267 查看
packagecom.itheima.dao; importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileReader; importjava.io.FileWriter; importjava.io.Reader; importjava.io.Writer; importjava.sql.Connection; importjava.sql.PreparedStatement; importjava.sql.ResultSet; importjava.sql.SQLException; importorg.junit.Test; importcom.itheima.util.JDBCUtil; publicclassD1Dao { //大数据存储 @Test publicvoidaddT1()throwsException { Connectionconn=null; PreparedStatementpst=null; ResultSetrs=null; Filefr=newFile("d://1.txt"); Readerreader=newFileReader(fr); intlen=0; try { conn=JDBCUtil.getConn(); Stringsql="insertintot1values(?,?)"; pst=conn.prepareStatement(sql); pst.setInt(1,1); //clob代表大量的字符数据 pst.setCharacterStream(2,reader,(int)fr.length()); pst.executeUpdate(); }catch(SQLExceptione) { e.printStackTrace(); } } }
packagecom.itheima.dao;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importorg.junit.Test;
importcom.itheima.util.JDBCUtil;
publicclassBlobDao
{
@Test
publicvoidaddBlob()
{
Connectionconn=null;
PreparedStatementpst=null;
try
{
conn=JDBCUtil.getConn();
Stringsql="insertintot2(id,content)values(?,?)";
pst=conn.prepareStatement(sql);
pst.setInt(1,1);
InputStreamis=newFileInputStream("src/1.jpg");
pst.setBinaryStream(2,is,is.available());
pst.executeUpdate();
}catch(Exceptione)
{
e.printStackTrace();
}
finally
{
JDBCUtil.closeAll(conn,pst,null);
}
}
@Test
publicvoidreaderBlob()
{
Connectionconn=null;
PreparedStatementpst=null;
ResultSetrs=null;
try
{
conn=JDBCUtil.getConn();
Stringsql="select*fromt2whereid=?";
pst=conn.prepareStatement(sql);
pst.setInt(1,1);
rs=pst.executeQuery();
if(rs.next())
{
//通过prepareStatement方法调用获取字符流的方法getBinaryStream
InputStreamis=rs.getBinaryStream("content");
OutputStreamfos=newFileOutputStream("d:/2.jpg");
byte[]buff=newbyte[1024];
intlen=0;
while((len=is.read(buff))!=-1)
{
fos.write(buff,0,len);
}
is.close();
fos.close();
}
}catch(Exceptione)
{
e.printStackTrace();
}
finally
{
JDBCUtil.closeAll(conn,pst,rs);
}
}
}
packagecom.itheima.dao;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.transaction.Transaction;
importorg.junit.Test;
importcom.itheima.util.JDBCUtil;
publicclassBatchStatement
{
/**
*Statement的批量操作:可有有多种操作,增加或者删除一起执行
*@authorsimon
*
*/
@Test
publicvoidaddBatch()
{
Connectionconn=null;
Statementpst=null;
conn=JDBCUtil.getConn();
try
{
Stringsql1="insertintot3values(1,'你好')";
Stringsql2="insertintot3values(2,'你好')";
Stringsql3="insertintot3values(3,'你好')";
Stringsql4="insertintot3values(4,'你好')";
Stringsql5="insertintot3values(5,'你好')";
Stringsql6="insertintot3values(6,'你好')";
Stringsql7="deletefromt3whereid=4";
pst=conn.createStatement();
pst.addBatch(sql1);
pst.addBatch(sql2);
pst.addBatch(sql3);
pst.addBatch(sql4);
pst.addBatch(sql5);
pst.addBatch(sql6);
pst.addBatch(sql7);
int[]is=pst.executeBatch();
}catch(SQLExceptione)
{
e.printStackTrace();
}
}
/**
*PreparedStatement只能用于一种形式的批量删除,比如添加
*/
@Test
publicvoidbatchPreparedStatement()
{
Connectionconn=null;
PreparedStatementpst=null;
ResultSetrs=null;
conn=JDBCUtil.getConn();
try
{
pst=conn.prepareStatement("insertintot3values(?,?)");
for(inti=0;i<99;i++)
{
pst.setInt(1,i);
pst.setString(2,"nihao");
pst.executeUpdate();
}
}catch(SQLExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
finally
{
JDBCUtil.closeAll(conn,pst,rs);
}
}
}
相关文章推荐
- C# 执行多条SQL语句,实现数据库事务(通过Hashtable存储数据) .
- Mysql bench执行sql语句批量操作数据所遇到的问题
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- 将表里的数据批量生成INSERT语句的存储过程 继续增强版
- 关于主键列执行批量删除单表数据的存储过程,
- Oracle采用存储过程执行sql语句,将查询到的数据导出到EXCEL中
- 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作
- 将表里的数据批量生成INSERT语句的存储过程 继续增强版
- 在MySQL命令行窗口执行sql脚本,批量执行SQL语句,批量更新数据
- 借助数据库执行sql语句批量生成测试数据
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- 使用查询结果产生的数据批量执行update 或add sql语句
- mssql 生成‘批量Insert数据的sql语句’的存储过程
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- 大量包含Insert语句的脚本文件批量执行导入数据
- C# 执行多条SQL语句,实现数据库事务(通过Hashtable存储数据) .
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作