【问题标题】:If I execute my query through phpMyAdmin it works while it doesn't in my java program如果我通过 phpMyAdmin 执行我的查询,它可以工作,而它不在我的 java 程序中
【发布时间】:2019-07-20 09:39:26
【问题描述】:

我是 java 新手,为了考试,我必须制作一个应用程序,它是书店的商店,但我在应用程序的数据库部分遇到问题:我准备好的语句在 if 时不起作用我复制从中生成的查询并将其粘贴到 phpMyAdmin 中,它可以完美运行。

应用程序给我的错误是:

java.sql.SQLSyntaxErrorException:您的 SQL 中有错误 句法;检查与您的 MariaDB 服务器相对应的手册 在 'SET @address = 附近使用正确语法的版本 LAST_INSERT_ID(); INSERT INTO utenti (email, password, nome, cogn' at 第 1 行

产生错误的方法

public static void createUser(String email, String password, String nome, String cognome, long telefono, String indirizzo, int cap, String città) throws Exception{
        Connection conn = MysqlConnection.getConnection();

        try{
            st = conn.prepareStatement("INSERT INTO indirizzi (indirizzo, cap, città) "
                    + "VALUES (?,?,?); "
                    + "SET @address = LAST_INSERT_ID(); "
                    + "INSERT INTO utenti (email, password, nome, cognome, telefono, idindirizzo) "
                    + "VALUES (?, ?, ?, ?, ?, @address);");
            st.setString(1, indirizzo);
            st.setInt(2, cap);
            st.setString(3, città);
            st.setString(4, email);
            st.setString(5, password);
            st.setString(6, nome);
            st.setString(7, cognome);
            st.setLong(8, telefono);

            System.out.println(st);

            st.executeUpdate();
        }
        catch(Exception e){
            throw e;
        }
        finally{
            try{
                conn.close();
            }
            catch(SQLException e){
                throw e;
            }
        }
    }

使用它的方法(只是一种测试方法,看看另一个是否有效)

    private static void createUser()
    {
        try {
            ModelUser.createUser("a@a.com", "qwertyasdfg", "Aldo", "Simone", 391234567890l, "via dietro 1/g", 37051, "Sì");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

生成的sql

INSERT INTO indirizzi (indirizzo, cap, città) VALUES ('via dietro 1/g',37051,'Sì'); SET @address = LAST_INSERT_ID(); INSERT INTO utenti (email, password, nome, cognome, telefono, idindirizzo) VALUES ('a@a.com', 'qwertyasdfg', 'Aldo', 'Simone', 391234567890, @address);

为了可读性做了一点编辑

INSERT INTO indirizzi (indirizzo, cap, città)
VALUES ('via dietro 1/g',37051,'Sì');
SET @address = LAST_INSERT_ID();
INSERT INTO utenti (email, password, nome, cognome, telefono, idindirizzo)
VALUES ('a@a.com', 'qwertyasdfg', 'Aldo', 'Simone', 391234567890, @address);

我认为这应该可以工作,因为查询本身在放入 phpMyAdmin 时正在工作

【问题讨论】:

  • 检查stackoverflow.com/questions/10797794/… ...如果您想使用多个查询,您需要正确配置您的jdbc驱动程序。
  • 非常感谢!我整个上午都在寻找答案,但找不到。它马上就奏效了!
  • 但是您应该考虑每个语句只使用一个查询,因为这通常更具可读性。 (我认为您应该能够在第二个查询中直接调用 LAST_INSERT_ID() 而不是使用 @address)。
  • 我认为 LAST_INSERT_ID() 会输出“utenti”的最后一个 id。我也会试试的!非常感谢!

标签: java mysql sql mariadb


【解决方案1】:

您实际上是在尝试使用一个准备好的语句执行两个查询。改变你的方法如下

public static void createUser(String email, String password,
          String nome, String cognome, long telefono, String indirizzo,
          int cap, String città) throws Exception{

    Connection conn = MysqlConnection.getConnection();

    try{
        PreparedStatement st = conn.prepareStatement("INSERT INTO indirizzi
                (indirizzo, cap, città) VALUES (?,?,?)");
        st.setString(1, indirizzo);
        st.setInt(2, cap);
        st.setString(3, città);
        st.executeUpdate();

        /* You may want to close PreparedStatement here */

        st = conn.prepareStatement("INSERT INTO utenti
              (email, password, nome, cognome, telefono, idindirizzo)
              VALUES (?, ?, ?, ?, ?, LAST_INSERT_ID())");        
        st.setString(1, email);
        st.setString(2, password);
        st.setString(3, nome);
        st.setString(4, cognome);
        st.setLong(5, telefono);
        st.executeUpdate();
         /* You may want to close PreparedStatement here */
    }catch(Exception e){
        throw e;
    }finally{
        try{
            conn.close();
        }
        catch(SQLException e){
            throw e;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多