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

      

  • 相关阅读:
    如何让研发团队保持敏捷并不断进步?
    敏捷方法适合什么样的团队?
    规模化敏捷中的“三要”和“三不要”
    敏捷开发中如何使用看板方法创造价值
    4.0 初步预计更新内容
    3.0 环境应用(待更新)
    5.0 Genymotion安装以及基础使用
    2.0 python+appium环境搭建
    1.0 python-client以及ui自动化介绍
    教你一招另辟蹊径抓取美团火锅数据
  • 原文地址:https://www.cnblogs.com/-slz-2/p/15466720.html
Copyright © 2011-2022 走看看