jdbc 08: statement应用场景

2年前 (2022) 程序员胖胖胖虎阿
206 0 0

jdbc连接mysql,statement的应用场景

package com.examples.jdbc.o8_statement应用场景;

import java.sql.*;
import java.util.ResourceBundle;
import java.util.Scanner;

/*
    普通数据库操作对象的应用场景:
    根据用户输入的:desc或者asc将查询结果集按照指定顺序输出
 */
public class Test {
    //静态代码块完成资源绑定器对配置文件属性的绑定
    public static void main(String[] args) {
         //commonStatement();
        preparedStatement();
    }

    /**
     * 预处理数据库操作对象
     */
    private static void preparedStatement(){

        //获取用户输入的desc 或者 asc
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入desc 或者 asc,desc代表降序排列,asc代表升序排列");
        System.out.println("请输入:");
        String orderType = scanner.nextLine();

        //资源绑定器绑定配置属性文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String userName = resourceBundle.getString("userName");
        String passWord = resourceBundle.getString("passWord");

        //3个资源对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        //jdbc数据库连接6步骤

        try {
            //1.
            Class.forName(driver);

            //2.
            connection = DriverManager.getConnection(url, userName, passWord);

            //3.

            //String sql = "select * from tb_user order by uname ?";
            //preparedStatement = connection.prepareStatement(sql);
            //preparedStatement.setString(1, orderType);

            //4.
            resultSet = preparedStatement.executeQuery();

            //5.
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String uname = resultSet.getString("uname");
                String upasswd = resultSet.getString("upasswd");
                System.out.println("id: " + id + " uname: " + uname + " upasswd: " + upasswd);
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            //6.
            if(resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 普通数据库操作对象的应用场景
     */
    private static void commonStatement() {

        //获取用户输入的desc 或者 asc
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入desc 或者 asc,desc代表降序排列,asc代表升序排列");
        System.out.println("请输入:");
        String orderType = scanner.nextLine();

        //资源绑定器绑定配置属性文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String userName = resourceBundle.getString("userName");
        String passWord = resourceBundle.getString("passWord");

        //3个资源对象
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        //jdbc数据库连接6步骤

        try {
            //1.
            Class.forName(driver);

            //2.
            connection = DriverManager.getConnection(url, userName, passWord);

            //3.
            statement = connection.createStatement();

            //4.
            String sql = "select * from tb_user order by upasswd " + orderType;
            resultSet = statement.executeQuery(sql);

            //5.
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String uname = resultSet.getString("uname");
                String upasswd = resultSet.getString("upasswd");
                System.out.println("id: " + id + " uname: " + uname + " upasswd: " + upasswd);
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            //6.
            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();
                }
            }
        }
    }
}
版权声明:程序员胖胖胖虎阿 发表于 2022年10月28日 上午8:08。
转载请注明:jdbc 08: statement应用场景 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...