欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC对数据库进行增删改查操作

程序员文章站 2022-03-02 22:15:20
...

jdbc就是java数据库连接,要想完成对数据库的连接,需要以下七个步骤:
第一步:加载驱动

Class.forName("com.mysql.jdbc.Driver");
         
第二步:创建连接      

connection = getConnection("jdbc:mysql://127.0.0.1:3306/db_ssm?useSSL=true&characterEncoding=utf-8&user=root&password=566666")

````这里的db_ssm为自己的数据库名称,3306为端口,user名字为root,password为mysql的密码。

第三步:写sql

 String sql="select * from tb_user";
 这里是将tb_user表的数据显示出来的sql语句。

 第四步:得到statement对象
 

 statement=connection.prepareStatement(sql);
 
第五步:执行sql 得到结果集
resultSet=statement.executeQuery();

第六步:处理结果集
while(resultSet.next()){

               System.out.println(resultSet.getString(1));
               System.out.println(resultSet.getString(2));
               System.out.println(resultSet.getString(3));
           }
第七步:关闭资源
System.out.println();

这就是jdbc连接数据库的七步骤。
下面开始讲数据库的增删改查。
首先是增加内容,定义一个insert类,然后连接数据库,写SQL语句,步骤和七步骤基本类似,代码如下

public class Insert {
public static void main(String[] args){
PreparedStatement statement=null;
Connection connection =null;

        //2.创建连接
        try {
            connection=DBUtil.getConnection();
            //3.写sql
            String sql="insert into tb_user (id ,name,password)values (?,?,?)";

            statement=connection.prepareStatement(sql);
            statement.setInt(1, 2);
            statement.setString(2,"huangzong");
            statement.setString(3,"1234567");

            statement.executeUpdate();


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

        finally {
            DBUtil.close1(connection,statement);
        }
删除,查看,修改的内容也与之类似,在此就不多赘述了,代码如下:

                    ```删除部分源码
public class Delete {
    public static void  main(String[] args){
        PreparedStatement statement=null;
        Connection connection =null;


        //2.创建连接
        try {
            connection=DBUtil.getConnection();
            //3.写sql
            String sql="delete from tb_user where name = 'luozong'";

            statement=connection.prepareStatement(sql);

            statement.executeUpdate();


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


        finally {
            DBUtil.close1(connection,statement);
        }

                      ```***查看部分源码***
public class test {

    public static void main(String[] args) throws SQLException {
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        Connection connection =null;
       try{


    /*       //2.创建连接
           Class.forName("com.mysql.jdbc.Driver");
            connection = getConnection("jdbc:mysql://127.0.0.1:3306/db_ssm?useSSL=true&characterEncoding=utf-8&user=root&password=566666");*/

           connection=DBUtil.getConnection();
           System.out.println("创建连接成功");
           //3.写sql
           String sql="select * from tb_user";
           //4.得到statement对象
           statement=connection.prepareStatement(sql);
           //5.执行sql 得到结果集
           resultSet=statement.executeQuery();
           //6.处理结果集
           while(resultSet.next()){

               System.out.println(resultSet.getString(1));
               System.out.println(resultSet.getString(2));
               System.out.println(resultSet.getString(3));
           }
           //7.关闭资源
 System.out.println();


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


        finally {
           DBUtil.close(connection,statement,resultSet);
       }

                   ***修改部分源码***
public class Update {
    public static void  main(String[] args){
        PreparedStatement statement=null;
        Connection connection =null;


        //2.创建连接
        try {
            connection=DBUtil.getConnection();
            //3.写sql
            String sql="update tb_user set name='guozong' where id='3'";

            statement=connection.prepareStatement(sql);

            statement.executeUpdate();


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

        finally {
            DBUtil.close1(connection,statement);
        }

```这里用到了DBUtil,可以单独定义一个DBUtil类,在里面定义连接释放等操作,这样可以减小代码的冗杂度,让代码看起来更加清晰简洁。
      后续可以分层将代码更一步优化。



相关标签: jdbc