发表于: 2018-03-11 23:43:40
1 723
一。准备小课堂的PPT,讲唯一索引和普通索引的区别。
1.SQL插入索引语句。
普通索引:ALTER TABLE'table-name' ADD INDEX index_name('column')
唯一索引:ALTER TABLE'table-name' ADD UNIQUE('column')
主键索引:ALTER TABLE'table-name' ADD PRIMARY KEY ('column')
..
二。做几个索引出来,顺便检查这段时间看书的成果。比如怎么引用啦,怎么跳转啦,方法啦,对象啦,封装啦。
try catch,先运行try里面的,如果不成功,再运行外面的。
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 {
Statement stat = conn.createStatement();
StringBuilder sb = new StringBuilder();
for(int i=0;i<500;i++) {
sb.append("('idxxx" + i + "'," + (i + 1) + ")");
if(i!=499) {
sb.append(",");
}
}
String sql = "insert into user (id,username) values" + sb.toString();
int num = stat.executeUpdate(sql);
System.out.println(num);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
}
(还没完)
三。复习了一下JDBC连接数据库,发现大部分代码都能看懂了。
public class JDBC {
public static void main(String[] args) {
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "1234";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
if (!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = con.createStatement();
String sql = "select * from student";
ResultSet rs = statement.executeQuery(sql);
System.out.println("--------");
System.out.println("执行结果如下所示:");
System.out.println("----------");
System.out.println("姓名" + " " + "QQ" + " " +"毕业院校");
System.out.println("------------");
String name = null;
String test_qq = null;
String gra_school = null;
while (rs.next()) {
name = rs.getString("name");
test_qq = rs.getString("qq");
gra_school = rs.getString("gra_school");
System.out.println(name + " " + test_qq + " " + gra_school);
}
rs.close();
con.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can't find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("数据库数据成功获取!!");
}
}
}
运行之后,成功。
Succeeded connecting to the Database!
--------
执行结果如下所示:
----------
姓名 QQ 毕业院校
------------
范冰冰 1286443 liuyifei@163.com
王鸥 456528 liudehua@163.com
张庆东 123456 UI
数据库数据成功获取!!
四。实验了一下,插入一百万条数据需要多长时间。然后清空,又实验了下插入一百条数据需要多长时间。
public class Tnsert {
public static void main(String[] args) throws ClassNotFoundException,SQLException {
final String url = "jdbc:mysql://localhost:3306/how2java";
final String name = "com.mysql.jdbc.Driver";
final String user = "root";
final String password = "1234";
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();
String prefix = "insert into hero (name,hp,damage) VALUES ";
try {
StringBuffer suffix = new StringBuffer();
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("");
for (int i =1; i <=10; i++) {
suffix = new StringBuffer();
System.out.println("提交10条");
for(int j = 1; j <= 10;j++) {
suffix.append("("+"'" +i+"英雄"+ "'"+"," + 500 + "," + 40+"),");
}
String sql = prefix + suffix.substring(0,suffix.length() -1);
pst.addBatch(sql);
pst.executeBatch();
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
Long end = new Date().getTime();
System.out.println("100条数据插入花费时间:" +(end - begin) / 10 + " s");
System.out.println("插入完成");
}
}
获取连接成功
提交10条
提交10条
提交10条
提交10条
提交10条
提交10条
提交10条
提交10条
提交10条
提交10条
100条数据插入花费时间:6 s
插入完成
明天的计划:讲小课堂,验证任务
遇到的问题:@Before @After还不太懂,先讲完小课堂,再说
今天的收获:能看懂不少代码了
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论