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();
           }
       }
    }

}

你可能感兴趣的:(javaweb,java,sqlserver)