zoukankan      html  css  js  c++  java
  • 使用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();
                    }
                }
            }
        }
    
    }
    

      

  • 相关阅读:
    SSH 连接超时解决办法
    alter system switch logfile和alter system archive log current 的区别
    Oracle 删除归档日志脚本
    Oracle 11g 新特性简介
    Oracle sqlplus 常用命令总结
    Oracle 11g 新特性简介
    计算文件的MD5值上传到服务器 下载验证文件是否被篡改
    看不下去的代码放这有机会用手机看。。。
    C++ Primer第一章学习笔记——C++初邂逅
    OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui
  • 原文地址:https://www.cnblogs.com/-slz-2/p/15466720.html
Copyright © 2011-2022 走看看