JDBC学习----08--演示Statement用途(升序降序)

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

/**
 * @author zzw
 * @create 2020/11/14-14:41
 */
public class jdbcTest08 {
    public static void main(String[] args) {
        /*
        我们使用预编译的方式,
        报错 check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1
        这里我们使用了sql语句的拼接,但是由于是编译后才进行的拼接,所以会报错,这个时候我们只能使用Statement类,而不是PrearedStatement。

        **我觉得Statement类就是给了在需要用户点击或者输入后数据发生变化的时候使用
        **PrearedStatement类就是用户在进行登录等关键操作的时候使用?


        //用户在控制台输入desc就是降序,输入asc就是升序
        Scanner s = new Scanner(System.in);
        System.out.println("请输入desc或asc");
        System.out.print("请输入:");
        String keyWords = s.nextLine();

        //jdbc6步走
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1、注册驱动
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            //2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "a");
            //3、获取数据库操作对象
            String sql = "select  语文 from jdbc01 order by 语文 ?";  //?是一个desc或者asc
            ps = conn.prepareStatement(sql);
            //编译之后给占位符赋值
            ps.setString(1, keyWords);
            //4、执行sql
            rs = ps.executeQuery();
            //遍历结果集  rs.next()有数据就进行取值
            while (rs.next()) {
                System.out.println(rs.getString("语文"));
            }


            //5、处理查询结果集
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6、释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

         */


        //我们使用Statement来进行字符串拼接,根据用户输入对数据进行变化
        //用户在控制台输入desc就是降序,输入asc就是升序
        Scanner s = new Scanner(System.in);
        System.out.println("请输入desc或asc");
        System.out.print("请输入:");
        String keyWords = s.nextLine();

        //jdbc6步走
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //1、注册驱动
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            //2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "a");
            //3、获取数据库操作对象
            stmt = conn.createStatement();
            //4、执行sql
            String sql = "select 语文 from jdbc01 order by 语文 " + keyWords;
            rs = stmt.executeQuery(sql);
            //遍历结果集  rs.next()有数据就进行取值
            while (rs.next()) {
                System.out.println(rs.getString("语文"));
            }
            //5、处理查询结果集
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6、释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值