插入数据sql代码,java代码转载自----》》https://blog.csdn.net/jason_m_ho/article/details/82941297,本人亲测运行有效
mysql代码,先创建一个testdb数据库在运行下面sql语句创建tb_data表,需要mysql-connector-java-5.1.21.jar连接数据库的jar包mysql-connector-java
CREATE TABLE `tb_data` (
`id` INT(11) DEFAULT NULL,
`user_name` VARCHAR(100) DEFAULT NULL,
`create_time` DATETIME DEFAULT NULL,
`random` DOUBLE DEFAULT NULL
) ENGINE=MYISAM DEFAULT CHARSET=utf8;
java代码
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDataDemo {
static Connection conn = null;
public static void initConn() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/testdb?"
+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";
try {
// 动态加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动程序");
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String randomStr(int size) {
//定义一个空字符串
String result = "";
for (int i = 0; i < size; ++i) {
//生成一个97~122之间的int类型整数
int intVal = (int) (Math.random() * 26 + 97);
//强制转换(char)intVal 将对应的数值转换为对应的字符,并将字符进行拼接
result = result + (char) intVal;
}
//输出字符串
return result;
}
public static void insert(int insertNum) {
// 开时时间
Long begin = System.currentTimeMillis();
System.out.println("开始插入数据...");
// sql前缀
String prefix = "INSERT INTO tb_data (id, user_name, create_time, random) VALUES ";
try {
// 保存sql后缀
StringBuffer suffix = new StringBuffer();
// 设置事务为非自动提交
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("");
for (int i = 1; i <= insertNum; i++) {
// 构建sql后缀
suffix.append("(" + i +",'"+ randomStr(8) + "', SYSDATE(), " + i * Math.random() + "),");
}
// 构建完整sql
String sql = prefix + suffix.substring(0, suffix.length() - 1);
// 添加执行sql
pst.addBatch(sql);
// 执行操作
pst.executeBatch();
// 提交事务
conn.commit();
// 关闭连接
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 结束时间
Long end = System.currentTimeMillis();
System.out.println("插入"+insertNum+"条数据数据完成!");
System.out.println("耗时 : " + (end - begin) / 1000 + " 秒");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initConn();
insert(1000000);
}
}
## 报错解决方法:
运行有可能会报错,那么是你的sql服务的my.ini文件当中需要设置
[mysqld]
net_buffer_length=512k
max_allowed_packet=500M
下编文字完美解决报错问题转载自https://www.jianshu.com/p/dcf31a642bfa
Error updating database. Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (2798 > 1024). You can change this value on the server by setting the max_allowed_packet’ variable.
大概如上错误:原因是mysql接受service数据包会有限制配置。这里我们修改mysql配置就可以了。
以root用户登录,命令:mysql -uroot -p 回车 输入密码;
输入
show VARIABLES like ‘%max_allowed_packet%’;
可以看到对应的数值
设置20M ,当然这个数值是自己可以设置的。
set global max_allowed_packet = 210241024*10;
成功后退出 :exit;
然后再次进入使用show VARIABLES like ‘%max_allowed_packet%’;就可以看到刚才修改的数值了。
作者:19我想胖
链接:https://www.jianshu.com/p/dcf31a642bfa
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
向mysql插入10万条数据
装载自https://blog.csdn.net/summeranhx/article/details/81583575
创建sheep2的名称数据库
建立sheep_inside_code表
列名为 id int 50
inside_code int 50
java代码
package cn.edu.nwsuaf.sheep2.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
/**
* Created by hx on 2018/8/11.
*/
public class InsertInsideCode {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
final String url = "jdbc:mysql://127.0.0.1/sheep2";
final String name = "com.mysql.jdbc.Driver";
final String user = "root"; // 数据库账户名
final String password = "123456"; // 数据库密码
Connection conn = null;
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
if (conn!=null) {
System.out.println("获取连接成功");
insert(conn);
}else {
System.out.println("获取连接失败");
}
}
public static void insert(Connection conn) {
// 开始时间
Long begin = new Date().getTime();
// sql前缀
String prefix = "INSERT INTO sheep_inside_code (id,inside_code) VALUES ";
try {
// 保存sql后缀
StringBuffer suffix = new StringBuffer();
// 设置事务为非自动提交
conn.setAutoCommit(false);
// 比起st,pst会更好些
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("");//准备执行语句
// 外层循环,总提交事务次数
for (int i = 1; i <= 5; i++) {
suffix = new StringBuffer();
// 第j次提交步长
if(i == 1) {
for (int j = 1; j < 10; j++) {
// 构建SQL后缀
suffix.append("(" + j+","+"'0000" +j+"'),");
}
}else if(i == 2) {
for (int j = 10; j < 100; j++) {
// 构建SQL后缀
suffix.append("(" + j+","+"'000" +j+"'),");
}
}else if(i == 3) {
for (int j = 100; j < 1000; j++) {
// 构建SQL后缀
suffix.append("(" + j+","+"'00" +j+"'),");
}
}else if(i == 4) {
for (int j = 1000; j < 10000; j++) {
// 构建SQL后缀
suffix.append("(" + j+","+"'0" +j+"'),");
}
}else {
for (int j = 10000; j <= 99999; j++) {
// 构建SQL后缀
suffix.append("(" + j+","+"'" +j+"'),");
}
}
// 构建完整SQL
String sql = prefix + suffix.substring(0, suffix.length() - 1);
// 添加执行SQL
pst.addBatch(sql);
// 执行操作
pst.executeBatch();
// 提交事务
conn.commit();
// 清空上一次添加的数据
suffix = new StringBuffer();
}
// 头等连接
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 结束时间
Long end = new Date().getTime();
// 耗时
System.out.println("10万条数据插入花费时间 : " + (end - begin) / 1000 + " s");
System.out.println("插入完成");
}
}``
```java
在这里插入代码片
在eclipse上运行后打开mysql发现只显示1000行数据那是因为*
【已解决】Navicat for mysql只显示1000条记录转载自https://blog.csdn.net/qq_42442369/article/details/85122312
**原因是navicat分页,一页只能显示1000行,超出1000行就在下一页显示,点击右下角的向左向右箭头就可以看到其他部分的数据了。
也可以通过工具–>选项–>数据&网格–>限制记录,来修改默认值。调整一页显示的行数数量。**
这篇文章更加系统介绍了插入数据时允许最大值的问题
链接地址------》》https://www.cnblogs.com/fanwencong/p/5765136.html