首页 > 解决方案 > 无法弄清楚如何摆脱mysql上的语法错误

问题描述

我正在尝试在 java 主文件上运行一条语句,并且我能够连接到数据库,但不运行 sql 语句我的代码是:

try {
            System.out.println("Connecting to the database...");
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println("Connected to database successfully");
            System.out.println("Inserting");
            stmt=conn.createStatement();
            String sql="USE TheEmployeeDatabase" +
                    "SELECT * FROM EmployeeTable";
            stmt.executeUpdate(sql);
            System.out.println("worked");
        }  catch (SQLException e) {
            e.printStackTrace();
        }

并得到错误

You have an error in your SQL syntax

标签: javamysqldatabaseintellij-idea

解决方案


String sql="USE TheEmployeeDatabase " <--- 你需要空格,否则你会连接 TheEmployeeDatabaseSELECT


推荐阅读