大文本的提取:
/*
create table t1(
id int primary key,
content LONGTEXT
);
*/
//存入數據庫
public class ClobDemo {
public void testAdd(){
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("insert into t1(id,content) values(?,?)");
stmt.setInt(1, 1);
File file=new File("src/jpm.txt");
Reader reader=new FileReader(file);
stmt.setCharacterStream(2, reader, (int)file.length());
stmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
//從數據庫中讀取
@Test
public void testRead(){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("select * from t1 where id=1");
rs=stmt.executeQuery();
if(rs.next()){
Reader reader=rs.getCharacterStream("content");
Writer writer=new FileWriter("e:/jpm.txt");
char buf[]=new char[1024];
int len=-1;
while((len=reader.read(buf))!=-1){
writer.write(buf,0,len);
}
reader.close();
writer.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
}sql
二進制數據的存取:
/*
create table t2(
id int primary key,
content LONGBLOB
);
*/
//村
public class BlobDemo {
public void addtest(){
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("insert into t2(id,content) values(?,?)");
stmt.setInt(1, 1);
InputStream in=new FileInputStream("src/1.jpg");
stmt.setBinaryStream(2, in, in.available());
stmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
//取
@Test
public void readtest(){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("select * from t2 where id=1");
rs=stmt.executeQuery();
if(rs.next()){
InputStream in=rs.getBinaryStream("content");
OutputStream out=new FileOutputStream("e:/1.jpg");
byte byt[]=new byte[1024];
int len=-1;
while((len=in.read(byt))!=-1){
out.write(byt, 0, len);
}
in.close();
out.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
}數據庫
批處理:
/*
create table t3(
id int primary key,
string varchar(100)
);
*/
//多個語句
public class TextBatch {
public void testBatch1() throws Exception{
Connection conn = JdbcUtil.getConnection();
Statement stmt = conn.createStatement();//Statement內部有一個List<String sql>集合
String sql1 = "insert into t3 values(1,'aaa1')";
String sql2 = "insert into t3 values(2,'aaa2')";
String sql3 = "delete from t3 where id=1";
//把三條語句放到Statement的List集合中
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
//執行
int ii[] = stmt.executeBatch();//元素:每條語句影響到的行數
for(int i:ii)
System.out.println(i);
JdbcUtil.release(null, stmt, conn);
}
//1000個語句
public void testBatch2() throws Exception{
Connection conn=JdbcUtil.getConnection();
PreparedStatement stmt=conn.prepareStatement("insert into t3 values(?,?)");
for(int i=0;i<1000;i++){
stmt.setInt(1, i);
stmt.setString(2, "aaa"+i);
stmt.addBatch();
}
stmt.executeBatch();
JdbcUtil.release(null, stmt, conn);
}
//10002個語句
@Test
public void testBatch3() throws Exception{
Connection conn=JdbcUtil.getConnection();
PreparedStatement stmt=conn.prepareStatement("insert into t3 values(?,?)");
for(int i=0;i<100002;i++){
stmt.setInt(1, i);
stmt.setString(2, "aaa"+i);
if(i%1000==0){
stmt.addBatch();
stmt.executeBatch();
}
}
stmt.executeBatch();
JdbcUtil.release(null, stmt, conn);
}
}.net
jdbc的調用存儲過程:
/*
delimiter $$對象
CREATE PROCEDURE demoSp(IN inputParam VARCHAR(255), INOUT inOutParam varchar(255))
BEGIN
SELECT CONCAT('welcome to:', inputParam) into inOutParam;
END $$get
delimiter ;
*/input
//如何調用數據庫中的存儲過程
public class ProcedureDemo {
//CallableStatement
@Test
public void test() throws Exception{
Connection conn = JdbcUtil.getConnection();
ResultSet rs = null;
//獲取執行存儲過程的對象
CallableStatement stmt = conn.prepareCall("{call demoSp(?,?)}");//{call demoSp(?,?)} 除了存儲過程的名稱,其他固定寫法
//設置參數:輸入參數要給一個值;輸出參數註冊SQL數據類型
//輸入參數要給一個值
stmt.setString(1,"hch");
//輸出參數註冊SQL數據類型
stmt.registerOutParameter(2, Types.VARCHAR);
//執行
stmt.execute();
String result = stmt.getString(2);
System.out.println(result);
JdbcUtil.release(rs, stmt, conn);
}
}string