当前位置:   article > 正文

利用JDBC与进行数据库连接及简单的增删改查_使用jdbc连接数据库,并进行基本的操作(如增删改查)jsp代码

使用jdbc连接数据库,并进行基本的操作(如增删改查)jsp代码

今天学习了使用JDBC对数据库进行操作,现总结如下
一、七个步骤
1.加载数据库驱动到JVM中

   Class.forName("com.mysql.jdbc.Driver");//forName()方法可将指定字符串的类加载到JVM中
  • 1

2.建立数据库连接
首先要构建数据库连接的URL以及数据库的用户名和密码。‘

String url = "jdbc:mysql://localhost:3306/std";//3306为端口号,std用自己数据库名字代替
            String username = "root";
            String password = "123456";
  • 1
  • 2
  • 3

使用DriverMager对象的 getConnection()方法创建Connection对象

Connection connection1 = DriverManager.getConnection(url, username, password);
  • 1

3.创建数据库作对象
在JDBC的API中封装了Statement的扩展PreparedStaement,应用于其中的SQL语句,可用占位符"?'来代替参数。

PreparedStatement statement = null;
  • 1

4.定义SQL语句
定义增删改查等SQL语句用于数据库操作

  String sq3 = "delete from user where id=4";
  • 1

5.执行数据库操作

  statement = connection1.prepareStatement(sq3);
  • 1

6获取操作结果集
ResultSet接口封装了数据查询的结果集,针对Java中数据提供了一套get***()方法,来获取每一行的数据。注意对于delete,update,insert 操作没有Resultset对象返回。select操作会返回Resultset对象。

 resultSet=statement.executeQuery();
  • 1

7关闭数据库连接
在对数据库的操作结束后一定要及时关闭数据库资源,关闭时,对Resultset,Connection,PrepareStaement对象都要关闭。

 if(resultSet!=null) {
              try {
                  resultSet.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
          if(statement!=null) {
              try {
                  statement.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
          if(connection!=null) {
              try {
                  connection.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

增删改查操作完整代码如下:

public class DBop {
    //连接数据库
    public static Connection getconnect() {
        Connection connection = null;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/std?useSSL=true&" +
                    "characterEncoding=utf-8&user=root&password=123456");
            System.out.println("成功");
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //查询数据库
    public static void Selectdb() {
        String sql = "select* from user";
        Connection connection = getconnect();
        PreparedStatement statement=null;
        try {
            statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getString(2));
                System.out.println(resultSet.getString(3));

            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

    //增加数据
    public static void insertdb() {
        String sq2 = "insert into user values(?,?,?)";
        Connection connection = getconnect();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sq2);
            statement.setString(1, "6p");
            statement.setString(2, "183");
           statement.setString(3, "2");

            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //删除数据
    public static void deletedb() {
        PreparedStatement statement=null;
        String sq3 = "delete from user where id=2";
        Connection connection = getconnect();
        try {

            statement = connection.prepareStatement(sq3);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //更新数据
    public static void updatedb() {
        String sq4 = "update score set grade=200 where id=6";
        Connection connection = getconnect();
        PreparedStatement statement;
        try {
            statement = connection.prepareStatement(sq4);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //关闭数据库}
  public    static void closeconnection(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println("成功关闭");
        }
    }




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113

总结:JDBC操作要求我们牢记七个步骤并,值得注意的是一些数据库异常的处理,定义Connection,PreparedStamanet,等对象时最好在try语句之前,以免出现变量未定义的提示,此外,根据封装的思想,可将连接,关闭等数据库操作封装为函数,方便其他类调用。

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

闽ICP备14008679号