无法弄清楚如何摆脱mysql上的语法错误

时间:2019-04-16 21:54:41

标签: java mysql database intellij-idea

我正在尝试在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

2 个答案:

答案 0 :(得分:0)

字符串sql =“ USE TheEmployeeDatabase” <---您在此处需要空格,否则将串联TheEmployeeDatabaseSELECT

答案 1 :(得分:0)

As long as the schema name TheEmployeeDatabase1 is part of your DB_URL, you don't need to have the USE TheEmployeeDatabase statement.

You statement should just be:

SELECT * FROM EmployeeTable