TestJdbc03.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.lovecoding.jdbc;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import java.sql.*;
  5. import java.util.Scanner;
  6. public class TestJdbc03 {
  7. //连接对象
  8. Connection connection = null;
  9. Statement statement = null;
  10. @Before
  11. public void before() throws ClassNotFoundException, SQLException {
  12. //连接mysql
  13. //1 加载驱动
  14. Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
  15. //2 创建连接 需要指定 端口 地址 用户名 密码
  16. //public static Connection getConnection(String url,String user, String password)
  17. //localhost 数据库地址 3306 端口 lovecoding 数据库 8.0之后serverTimezone=UTC
  18. String url = "jdbc:mysql://localhost:3306/lovecoding?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&rewriteBatchedStatements=true&useSSL=false";
  19. //网络编程
  20. connection = DriverManager.getConnection(url, "root", "root");
  21. statement = connection.createStatement();
  22. }
  23. // sql注入问题 万能密码
  24. // 批处理
  25. // 事务
  26. @Test
  27. public void test01() throws Exception {
  28. // sql拼接
  29. String ename = "李四";
  30. double salary = 15000;//15000
  31. String birthday = "1990-1-1";//1990-1-1
  32. String tel = "13578595685";//13578595685
  33. String email = "'zhangsan,','1'";//'zhangsan,','1'
  34. //sql执行对象
  35. //4 编写sql 添加
  36. // 原因1 需要加单引号 2 hiredate 非空 需要给值
  37. String sql = " INSERT INTO `lovecoding`.`t_employee` ( ename, salary, birthday, tel, email) VALUES ( '"+ename+"' , '"+salary+"' , '"+birthday+"' , '"+tel+"' ,'"+email+"' ); ";
  38. // VALUES ( '李四' , 15000 , '1990-1-1' , '13578595685' ,''zhangsan,','1'' );
  39. int i = statement.executeUpdate(sql);
  40. //6 判断
  41. if (i > 0) {
  42. System.out.println("执行成功");
  43. }else{
  44. System.out.println("执行失败");
  45. }
  46. //关闭资源
  47. statement.close();
  48. connection.close();
  49. }
  50. //SQL拼接
  51. @Test
  52. public void test02() throws Exception {
  53. // sql拼接
  54. String ename = "李四";
  55. double salary = 15000;//15000
  56. String birthday = "1990-1-1";//1990-1-1
  57. String tel = "13578595685";//13578595685
  58. String email = "'zhangsan,','1'";//'zhangsan,','1'
  59. //sql执行对象
  60. //4 编写sql 添加
  61. // 原因1 需要加单引号 2 hiredate 非空 需要给值
  62. String sql = " INSERT INTO `lovecoding`.`t_employee` " +
  63. " ( ename, salary, birthday, tel, email) " +
  64. " VALUES ( ? , ? , ? , ? , ? ); ";
  65. //PreparedStatement 可解决sql拼接问题
  66. //'zhangsan,','1'
  67. // Statement 拼接 sql
  68. // PreparedStatement 解析? setString参数字符串去处理。
  69. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  70. //注入参数 ?
  71. // 索引从 1 开始
  72. preparedStatement.setString(1,ename);
  73. preparedStatement.setDouble(2,salary);
  74. preparedStatement.setString(3,birthday);
  75. preparedStatement.setString(4,tel);
  76. preparedStatement.setString(5,email);
  77. int i = preparedStatement.executeUpdate();
  78. //6 判断
  79. if (i > 0) {
  80. System.out.println("执行成功");
  81. }else{
  82. System.out.println("执行失败");
  83. }
  84. //关闭资源
  85. preparedStatement.close();
  86. connection.close();
  87. }
  88. // 解决
  89. // PreparedStatement
  90. // 设置 参数 替换 拼接的sql
  91. // 参数 用 ? 索引 设置 ? 的值。
  92. //sql注入
  93. //select * from t_employee where eid = 值;
  94. @Test
  95. public void test03() throws Exception {
  96. String eid = "35 or 1=1"; // 35 or 1=1 万能密码 select where name=? ,password = ? or 1=1
  97. // " select * from t_employee where eid = 35 or 1=1 "
  98. String sql = " select * from t_employee where eid = "+ eid;
  99. //执行sql
  100. ResultSet rs = statement.executeQuery(sql);
  101. //遍历
  102. while (rs.next()){
  103. String id = rs.getString(1);
  104. String ename = rs.getString(2);
  105. System.out.println(id + "-" +ename);
  106. }
  107. rs.close();
  108. statement.close();
  109. connection.close();
  110. }
  111. @Test
  112. public void test04() throws Exception {
  113. String eid = "a34or 1=1";
  114. // " select * from t_employee where eid = '35 or 1=1' "
  115. String sql = " select * from t_employee where eid = ? "; //34or 1=1 转换数字 数字直接转 34
  116. //执行sql
  117. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  118. //preparedStatement设置值
  119. preparedStatement.setString(1,eid);
  120. //执行查询
  121. ResultSet rs = preparedStatement.executeQuery();
  122. //遍历
  123. while (rs.next()){
  124. String id = rs.getString(1);
  125. String ename = rs.getString(2);
  126. System.out.println(id + "-" +ename);
  127. }
  128. rs.close();
  129. statement.close();
  130. connection.close();
  131. }
  132. //获取自增长键值
  133. @Test
  134. public void test05() throws Exception {
  135. // sql拼接
  136. String ename = "李四1";
  137. double salary = 15000;//15000
  138. String birthday = "1990-1-1";//1990-1-1
  139. String tel = "13578595685";//13578595685
  140. String email = "'zhangsan,','1'";//'zhangsan,','1'
  141. //sql执行对象
  142. //4 编写sql 添加
  143. // 原因1 需要加单引号 2 hiredate 非空 需要给值
  144. String sql = " INSERT INTO `lovecoding`.`t_employee` " +
  145. " ( ename, salary, birthday, tel, email) " +
  146. " VALUES ( ? , ? , ? , ? , ? ); ";
  147. //PreparedStatement
  148. //获取自增主键参数 Statement.RETURN_GENERATED_KEYS
  149. //getGeneratedKeys();
  150. PreparedStatement preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
  151. //注入参数
  152. preparedStatement.setString(1,ename);
  153. preparedStatement.setDouble(2,salary);
  154. preparedStatement.setString(3,birthday);
  155. preparedStatement.setString(4,tel);
  156. preparedStatement.setString(5,email);
  157. int i = preparedStatement.executeUpdate();
  158. //自增主键
  159. ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
  160. while (generatedKeys.next()){
  161. Object id = generatedKeys.getObject(1);
  162. System.out.println(id);
  163. }
  164. //6 判断
  165. if (i > 0) {
  166. System.out.println("执行成功");
  167. }else{
  168. System.out.println("执行失败");
  169. }
  170. //关闭资源
  171. preparedStatement.close();
  172. connection.close();
  173. }
  174. //通过 preparedStatement 完成 添加或者修改。
  175. //批处理(了解)
  176. @Test
  177. public void test06() throws Exception {
  178. //添加1000记录
  179. long l1 = System.currentTimeMillis();
  180. // sql拼接
  181. String ename = "测试用户";
  182. double salary = 15000;//15000
  183. String birthday = "1990-1-1";//1990-1-1
  184. String tel = "13578595685";//13578595685
  185. String email = "'zhangsan,','1'";//'zhangsan,','1'
  186. //sql执行对象
  187. //4 编写sql 添加
  188. // 原因1 需要加单引号 2 hiredate 非空 需要给值
  189. String sql = " INSERT INTO `lovecoding`.`t_employee` " +
  190. " ( ename, salary, birthday, tel, email) " +
  191. " VALUES ( ? , ? , ? , ? , ? ); ";
  192. PreparedStatement preparedStatement = null;
  193. for (int i = 0; i < 1000; i++) {
  194. preparedStatement = connection.prepareStatement(sql);
  195. //注入参数
  196. preparedStatement.setString(1,ename+i);
  197. preparedStatement.setDouble(2,salary);
  198. preparedStatement.setString(3,birthday);
  199. preparedStatement.setString(4,tel);
  200. preparedStatement.setString(5,email);
  201. //执行
  202. preparedStatement.executeUpdate();
  203. }
  204. long l2 = System.currentTimeMillis();
  205. System.out.println(l2-l1); //736
  206. //关闭资源
  207. preparedStatement.close();
  208. connection.close();
  209. }
  210. //连接参数 在url后面再加一个参数 rewriteBatchedStatements=true
  211. //添加到批处理 addBatch()
  212. // INSERT INTO 表名 values(值0,值1)
  213. // 分号 去掉;
  214. @Test
  215. public void test07() throws Exception {
  216. //添加1000记录
  217. long l1 = System.currentTimeMillis();
  218. // sql拼接
  219. String dname = "批处理部门";
  220. String desc = "部门介绍";
  221. //sql执行对象
  222. //4 编写sql 添加
  223. String sql = " INSERT INTO `lovecoding`.`t_department` VALUES ( null , ? , ? ) ";
  224. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  225. for (int i = 0; i < 1000; i++) {
  226. //注入参数
  227. preparedStatement.setObject(1,dname+i);
  228. preparedStatement.setObject(2,desc);
  229. //添加
  230. preparedStatement.addBatch();
  231. }
  232. //整体执行
  233. preparedStatement.executeBatch();
  234. long l2 = System.currentTimeMillis();
  235. System.out.println(l2-l1); // 31
  236. //关闭资源
  237. preparedStatement.close();
  238. connection.close();
  239. }
  240. //事务
  241. /*
  242. update t_department set description = 'xx' where did = 2;
  243. update t_department set description = 'yy' where did = 3;
  244. 故意把其中一条sql语句写错。
  245. update t_department set description = 'xx' where did = 2;
  246. update t_department set description = 'yy' what did = 3; #what是错误的
  247. */
  248. @Test
  249. public void test08() throws Exception {
  250. //sql对象
  251. Statement statement = connection.createStatement();
  252. //转账
  253. String sql1 = " update t_department set dname = 'aa' where did = 2 ";
  254. String sql2 = " update t_department set dname = 'aa' where did = 3 ";
  255. //手动事务
  256. connection.setAutoCommit(false);
  257. try {
  258. statement.executeUpdate(sql1);
  259. statement.executeUpdate(sql2);
  260. //正常提交代码
  261. connection.commit();
  262. } catch (SQLException e) {
  263. //回滚
  264. connection.rollback();
  265. e.printStackTrace();
  266. }
  267. //释放资源
  268. statement.close();
  269. connection.close();
  270. }
  271. }