您的位置:首页 > 大数据

大数据的存储和批量语句执行

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);
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: