目录

1.方式一:使用PreparedStatement

2.方式二:使用addBatch(),excuteBatch(),clearBatch()

3.方式三:设置不允许自动提交数据

4.总结


1.方式一:使用PreparedStatement

步骤

①获取连接。

②编写SQL语句。

③预编译SQL语句。

④填充占位符。

⑤执行SQL语句。

实例:创建数据表并包含一个int类型字段userid,并向其中添加2万条数据。

package lib;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class Test2 {
	public static void main(String []args) {
		InputStream is = null;
		Connection connection = null;
		try {
			//获取连接
			is = Test2.class.getClassLoader().getResourceAsStream("test.properties");
			Properties properties=new Properties();
			properties.load(is);
			String user=properties.getProperty("user");
			String password=properties.getProperty("password");
			String url=properties.getProperty("url");
			String driver=properties.getProperty("driver");
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
			//删除旧表
			String sql="drop table test";
			PreparedStatement ps=connection.prepareStatement(sql);
			ps.execute();
			//创建新表
			sql="create table test (userid int)";
			ps=connection.prepareStatement(sql);
			ps.execute();
			//插入数据
			sql="insert into test values (?)";
			ps=connection.prepareStatement(sql);
			for(int i=1;i<=20000;i++) {
				ps.setObject(1, i);
				ps.execute();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//资源关闭
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

2.方式二:使用addBatch(),excuteBatch(),clearBatch()

步骤

①获取连接。

②编写sq语句。

③预编译SQL语句。

④填充占位符。

⑤调用PreparedStatement对象的addBatch()攒SQL语句。

⑥调用PreparedStatement对象的excuteBatch()执行SQL语句。

⑦调用PreparedStatement对象的clearBatch()清空Batch。

注意点:

①上面的三个函数作用分别是:攒SQL语句、执行SQL语句、清空SQL语句。

②MySQL服务器默认是关闭批处理的,需要设置一个参数使MySQL支持批处理操作。做法:在配置文件中URL后面加上:?rewriteBatchedStatements=true

实例:同上

package lib;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class Test2 {
	public static void main(String []args) {
		InputStream is = null;
		Connection connection = null;
		try {
			//获取连接
			is = Test2.class.getClassLoader().getResourceAsStream("test.properties");
			Properties properties=new Properties();
			properties.load(is);
			String user=properties.getProperty("user");
			String password=properties.getProperty("password");
			String url=properties.getProperty("url");
			String driver=properties.getProperty("driver");
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
			//删除旧表
			String sql="drop table test";
			PreparedStatement ps=connection.prepareStatement(sql);
			ps.execute();
			//创建新表
			sql="create table test (userid int)";
			ps=connection.prepareStatement(sql);
			ps.execute();
			//插入数据
			sql="insert into test values (?)";
			ps=connection.prepareStatement(sql);
			for(int i=1;i<=20000;i++) {
				ps.setObject(1, i);
				//攒SQL语句
				ps.addBatch();
				if(i%500==0) {
					//执行SQL语句
					ps.executeBatch();
					//清空SQL语句
					ps.clearBatch();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//资源关闭
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3.方式三:设置不允许自动提交数据

步骤

同方式二,不过在获取连接之后要先调用Connection对象的setAutoCommit()方法将数据库的自动提交功能进行关闭,然后再所有数据写入完成以后再调用Connection对象的commit()方法来将数据进行统一提交

实例:同上

package lib;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class Test2 {
	public static void main(String []args) {
		InputStream is = null;
		Connection connection = null;
		try {
			//获取连接
			is = Test2.class.getClassLoader().getResourceAsStream("test.properties");
			Properties properties=new Properties();
			properties.load(is);
			String user=properties.getProperty("user");
			String password=properties.getProperty("password");
			String url=properties.getProperty("url");
			String driver=properties.getProperty("driver");
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
			//关闭自动提交
			connection.setAutoCommit(false);
			//删除旧表
			String sql="drop table test";
			PreparedStatement ps=connection.prepareStatement(sql);
			ps.execute();
			//创建新表
			sql="create table test (userid int)";
			ps=connection.prepareStatement(sql);
			ps.execute();
			//插入数据
			sql="insert into test values (?)";
			ps=connection.prepareStatement(sql);
			long time=System.currentTimeMillis();
			for(int i=1;i<=1000000;i++) {
				ps.setObject(1, i);
				//攒SQL语句
				ps.addBatch();
				if(i%500==0) {
					//执行SQL语句
					ps.executeBatch();
					//清空SQL语句
					ps.clearBatch();
				}
				//提交数据
				if(i==1000000) {
					connection.commit();
					//恢复自动提交
					connection.setAutoCommit(true);
				}
			}
			System.out.println(System.currentTimeMillis()-time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//资源关闭
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

4.总结

其实方式一前面还有一种方式是使用Statement类来进行批处理,但是由于PreparedStatement会进行预编译SQL语句,之后只要填充占位符即可,不用每次插入数据都进行SQL语句的声明,减少了内存开销和时间花费

在方式二中,使用addBatch()、excuteBatch()、clearBatch()三个方法来对SQL语句进行“攒起来”的操作,攒够一定量后再进行插入,减少了与数据库之间的交互次数,减少了插入的时间,提高效率。

方式三中,在获取连接以后,关闭数据库的自动提交功能,在最后一次添加数据后再将所有数据进行统一提交,减少了数据提交的次数,进而减少提交数据的时间,提高了效率。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐