发表于: 2018-03-12 23:55:38
1 451
public class Boker {
private Connection conn;
@Before
public void setUp() throws Exception {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");
}
@After
public void teraDown() throws Exception {
conn.close();
}
@Test
public void testInsert() throws SQLException {
this.insertS();
}
private void insertS() {
try {
Long begin = new Date().getTime();
System.out.println(begin);
Statement stat = conn.createStatement();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500000; i++) {
sb.append("('idxxx" + i + "'," + (i + 1) + ")");
if (i != 499999) {
sb.append(",");
}
}
String sql = "insert into user (id,username) values" + sb.toString();
int num = stat.executeUpdate(sql);
System.out.println(num);
Long end = new Date().getTime();
System.out.println(end);
System.out.println("插入五十万条数据花费时间:" + (end - begin) + " ms");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void testSearch() throws SQLException {
this.searchS();
}
private void searchS() throws SQLException {
Statement stat = conn.createStatement();
String sql = "select * from user where id = 'idxxx9910'";
Long begin = new Date().getTime();
System.out.println(begin);
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id") + "\t" + rs.getInt("username") + "\t");
Long end = new Date().getTime();
System.out.println(end);
System.out.println("查询一条数据花费时间:" + (end - begin) + " ms");
}
}
@Test
public void testDelete() {
this.deleteS();
}
private void deleteS() {
try {
Statement stat = conn.createStatement();
String sql = "delete from user";
System.out.println(stat.executeUpdate(sql));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
报错:
java.sql.SQLException: Incorrect integer value: 'idxxx0' for column 'id' at row 1
原因是因为我把idxxx输入数据库,但是user表的设计id的字符类型是int,才11位。后来改成了varchar,报错解决。
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (11277826 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable
场景:当向mysql数据库中插入数据大于1m的数据时,提示该异常
原因:mysql插入数据的最大允许默认值为1048576(1M)
这个值得系统参数:max_allowed_packet
查询方法:show VARIABLES like '%max_allowed_packet%';
解决方法:
1.在MySQL安装目录下的my.ini文件中的[mysqld]段中的找到"max_allowed_packet = 1M",修改为自己想要的值
(如果没有这行内容,自己添加上去),保存,重起MySQL服务。现在可以load大于1M的文件了。
2.
使用命令“SET GLOBAL max_allowed_packet=32*1024*1024”
或:set @@max_allowed_packet=8*1024*1024
没有索引,插入时间是5.8秒
加索引
有了索引之后,添加数据,时间变长了。
二。加入索引之后,查询数据的时间是:
1520873597018
idxxx9910 9911
1520873597021
查询一条数据花费时间:3 ms
取消索引之后,查询用时是:
1520873699123
idxxx9910 9911
1520873699125
查询一条数据花费时间:2 ms
为什么没有索引,反倒用时更少了呢?应该更多才对?莫非是电脑没反映过来?
三。mysql命令行里,用SQL语句操作,添加各种索引
添加主键索引,为什么报错
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user' ADD PRIMARY KEY ('column')' at line 1
错误原因是以为column应该用我的列明替换,比如id
继续报错:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user'ADD PRIMARY KEY ('id')' at line 1
我把Navicat的主键去掉,然后再运行这句话。
OK,OK成功了。
明天的计划:小课堂因为重新排序,所以要明天讲,验证任务
遇到的问题:暂无。
今天的收获:能看懂不少代码了
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论