您的位置:首页 > 其它

使用jdbc存储图片和大文本

2016-09-09 16:38 211 查看
package cn.itcast.i_batch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Arrays;

import org.junit.Test;

import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
//1 使用Statement对象批量执行sql
public void fun1() throws Exception{

//1 获得连接
Connection conn = JDBCUtils.getConnection();
//2 获得Statement
Statement st =    conn.createStatement();
//3 添加多条sql语句到st中

st.addBatch("create table t_stu ( id int primary key auto_increment , name varchar(20) )");
st.addBatch("insert into t_stu values(null,'tom')");
st.addBatch("insert into t_stu values(null,'jerry')");
st.addBatch("insert into t_stu values(null,'jack')");
st.addBatch("insert into t_stu values(null,'rose')");
//4 执行sql
int[]  results = st.executeBatch();
System.out.println(Arrays.toString(results));
//5关闭资源
JDBCUtils.close(conn, st, null);
}
@Test
//2 使用PrepareStatement对象批量执行sql
public void fun2() throws Exception{

//1 获得连接
Connection conn = JDBCUtils.getConnection();
//2 书写sql语句
String sql = "insert into t_stu values(null,?)";
//3 创建PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4 循环.添加参数
for(int i=0;i<100;i++){
ps.setString(1, "用户"+i);
ps.addBatch();
}
//5 批量执行
int[]  results =ps.executeBatch();
System.out.println(Arrays.toString(results));
//5关闭资源
JDBCUtils.close(conn, ps, null);
}
}


1.使用jdbc存储大文本

package cn.itcast.g_text;

import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
//演示向mysql中存放大文本数据
//存储大文本必须使用PrepareStatement对象
public void fun1() throws Exception{

//1 获得连接
Connection conn = JDBCUtils.getConnection();
//2 书写sql
String sql = "insert into mytext values(null,?)";
//3 创建PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4 设置参数
//参数1:参数的索引
//参数2:需要保存的文本的流
//参数3:文件长度

File f = new File("src/text.txt");

FileReader reader = new FileReader(f);

ps.setCharacterStream(1, reader, (int)f.length());

//5 执行sql
int result = ps.executeUpdate();
System.out.println(result);
//6关闭资源
JDBCUtils.close(conn, ps, null);
}

}


2.使用jdbc存储图片

ackage cn.itcast.h_blob;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import cn.itcast.e_tool.JDBCUtils;
public class Demo {
@Test
//演示向mysql中存放图片
//存储图片必须使用PrepareStatement对象
public void fun1() throws Exception{

//1 获得连接
Connection conn = JDBCUtils.getConnection();
//2 书写sql
String sql = "insert into myblob values(null,?)";
//3 创建PrepareStatement
PreparedStatement ps = conn.prepareStatement(sql);
//4 设置参数
//参数1:参数的索引
//参数2:需要保存的图片的流
//参数3:图片文件长度

File f = new File("src/wg.PNG");

InputStream  is = new FileInputStream(f);

ps.setBinaryStream(1, is, (int)f.length());

//5 执行sql
int result = ps.executeUpdate();
System.out.println(result);
//6关闭资源
JDBCUtils.close(conn, ps, null);
}

}


3.批量执行sql
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐