如果我通过phpMyAdmin执行查询,则它在我的Java程序中不存在时仍然有效

时间:2019-07-20 09:39:26

标签: java mysql sql mariadb

我是Java的新用户,要进行考试,我必须制作一个用于书店的应用程序,但是我的应用程序的数据库部分存在问题:如果我复制从它生成的查询,并将其粘贴到phpMyAdmin中,它运行良好。

应用程序给我的错误是:

  

java.sql.SQLSyntaxErrorException:您的SQL错误   句法;检查与您的MariaDB服务器相对应的手册   在'SET @address =附近使用的正确语法的版本   LAST_INSERT_ID();插入utenti(电子邮件,密码,nome,cogn'位于   第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时本身就可以正常工作

1 个答案:

答案 0 :(得分:0)

您实际上正在尝试使用一条准备好的语句执行两个查询。像下面那样更改您的方法

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