新建DBUtil简化之前的增删改查操作

新建一个java class取名(DBUtil)

在这里插入图片描述
DBUtil是如下代码,加入getConnection与closeAll方法简化之前的增删改查操作。

package com.zhongruan;


import java.sql.*;

public class DBUtil {
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
        return connection;
    }
    public static void closeAll(ResultSet resultSets, Statement statement, Connection connection)throws SQLException{
        if (resultSets!=null){
            resultSets.close();
        }
        statement.close();
        connection.close();
    }
}

package com.zhongruan;

import java.sql.*;

public class tianjia {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动

        //2.创建连接
        DBUtil dbUtil=new DBUtil();
        Connection connection=dbUtil.getConnection();
        //3.写sql
        String sql ="insert into stu (name,age) values ('拉拉拉','24')";

        //4.得到statement对象
        PreparedStatement statement=connection.prepareStatement(sql);
        //5.执行sql
        //ResultSet resultSets =
        statement.executeUpdate();
        //6.处理结果集

        //7.关闭资源
       dbUtil.closeAll(null,statement,connection);
    }
}

package com.zhongruan;

import java.sql.*;

public class shanchu {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动


        //2.创建连接
        DBUtil dbUtil=new DBUtil();
        Connection connection=dbUtil.getConnection();
        //3.写sql
        String sql ="delete from stu where id=1";
        //4.得到statement对象
        PreparedStatement statement=connection.prepareStatement(sql);
        //5.执行sql
        //ResultSet resultSets =
        statement.executeUpdate();
        //6.处理结果集

        //7.关闭资源
        dbUtil.closeAll(null,statement,connection);
    }
}

package com.zhongruan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class xiugai {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动

        //2.创建连接
        DBUtil dbUtil=new DBUtil();
        Connection connection=dbUtil.getConnection();
        //3.写sql
        String sql ="update stu set name='嘤嘤怪' where id=5";
        //4.得到statement对象
        PreparedStatement statement=connection.prepareStatement(sql);
        //5.执行sql
        //ResultSet resultSets =
        statement.executeUpdate();
        //6.处理结果集

        //7.关闭资源
        dbUtil.closeAll(null,statement,connection);
    }
}

package com.zhongruan;

import java.sql.*;

public class chaxun {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动

        //2.创建连接
        DBUtil dbUtil=new DBUtil();
        Connection connection=dbUtil.getConnection();
        //3.写sql
        String sql ="select * from stu where age=20";
        //4.得到statement对象
        PreparedStatement statement=connection.prepareStatement(sql);
        //5.执行sql
        ResultSet resultSets = statement.executeQuery();
        //6.处理结果集
        while (resultSets.next()){
            int id=resultSets.getInt(1);
            String name=resultSets.getString(2);
            int age=resultSets.getInt(3);
            System.out.println(id+name+age);
        }
        //7.关闭资源
        dbUtil.closeAll(resultSets,statement,connection);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值