|
@@ -1,318 +0,0 @@
|
|
-package com.lovecoding.jdbc;
|
|
|
|
-
|
|
|
|
-import org.junit.Before;
|
|
|
|
-import org.junit.Test;
|
|
|
|
-
|
|
|
|
-import java.sql.*;
|
|
|
|
-import java.util.Scanner;
|
|
|
|
-
|
|
|
|
-public class TestJdbc03 {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Connection connection = null;
|
|
|
|
-
|
|
|
|
- Statement statement = null;
|
|
|
|
-
|
|
|
|
- @Before
|
|
|
|
- public void before() throws ClassNotFoundException, SQLException {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String url = "jdbc:mysql://localhost:3306/lovecoding?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&rewriteBatchedStatements=true&useSSL=false";
|
|
|
|
-
|
|
|
|
- connection = DriverManager.getConnection(url, "root", "root");
|
|
|
|
-
|
|
|
|
- statement = connection.createStatement();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test01() throws Exception {
|
|
|
|
-
|
|
|
|
- String ename = "李四";
|
|
|
|
- double salary = 15000;
|
|
|
|
- String birthday = "1990-1-1";
|
|
|
|
- String tel = "13578595685";
|
|
|
|
- String email = "'zhangsan,','1'";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String sql = " INSERT INTO `lovecoding`.`t_employee` ( ename, salary, birthday, tel, email) VALUES ( '"+ename+"' , '"+salary+"' , '"+birthday+"' , '"+tel+"' ,'"+email+"' ); ";
|
|
|
|
-
|
|
|
|
- int i = statement.executeUpdate(sql);
|
|
|
|
-
|
|
|
|
- if (i > 0) {
|
|
|
|
- System.out.println("执行成功");
|
|
|
|
- }else{
|
|
|
|
- System.out.println("执行失败");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- statement.close();
|
|
|
|
- connection.close();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test02() throws Exception {
|
|
|
|
-
|
|
|
|
- String ename = "李四";
|
|
|
|
- double salary = 15000;
|
|
|
|
- String birthday = "1990-1-1";
|
|
|
|
- String tel = "13578595685";
|
|
|
|
- String email = "'zhangsan,','1'";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String sql = " INSERT INTO `lovecoding`.`t_employee` " +
|
|
|
|
- " ( ename, salary, birthday, tel, email) " +
|
|
|
|
- " VALUES ( ? , ? , ? , ? , ? ); ";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- preparedStatement.setString(1,ename);
|
|
|
|
- preparedStatement.setDouble(2,salary);
|
|
|
|
- preparedStatement.setString(3,birthday);
|
|
|
|
- preparedStatement.setString(4,tel);
|
|
|
|
- preparedStatement.setString(5,email);
|
|
|
|
-
|
|
|
|
- int i = preparedStatement.executeUpdate();
|
|
|
|
-
|
|
|
|
- if (i > 0) {
|
|
|
|
- System.out.println("执行成功");
|
|
|
|
- }else{
|
|
|
|
- System.out.println("执行失败");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- preparedStatement.close();
|
|
|
|
- connection.close();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test03() throws Exception {
|
|
|
|
- String eid = "35 or 1=1";
|
|
|
|
-
|
|
|
|
- String sql = " select * from t_employee where eid = "+ eid;
|
|
|
|
-
|
|
|
|
- ResultSet rs = statement.executeQuery(sql);
|
|
|
|
-
|
|
|
|
- while (rs.next()){
|
|
|
|
- String id = rs.getString(1);
|
|
|
|
- String ename = rs.getString(2);
|
|
|
|
-
|
|
|
|
- System.out.println(id + "-" +ename);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- rs.close();
|
|
|
|
- statement.close();
|
|
|
|
- connection.close();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test04() throws Exception {
|
|
|
|
- String eid = "a34or 1=1";
|
|
|
|
-
|
|
|
|
- String sql = " select * from t_employee where eid = ? ";
|
|
|
|
-
|
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- preparedStatement.setString(1,eid);
|
|
|
|
-
|
|
|
|
- ResultSet rs = preparedStatement.executeQuery();
|
|
|
|
-
|
|
|
|
- while (rs.next()){
|
|
|
|
- String id = rs.getString(1);
|
|
|
|
- String ename = rs.getString(2);
|
|
|
|
-
|
|
|
|
- System.out.println(id + "-" +ename);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- rs.close();
|
|
|
|
- statement.close();
|
|
|
|
- connection.close();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test05() throws Exception {
|
|
|
|
-
|
|
|
|
- String ename = "李四1";
|
|
|
|
- double salary = 15000;
|
|
|
|
- String birthday = "1990-1-1";
|
|
|
|
- String tel = "13578595685";
|
|
|
|
- String email = "'zhangsan,','1'";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String sql = " INSERT INTO `lovecoding`.`t_employee` " +
|
|
|
|
- " ( ename, salary, birthday, tel, email) " +
|
|
|
|
- " VALUES ( ? , ? , ? , ? , ? ); ";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
|
|
|
|
-
|
|
|
|
- preparedStatement.setString(1,ename);
|
|
|
|
- preparedStatement.setDouble(2,salary);
|
|
|
|
- preparedStatement.setString(3,birthday);
|
|
|
|
- preparedStatement.setString(4,tel);
|
|
|
|
- preparedStatement.setString(5,email);
|
|
|
|
-
|
|
|
|
- int i = preparedStatement.executeUpdate();
|
|
|
|
-
|
|
|
|
- ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
|
|
|
|
- while (generatedKeys.next()){
|
|
|
|
- Object id = generatedKeys.getObject(1);
|
|
|
|
- System.out.println(id);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- if (i > 0) {
|
|
|
|
- System.out.println("执行成功");
|
|
|
|
- }else{
|
|
|
|
- System.out.println("执行失败");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- preparedStatement.close();
|
|
|
|
- connection.close();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test06() throws Exception {
|
|
|
|
-
|
|
|
|
- long l1 = System.currentTimeMillis();
|
|
|
|
-
|
|
|
|
- String ename = "测试用户";
|
|
|
|
- double salary = 15000;
|
|
|
|
- String birthday = "1990-1-1";
|
|
|
|
- String tel = "13578595685";
|
|
|
|
- String email = "'zhangsan,','1'";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String sql = " INSERT INTO `lovecoding`.`t_employee` " +
|
|
|
|
- " ( ename, salary, birthday, tel, email) " +
|
|
|
|
- " VALUES ( ? , ? , ? , ? , ? ); ";
|
|
|
|
-
|
|
|
|
- PreparedStatement preparedStatement = null;
|
|
|
|
- for (int i = 0; i < 1000; i++) {
|
|
|
|
-
|
|
|
|
- preparedStatement = connection.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
- preparedStatement.setString(1,ename+i);
|
|
|
|
- preparedStatement.setDouble(2,salary);
|
|
|
|
- preparedStatement.setString(3,birthday);
|
|
|
|
- preparedStatement.setString(4,tel);
|
|
|
|
- preparedStatement.setString(5,email);
|
|
|
|
-
|
|
|
|
- preparedStatement.executeUpdate();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- long l2 = System.currentTimeMillis();
|
|
|
|
- System.out.println(l2-l1);
|
|
|
|
-
|
|
|
|
- preparedStatement.close();
|
|
|
|
- connection.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void test07() throws Exception {
|
|
|
|
-
|
|
|
|
- long l1 = System.currentTimeMillis();
|
|
|
|
-
|
|
|
|
- String dname = "批处理部门";
|
|
|
|
- String desc = "部门介绍";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- String sql = " INSERT INTO `lovecoding`.`t_department` VALUES ( null , ? , ? ) ";
|
|
|
|
-
|
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
- for (int i = 0; i < 1000; i++) {
|
|
|
|
-
|
|
|
|
- preparedStatement.setObject(1,dname+i);
|
|
|
|
- preparedStatement.setObject(2,desc);
|
|
|
|
-
|
|
|
|
- preparedStatement.addBatch();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- preparedStatement.executeBatch();
|
|
|
|
-
|
|
|
|
- long l2 = System.currentTimeMillis();
|
|
|
|
- System.out.println(l2-l1);
|
|
|
|
-
|
|
|
|
- preparedStatement.close();
|
|
|
|
- connection.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- update t_department set description = 'xx' where did = 2;
|
|
|
|
- update t_department set description = 'yy' where did = 3;
|
|
|
|
-
|
|
|
|
- 故意把其中一条sql语句写错。
|
|
|
|
-
|
|
|
|
- update t_department set description = 'xx' where did = 2;
|
|
|
|
- update t_department set description = 'yy' what did = 3; #what是错误的
|
|
|
|
- */
|
|
|
|
- @Test
|
|
|
|
- public void test08() throws Exception {
|
|
|
|
-
|
|
|
|
- Statement statement = connection.createStatement();
|
|
|
|
-
|
|
|
|
- String sql1 = " update t_department set dname = 'aa' where did = 2 ";
|
|
|
|
- String sql2 = " update t_department set dname = 'aa' where did = 3 ";
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- connection.setAutoCommit(false);
|
|
|
|
- try {
|
|
|
|
- statement.executeUpdate(sql1);
|
|
|
|
- statement.executeUpdate(sql2);
|
|
|
|
-
|
|
|
|
- connection.commit();
|
|
|
|
- } catch (SQLException e) {
|
|
|
|
-
|
|
|
|
- connection.rollback();
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- statement.close();
|
|
|
|
- connection.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-}
|
|
|