一、关于在数据库的使用步骤,和存储数据方式
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import org.junit.Test;
public class demo1 {
@Test
public void test01() throws Exception{
Connection conn =null;
try {
//进行了数据库的提交
conn = JDBCUtil.getConnection();
//把sql的管理事务设置为手动提交,设置了以后需要提交就要commit
//设置手动提交的步骤:
//1)setAutocommit(false);false为手动提交,默认为true为自动提交
conn.setAutoCommit(false);
//setTransactionIsolation(int):int参数表示的是事务管理的隔离性
//参数:1、表示是read uncommitted;
//2、表示是read commited;
//4、表示是repeatable read;8、表示是serializable
conn.setTransactionIsolation(4);
//编译预处理sql语句
PreparedStatement stmt = conn.prepareStatement("insert into attachments values(?,?,?,?,?)");
File file = new File("d:/exam.xml");
InputStream inputStream = new FileInputStream(file);
Date date = new Date();
//设置预处理sql语句中的参数
stmt.setInt(1, 1);
stmt.setString(2, "福利");
stmt.setBlob(3, inputStream);
// stmt.setDate(4,new java.sql.Date(date.getTime()));
stmt.setTimestamp(4, new Timestamp(date.getTime()));
stmt.setString(5, "mjf");
stmt.executeUpdate();
//2)设置了手动提交的话,设置commit()
conn.commit();
JDBCUtil.close(stmt, conn);
} catch (Exception e) {
e.printStackTrace();
//3)设置了手动提交的话,当程序有错时候可以回滚数据
conn.rollback();
}
}
@Test
public void test2() throws Exception{
Connection conn = JDBCUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from attachments where id =? ");
stmt.setInt(1, 1);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
InputStream inputStream = rs.getBlob("file").getBinaryStream();
File file = new File("d:/exam1.xml");
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[inputStream.available()];
int leng = 0;
while((leng=inputStream.read())!=-1){
out.write(b, 0, leng);
}
out.close();
inputStream.close();
}
JDBCUtil.close(rs, stmt, conn);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//jdbc的工具类
public class JDBCUtil {
//mysql数据库
private static String url = "jdbc:mysql://localhost:3306/day18";
private static String user = "root";
private static String password = "123";
private static String driverClass = "com.mysql.jdbc.Driver";
/**
* 驱动:oracle.jdbc.driver.OracleDriver
URL:jdbc:oracle:thin:@machine_name:port:dbname
注:machine_name:数据库所在的机器的名称;
port:端口号,默认是1521
*/
/*//oracle数据库
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user = "eric";
private static String password = "123456";
private static String driverClass = "oracle.jdbc.driver.OracleDriver";*/
//只需要注册一次
static{
//注册驱动
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获取连接
*/
public static Connection getConnection(){
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 关闭资源
*/
public static void close(Statement stmt,Connection conn){
try {
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 关闭资源
*/
public static void close(ResultSet rs,Statement stmt,Connection conn){
try {
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
二、关于在数据库实现批处理和不实现批处理的比较,在数据较多的情况下,实现批处理有利于优化效率
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class demo2 {
@Test
public void testTime() throws Exception{
long start = System.currentTimeMillis();
//test1();
//test2();
//test3();
test4();
long end = System.currentTimeMillis();
System.out.println("运行了:" + (end - start));
}
//通过statement对象来,没有用批处理来进行插入
//运行的时间:mysql: 37842 oracle:16664
@Test
public void test1() throws Exception{
Connection conn = JDBCUtil.getConnection();
Statement stmt = conn.createStatement();
for(int i=1;i<=1000;i++){
stmt.executeUpdate("insert into student values ("+i+",'张三','1000')");
}
JDBCUtil.close(stmt, conn);
}
//通过statement的批处理来处理数据
//运行时间: mysql:36829 oracle:19991
@Test
public void test2() throws Exception{
Connection conn = JDBCUtil.getConnection();
Statement stmt = conn.createStatement();
for(int i=1;i<=1000;i++){
//1)先把数据都添加到缓存区
stmt.addBatch("insert into student values ("+i+",'张三','1000')");
//2)在通过每1000条数据存满了就发送数据库
if(i%1000==0){
stmt.executeBatch();
//3)发送完以后就要清空缓存区
stmt.clearBatch();
}
}
JDBCUtil.close(stmt, conn);
}
//通过PreparedStatement对象来,没有用批处理来进行插入
//运行的时间:mysql: 45300 oracle:11202
@Test
public void test3() throws Exception{
Connection conn = JDBCUtil.getConnection();
String sql = "insert into student values (?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
for(int i=1;i<=1000;i++){
stmt.setInt(1, i);
stmt.setString(2, "张三");
stmt.setDouble(3, 1000);
stmt.executeUpdate();
}
JDBCUtil.close(stmt, conn);
}
//通过PreparedStatement对象并且运用批处理来处理
//运行的时间:mysql: 40576 oracle:1057
@Test
public void test4() throws Exception{
Connection conn = JDBCUtil.getConnection();
String sql = "insert into student values (?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
for(int i=1;i<=1000;i++){
stmt.setInt(1, i);
stmt.setString(2, "张三");
stmt.setDouble(3, 1000);
//1)把参数都加入缓存区
stmt.addBatch();
//2)在通过每1000条数据存满了就发送数据库
if(i%1000==0){
stmt.executeBatch();
//3)发送完以后就要清空缓存区
stmt.clearBatch();
}
}
JDBCUtil.close(stmt, conn);
}
}
三、在存数据到数据库中的时候,如果数据出现date too large的问题的解决方案
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class demo3 {
//注意:mysql数据库默认情况下,一次只能接受不超过1M的数据
//如果需要保存超过1M的数据,则需要修改mysql的配置 :max_allowed_packet
//步骤:
// 1) 找到mysql的安装目录下,my.ini配置,在最后添加一个变量: max_allowed_packet=50M
// 2) 重启mysql数据库
@Test
public void write() throws Exception{
Connection conn = JDBCUtil.getConnection();
String sql = "insert into attachments(name,file,addtime,author) values(?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
InputStream inputStream = new FileInputStream(new File("c:/exam.jpg"));
stmt.setInt(1, 1);
stmt.setBlob(2, inputStream);
/*
* 设置时间的参数,有两种方式:
* 方式一:可以直接设置Date,Time,Timestamp三种,date为年月日,timestamp为年月日时分秒,这里的Date都是java.sql下面
* 并且他们的构造方法中需要参数为long型的时间
*/
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
//stmt.setTime(3, new Time(new Date().getTime()));
//stmt.setDate(3, new Date(new Date().getTime()));
/*
* 方式二:直接给该参数设置一个字符串,把时间当做字符串来处理
*/
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// stmt.setString(3,sdf.format(new Date()));
stmt.executeUpdate();
JDBCUtil.close(stmt, conn);
}
}
数据库的操作能够解决数据的存取问题,现已广泛的应用中