码迷,mamicode.com
首页 > 数据库 > 详细

java基础第21天_mysql

时间:2016-06-24 20:49:33      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:java 基础 mysql

1.实践PreparedStament的CRUD操作。

2.对比Statement和PreparedStatement的大批量操作耗时?(100000记录)


3.事务特点?


4.mysql client如何控制事务?

-------------------------------------------

  1. 实践PreparedStament的CRUD操作。


  2. import java.sql.Connection;

  3. import java.sql.DriverManager;

  4. import java.sql.PreparedStatement;

  5. import java.sql.ResultSet;

  6. import java.sql.ResultSetMetaData;

  7. import java.sql.Statement;


  8. import org.junit.Test;


  9. /**

  10.  * 使用PreparedStatement

  11.  */

  12. public class TestJDBC3 {

  13. @Test

  14. public void insert(){


  15. try {

  16. //1.注册驱动

  17. String driver = "com.mysql.jdbc.Driver" ;

  18. Class.forName(driver);

  19. //2.建立连接

  20. String url = "jdbc:mysql://localhost:3306/mybase" ;

  21. String username = "root" ;

  22. String password = "root" ;

  23. Connection conn = DriverManager.getConnection(url, username, password);

  24. //3.创建语句对象

  25. String sql = "insert into stus(id,name,age) values(?,?,?)" ;

  26. PreparedStatement ppst = conn.prepareStatement(sql);

  27. ppst.setInt(1, 100);

  28. ppst.setString(2, "tomas");

  29. ppst.setInt(3, 23);

  30. ppst.execute();

  31. ppst.close();

  32. conn.close();

  33. }

  34. catch (Exception e) {

  35. e.printStackTrace();

  36. }

  37. }

  38. /**

  39.  * update , id = 1, age = 10

  40.  */

  41. @Test

  42. public void update() {

  43. try {

  44. //1.注册驱动

  45. String driver = "com.mysql.jdbc.Driver" ;

  46. Class.forName(driver);

  47. //2.建立连接

  48. String url = "jdbc:mysql://localhost:3306/mybase" ;

  49. String username = "root" ;

  50. String password = "root" ;

  51. Connection conn = DriverManager.getConnection(url, username, password);

  52. //3.创建语句对象

  53. Statement st = conn.createStatement();

  54. String sql= "update stus set age = ? where id = ?" ;

  55. PreparedStatement ppst = conn.prepareStatement(sql);

  56. ppst.setInt(1, 10);

  57. ppst.setInt(2, 1);

  58. boolean b = ppst.execute();

  59. System.out.println("" + b);

  60. st.close();

  61. conn.close();

  62. }

  63. catch (Exception e) {

  64. e.printStackTrace();

  65. }

  66. }

  67. /**

  68.  * delete , id = 1

  69.  */

  70. @Test

  71. public void delete() {

  72. try {

  73. //1.注册驱动

  74. String driver = "com.mysql.jdbc.Driver" ;

  75. Class.forName(driver);

  76. //2.建立连接

  77. String url = "jdbc:mysql://localhost:3306/mybase" ;

  78. String username = "root" ;

  79. String password = "root" ;

  80. Connection conn = DriverManager.getConnection(url, username, password);

  81. //3.创建语句对象

  82. String sql = "delete from stus where id = ?" ;

  83. PreparedStatement ppst = conn.prepareStatement(sql);

  84. ppst.setInt(1, 1);

  85. boolean b = ppst.execute();

  86. System.out.println("" + b);

  87. conn.close();

  88. }

  89. catch (Exception e) {

  90. e.printStackTrace();

  91. }

  92. }

  93. @Test

  94. public void select() {

  95. try {

  96. //1.注册驱动

  97. String driver = "com.mysql.jdbc.Driver" ;

  98. Class.forName(driver);

  99. //2.建立连接

  100. String url = "jdbc:mysql://localhost:3306/mybase" ;

  101. String username = "root" ;

  102. String password = "root" ;

  103. Connection conn = DriverManager.getConnection(url, username, password);

  104. conn.setAutoCommit(false);

  105. //3.创建语句对象

  106. //select id, name  ,age form ...

  107. String sql = "select * from stus where id > ?" ;

  108. PreparedStatement ppst = conn.prepareStatement(sql);

  109. ppst.setInt(1, 1);

  110. boolean b = ppst.execute();

  111. //判断是否是查询

  112. if(b){

  113. //获取查询结果集

  114. ResultSet rs = ppst.getResultSet();

  115. //移动游标

  116. while(rs.next()){

  117. //int id = rs.getInt("id");

  118. Integer id = (Integer) rs.getObject("id");

  119. String name = rs.getString("name");

  120. Integer age = (Integer) rs.getObject("age");

  121. System.out.println("id=" + id + ",name=" + name + ",age=" + age);

  122. }

  123. //结果集元数据

  124. ResultSetMetaData meta = rs.getMetaData();

  125. for(int i = 0 ; i < meta.getColumnCount() ; i ++){

  126. System.out.print(meta.getColumnLabel(i + 1) + "  ");

  127. }

  128. }

  129. //

  130. //5.释放资源

  131. ppst.close();

  132. conn.close();

  133. }

  134. catch (Exception e) {

  135. e.printStackTrace();

  136. }

  137. }

  138. }

2.对比Statement和PreparedStatement的大批量操作耗时?(100000记录)

//Statement

package com.it18zhang.jdbc;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.Statement;


import org.junit.Test;


public class TestJDBC {

/**

* insert1

*/

@Test

public void insert1(){

try {

//1.注册驱动

String driver = "com.mysql.jdbc.Driver" ;

Class.forName(driver);

//2.建立连接

String url = "jdbc:mysql://localhost:3306/mybase" ;

String username = "root" ;

String password = "root" ;

Connection conn = DriverManager.getConnection(url, username, password);

//设置自动提交模式

conn.setAutoCommit(false);

//3.创建语句对象

Statement st = conn.createStatement();

String sql = "insert into stus(id,name,age) values(9,‘tomson‘,11)" ;

long start = System.currentTimeMillis() ;

//4.执行语句

for(int i = 0 ; i < 100000 ; i ++){

sql = "insert into stus(id,name,age) values("+(i + 1)+",‘tom"+i+"‘,12)" ;

boolean b = st.execute(sql);

}

conn.commit();

System.out.println(System.currentTimeMillis() - start);

//5.释放资源

st.close();

conn.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

//PreparedStatement 程序

package com.it18zhang.jdbc;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;


import org.junit.Test;


/**

 * 使用PreparedStatement

 */

public class TestJDBC3 {

@Test

public void insert(){


try {

//1.注册驱动

String driver = "com.mysql.jdbc.Driver" ;

Class.forName(driver);

//2.建立连接

String url = "jdbc:mysql://localhost:3306/mybase" ;

String username = "root" ;

String password = "root" ;

Connection conn = DriverManager.getConnection(url, username, password);

conn.setAutoCommit(false);

long start  = System.currentTimeMillis();

//3.创建语句对象

String sql = "insert into stus(id,name,age,password) values(?,?,?,?)" ;

PreparedStatement ppst = conn.prepareStatement(sql);

for(int i = 0 ; i < 100000 ; i ++){

ppst.setInt(1, i + 1);

ppst.setString(2, "tomas" + i);

ppst.setInt(3, 10);

ppst.setString(4, "123");

ppst.addBatch();

if(i % 10000 == 0){

ppst.executeBatch();

ppst.clearBatch();

}

}

ppst.executeBatch();

conn.commit();

System.out.println("耗时 : " + (System.currentTimeMillis() - start));

ppst.close();

conn.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

3.事务特点?

原子性(atomicity):组成事务处理的语句形成了一个逻辑单元,不能只执行其中的一部分。 

一致性(consistency):在事务处理执行前后,数据库是一致的(两个账户要么都变,或者都不变)。 

隔离性(isolcation):一个事务处理对另一个事务处理没有影响。 

持续性(durability):事务处理的效果能够被永久保存下来 。

acid

a: atomic,原子性,不可分割。全成功、全失败。

c: consistent,一致性

i: isolate,隔离性

d: durable,永久性。

4.mysql client如何控制事务?

        1.关闭自动提交

                set autocommit=0;

        2.开启事务

                start transaction;

        3.提交事务

                commit;

        4.回滚

                rollback;

本文出自 “作业” 博客,请务必保留此出处http://10718270.blog.51cto.com/10708270/1792593

java基础第21天_mysql

标签:java 基础 mysql

原文地址:http://10718270.blog.51cto.com/10708270/1792593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!