使用PreparedStatement异常及必须使用Statement案例(sql连接)

package com.bjpowernode.jdbc;

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

/**
 * @Author:杨青
 * @Time:2021/10/26 16:34
 * @Description:
 *      使用PreparedStatement:
 *      运行出现异常:check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc''
 *                  sql语句进行连接时,会补上‘’单引号,引发异常
 */
public class JDBCTest08 {
    public static void main(String[] args) {
        /*
        //用户在控制台输入desc就是降序,输入asc就是升序
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入desc/asc;desc表示降序,asc表示升序:");
        System.out.print("请输入:");
        String keyWords=scanner.nextLine();
        //执行SQL
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123456");
            //3.获取预编译数据库操作对象
            String sql="select *from emp order by ename ?";
            ps=conn.prepareStatement(sql);
            //给占位符?传值
            ps.setString(1,keyWords);
            //4.执行sql语句
            rs=ps.executeQuery();
            //5.处理查询结果集
            while (rs.next()){
                System.out.println(rs.getString("ename"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        */
        //用户在控制台输入desc就是降序,输入asc就是升序
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入desc/asc;desc表示降序,asc表示升序:");
        System.out.print("请输入:");
        String keyWords=scanner.nextLine();
        //执行SQL
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","123456");
            //3.获取数据库操作对象
            stmt=conn.createStatement();
            //4.执行sql语句
            String sql="select *from emp order by ename "+keyWords; //字符串拼接
            rs=stmt.executeQuery(sql);
            //5.处理查询结果集
            while (rs.next()){
                System.out.println(rs.getString("ename"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

}

  

原文地址:https://www.cnblogs.com/-slz-2/p/15466720.html