【JDBC-Mysql】使用JDBC操作Mysql数据库

朴灿烈づ我的快乐病毒、 2023-10-08 20:35 148阅读 0赞

【JDBC-Mysql】使用JDBC操作Mysql数据库

  • 1)导入依赖
  • 2)定义Connection连接类
  • 3)使用JDBC进行Mysql数据库操作
    • 3.1.写入
    • 3.2.删除
    • 3.3.修改
    • 3.4.查询

1)导入依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>5.7.27</version>
  5. </dependency>

2)定义Connection连接类

  1. public class Conn {
  2. //定义Driver
  3. public static final String driver = "com.mysql.jdbc.Driver";
  4. //定义getConnection方法获取Connection
  5. public static Connection getConnection(String url, String username, String password) {
  6. Connection conn = null;
  7. try {
  8. //注册驱动
  9. Class.forName(driver);
  10. //获取Connection
  11. conn = DriverManager.getConnection(url, username, password);
  12. } catch (ClassNotFoundException e) {
  13. e.printStackTrace();
  14. } catch (SQLException e) {
  15. e.printStackTrace();
  16. }
  17. return conn;
  18. }
  19. //关闭连接
  20. public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
  21. if (conn != null) {
  22. try {
  23. conn.close();
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. if (ps != null) {
  29. try {
  30. ps.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. if (rs != null) {
  36. try {
  37. rs.close();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. //关闭连接
  44. public static void close(Connection conn, PreparedStatement ps) {
  45. if (conn != null) {
  46. try {
  47. conn.close();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. if (ps != null) {
  53. try {
  54. ps.close();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. }

3)使用JDBC进行Mysql数据库操作

3.1.写入

  1. public class Test {
  2. private static final String tmpUrl = "jdbc:mysql://192.168.1.1:3306/test?characterEncoding=utf-8";
  3. private static final String tmpUsername = "root";
  4. private static final String tmpPassword = "123456";
  5. private static Connection conn;
  6. private static PreparedStatement ps;
  7. private static ResultSet rs;
  8. public static void main(String[] args) {
  9. conn = Conn.getConnection(tmpUrl, tmpUsername, tmpPassword);
  10. try {
  11. //编辑sql,返回PreparedStatement状态
  12. ps = conn.prepareStatement("insert into `table` values(1234, '张三')");
  13. //执行写入数据的操作
  14. rs = ps.executeUpdate();
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. } finally {
  18. //释放资源
  19. Conn.close(conn, ps, rs);
  20. }
  21. }
  22. }

3.2.删除

  1. public class Test {
  2. private static final String tmpUrl = "jdbc:mysql://192.168.1.1:3306/test?characterEncoding=utf-8";
  3. private static final String tmpUsername = "root";
  4. private static final String tmpPassword = "123456";
  5. private static Connection conn;
  6. private static PreparedStatement ps;
  7. private static ResultSet rs;
  8. public static void main(String[] args) {
  9. conn = Conn.getConnection(tmpUrl, tmpUsername, tmpPassword);
  10. try {
  11. //编辑sql,返回PreparedStatement状态
  12. ps = conn.prepareStatement("delete from `table` where id = ?");
  13. //定义参数(?按照顺序进行参数配置,如下是删除id为1234的数据)
  14. ps.setInt(1,1234)
  15. //执行删除数据的操作
  16. rs = ps.executeUpdate();
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. } finally {
  20. //释放资源
  21. Conn.close(conn, ps, rs);
  22. }
  23. }
  24. }

3.3.修改

  1. public class Test {
  2. private static final String tmpUrl = "jdbc:mysql://192.168.1.1:3306/test?characterEncoding=utf-8";
  3. private static final String tmpUsername = "root";
  4. private static final String tmpPassword = "123456";
  5. private static Connection conn;
  6. private static PreparedStatement ps;
  7. private static ResultSet rs;
  8. public static void main(String[] args) {
  9. conn = Conn.getConnection(tmpUrl, tmpUsername, tmpPassword);
  10. try {
  11. //编辑sql,返回PreparedStatement状态
  12. ps = conn.prepareStatement("update `table` set name = '张三' where id = ?");
  13. //定义参数(?按照顺序进行参数配置,如下是修改id为1234的数据)
  14. ps.setInt(1,1234)
  15. //执行修改数据的操作
  16. rs = ps.executeUpdate();
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. } finally {
  20. //释放资源
  21. Conn.close(conn, ps, rs);
  22. }
  23. }
  24. }

3.4.查询

  1. public class Test {
  2. private static final String tmpUrl = "jdbc:mysql://192.168.1.1:3306/test?characterEncoding=utf-8";
  3. private static final String tmpUsername = "root";
  4. private static final String tmpPassword = "123456";
  5. private static Connection conn;
  6. private static PreparedStatement ps;
  7. private static ResultSet rs;
  8. public static void main(String[] args) {
  9. conn = Conn.getConnection(tmpUrl, tmpUsername, tmpPassword);
  10. try {
  11. //编辑sql,返回PreparedStatement状态
  12. ps = conn.prepareStatement("SELECT `id`, `name` FROM `table`");
  13. //返回ResultSet结果集
  14. rs = ps.executeQuery();
  15. //遍历结果集
  16. while (rs.next()) {
  17. //获取每条数据的id,name(两种写法都可以)
  18. //int id = rs.getInt("id");
  19. int id = rs.getInt(1);
  20. //String name = rs.getString("name");
  21. String name = rs.getString(2);
  22. }
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. } finally {
  26. //释放资源
  27. Conn.close(conn, ps, rs);
  28. }
  29. }
  30. }

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用原生JDBC操作数据库

    > 现在都是使用mybatis来操作数据库,至于如何使用JDBC来操作数据库,基本上忘记的差不多了,特此记录一下 > 感情深不深 ,都在代码里,废话不多说,直接上代码