发表于: 2019-10-13 18:53:14
1 852
今天做的事
师兄检查完我的日报之后
{
(i =<span style="box-sizing: border-box; word-break: break-all; white-space: inherit !important;" span="" <="">; i < count; i++) {
"insert into hero values (null, '%s', %.0f, %d)""盖伦"40s.execute(sql);
<code style='box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13.5px; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; word-break: break-all; white-space: inherit !important;' !important;"="" baseline="" static="" 1em="" visible="" 1.1em="" auto="" none="" 0px="" !important;="" content-box="" black="" 1px="" monospace="" courier,="" new",="" "courier="" mono",="" sans="" vera="" "bitstream="" consolas,="">}
对这段代码提出有没有别的办法的问题
查询之后明白了我用的循环语句插入
还可以用批量插入的方法
这样速度会更快一点
因为可以降低数据库访问
批量插入的方法是
private void initUserInfoTable()
String userInfoTableDdlStr = "create table if not exists `userinfo` (\n"+
" `userId` int(11) DEFAULT NULL,\n" +
" `hero name` varchar(255) DEFAULT NULL,\n" +
" `hp` varchar(255) DEFAULT NULL,\n" +
" mp` varchar(255) DEFAULT NULL,\n" +
" `phone` varchar(255) DEFAULT NULL,\n" +
" `birthDay` bigint(20) DEFAULT NULL,\n" +
" `school` varchar(255) DEFAULT NULL,\n" +
" `hobby` varchar(255) DEFAULT NULL,\n" +
" 攻击力` varchar(255) DEFAULT NULL,\n" +
" `护甲` bigint(20) DEFAULT NULL,\n" +
" `魔抗` varchar(255) DEFAULT NULL,\n" +
" 移动速度` varchar(255) DEFAULT NULL,\n" +
" KEY `index_userid` (`userId`)\n" +
") ENGINE=MyISAM DEFAULT CHARSET=utf8";
尝试用使用execute方式完成分页查询
public class Testc1_1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2j?characterEncoding=UTF-8&serverTimezone=UTC","root", "123456");
Statement s = c.createStatement();) {
// 不同1:execute可以执行查询语句
// 然后通过getResultSet,把结果集取出来
String sqlSelect = "select * from hero";
s.execute(sqlSelect);
ResultSet rs = s.getResultSet();
for(int i=0;rs.next();i++){
if(i%5==0) System.out.println();
System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getFloat("hp")+" "+rs.getInt("damage")+" ");
}
// executeUpdate不能执行查询语句
// s.executeUpdate(sqlSelect);
// 不同2:
// execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等
boolean isSelect = s.execute(sqlSelect);
System.out.println(isSelect);
// executeUpdate返回的是int,表示有多少条数据受到了影响
String sqlUpdate = "update Hero set hp = 300 where id < 100";
int number = s.executeUpdate(sqlUpdate);
System.out.println(number);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
遇到的问题
无
收获
学着思考哪种方式的代码效率最高
明天的机会
继续推进任务
评论