当前位置:   article > 正文

Java连接数据库中的MysqlUtil.java增删改查详解_java mysqlutil

java mysqlutil

目录

举一个查询类GetCount.java

 增删改查的MysqlUtil.java


四个类

流程

举一个查询类GetCount.java

这个是连接数据库的代码 (需要改成自己的)

  1. static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
  2. static String user = "账号";
  3. static String password = "密码";
  1. package com.sql;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import com.mysql.jdbc.Connection;
  5. import com.mysql.jdbc.Statement;
  6. public class GetCount {
  7. static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
  8. static String user = "账号";
  9. static String password = "密码";
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. getCount();
  13. }
  14. public static void getCount() {
  15. int count = 0;
  16. try {
  17. // 1. 加载驱动
  18. Class.forName("com.mysql.jdbc.Driver");
  19. // 2.创建链接
  20. Connection connection = (Connection) DriverManager.getConnection(url,user,password);
  21. // 3.创建Statement对象
  22. Statement statement = (Statement) connection.createStatement();
  23. // 4.执行sql
  24. String sql = "select * from student";
  25. ResultSet resultSet = statement.executeQuery(sql);
  26. // 5. 结果
  27. while (resultSet.next()) {
  28. count += 1;
  29. }
  30. if (resultSet !=null) {
  31. resultSet.close();
  32. }
  33. if (statement !=null) {
  34. statement.close();
  35. }
  36. if (connection !=null) {
  37. connection.close();
  38. }
  39. } catch (Exception e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. System.out.println(count);
  44. }
  45. }

七条数据:

 增删改查的MysqlUtil.java

 

  1. package com.sql;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import com.mysql.jdbc.Connection;
  5. import com.mysql.jdbc.Statement;
  6. public class MysqlUtil {
  7. static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
  8. static String user = "账号";
  9. static String password = "密码";
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. delete();
  13. }
  14. /*数据查询
  15. *
  16. *
  17. */
  18. public static void search() {
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. // 2.创建链接
  22. Connection connection = (Connection) DriverManager.getConnection(url,user,password);
  23. // 3.创建Statement对象
  24. Statement statement = (Statement) connection.createStatement();
  25. // 4.执行sql
  26. String sql = "select * from student";
  27. ResultSet resultSet = statement.executeQuery(sql);
  28. while(resultSet.next()) {
  29. String id = resultSet.getString("id");
  30. String name = resultSet.getString("name");
  31. String idCard = resultSet.getString("idCard");
  32. String phone = resultSet.getString("phone");
  33. System.out.println(id + " " + name + " " + idCard + " " + phone);
  34. }
  35. if(resultSet!=null) {
  36. resultSet.close();
  37. }
  38. if (statement !=null) {
  39. statement.close();
  40. }
  41. if (connection !=null) {
  42. connection.close();
  43. }
  44. } catch (Exception e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. /*数据插入
  50. *
  51. *
  52. *
  53. * */
  54. private static int insert() {
  55. // TODO Auto-generated method stub
  56. int i =0;
  57. try {
  58. Class.forName("com.mysql.jdbc.Driver");
  59. Connection connection =(Connection) DriverManager.getConnection(url,user,password);
  60. // 创建Statement对象
  61. Statement statement =(Statement) connection.createStatement();
  62. String sql ="insert into student(name,idCard,phone,height) values('苹果','12346','123456','184')";
  63. statement.executeUpdate(sql);
  64. if (statement !=null) {
  65. statement.close();
  66. }
  67. if (connection !=null) {
  68. connection.close();
  69. }
  70. i=1;
  71. } catch (Exception e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. return 1;
  76. }
  77. public static int update() {
  78. int i = 0;
  79. try {
  80. Class.forName("com.mysql.jdbc.Driver");
  81. Connection connection = (Connection) DriverManager.getConnection(url,user,password);
  82. // 3.创建Statement对象
  83. Statement statement = (Statement) connection.createStatement();
  84. String sql = "update student set name = '香蕉' where id = 1" ;
  85. statement.executeUpdate(sql);
  86. if (statement !=null) {
  87. statement.close();
  88. }
  89. if (connection !=null) {
  90. connection.close();
  91. }
  92. i = 1;
  93. } catch (Exception e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }
  97. return i;
  98. }
  99. public static int delete() {
  100. int i=0;
  101. try {
  102. Class.forName("com.mysql.jdbc.Driver");
  103. Connection connection = (Connection) DriverManager.getConnection(url,user,password);
  104. // 3.创建Statement对象
  105. Statement statement = (Statement) connection.createStatement();
  106. String sql = "delete from student where id = 1" ;
  107. statement.executeUpdate(sql);
  108. if (statement !=null) {
  109. statement.close();
  110. }
  111. if (connection !=null) {
  112. connection.close();
  113. }
  114. i = 1;
  115. } catch (Exception e) {
  116. // TODO Auto-generated catch block
  117. e.printStackTrace();
  118. }
  119. return i ;
  120. }
  121. }

在main()中调用

查询:调用search()函数;

添加:调用insert()函数;

更改:调用update()函数;

删除:调用delete()函数;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/1006290
推荐阅读
相关标签
  

闽ICP备14008679号