JDBC快速入门

JDBC快速入门

jdbc快速入门步骤:
1.导入jar包
2.注册驱动
3.获取连接对象
4.定义sql
5.获取执行sql对象
6.调用方法执行sql,返回结果处理
7.关闭流

案例:完成增删改查

/**
 * @author: 海康
 * @version: 1.0
 */
public class jdbc1 {
    @Test
    public void testJDBC1() throws ClassNotFoundException, SQLException {
        //1.导入jar包
        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.
                getConnection("jdbc:mysql://localhost:3306/test13", "root", "root");
        //3.定义sql
        String sql = "select * from t_student";
        //4.获取执行sql对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //5.执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        //6.获取结果集
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString(3);
            Timestamp timestamp = resultSet.getTimestamp(4);
            System.out.println("id:"+id+" - name:"+name+" - gender:"+gender+" - :"+timestamp);
        }

        //7.关闭流
        if (resultSet!=null){
            resultSet.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
        if (connection!=null){
            connection.close();
        }
    }

    @Test
    public void testExecuteUpdate() throws ClassNotFoundException, SQLException {
        // 2.注册类信息
        Class.forName("com.mysql.jdbc.Driver");
        //3.获取一个获取对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test13", "root", "root");
        //4.定义sql语句
//        String sql = "insert into t_student(name,gender,birthdary) values('观音','女','2020-11-04')";
//        String sql = "update t_student set gender = '男' where name = '孙悟空'";
        String sql = "delete from t_student where name = '白骨精'";
        //5.获取执行sql对象
        PreparedStatement ps = connection.prepareStatement(sql);
        //6.调用方法执行sql
        int i = ps.executeUpdate();
        System.out.println(i);

        //7.关闭流
        if (ps!=null){
            ps.close();
        }
        if (connection!=null){
            connection.close();
        }
    }
}

案例2:JDBC工具类

/**
 * @author: 海康
 * @version: 1.0
 */

import java.sql.*;

/**
 * 定义一个JDBCUtils工具类
 */
public class JdbcUtil {
    /**
     *  分析:
     *  1.由于每次都对:用户名,密码,url,加载注册驱动
     *  2.每次都需要获取连接对象
     *  3.关闭流
     */
    //1.由于每次都对:用户名,密码,url,加载注册驱动
    private static final String username = "root";
    private static final String password = "root";
    private static final String url = "jdbc:mysql://localhost:3306/test13";

    // 加载注册驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //2.每次都需要获取连接对象
    public static Connection getConnection(){
        Connection connection = null;
        try {
           connection  = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    // 3.关闭流【对增删改操作】
    public static void close(Connection connection, Statement statement){
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

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

    // 3.关闭流【对查询操作】
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
       close(connection,statement);
       if (resultSet!=null){
           try {
               resultSet.close();
           } catch (SQLException throwables) {
               throwables.printStackTrace();
           }
       }
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值