簡體   English   中英

java.sql.SQLSyntaxErrorException:您的 SQL 語法有錯誤; 通過JAVA向SQL數據庫插入數據時

[英]java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; when inserting data into SQL database through JAVA

我正在設計一個 JFrame,我將在其中接受來自 JFileChooser 的名稱、用戶名、密碼、電話、號碼和圖像位置“img”的數據。 不切實際地,我在沒有散列的情況下插入密碼,並且電話號碼列的數據類型為 VarChar(45)。 這應該被忽略,因為我對使用 JAVA 的 SQL 編程還很陌生。

注冊按鈕用於將數據插入到 SQL 行中。 ActionListener 如下:

signupbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try
                {
                    Statement st=conn.createStatement();
                    ResultSet rs=null;
                    String name=namef.getText(),usnm=usnmf.getText(),pswd=pswdf.getText(),ph=phf.getText();
                    String sql="insert into data (name,username,password,ph.no.,profile) values("+name+","+usnm+","+pswd+","+ph+","+img+")";
                    st.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
                    int key = 0;
                    rs = st.getGeneratedKeys();
                    if (rs.next()) 
                    {
                        key = rs.getInt(1);
                    }
                    JOptionPane.showMessageDialog(null, "Key : "+key);
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
            }
        });

當給出數據並單擊按鈕時,會捕獲並打印異常:

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',profile) values(Jyotirmay,Usnm,Pswd,1122334455,E:\1.jpg' at line 1

為什么會出現錯誤? 我嘗試了幾種方法,例如使用PreparedStatement而不是Statement with ? 但仍然收到錯誤。

聚苯乙烯

使用 PreparedStatement:

btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try
                {
                    String sql="INSERT into data (name, username, password, ph.no., profile) VALUES (?,?,?,?,?);";
                    PreparedStatement st=conn.prepareStatement(sql);
                    String name=namef.getText(),usnm=usnmf.getText(),pswd=pswdf.getText(),ph=phf.getText();
                    st.setString(1, name);
                    st.setString(2, usnm);
                    st.setString(3, pswd);
                    st.setString(4, ph);
                    st.setString(5, img);
                    st.executeUpdate();

                    JOptionPane.showMessageDialog(null, "SUCESS! Data inserted. Try Logging In.");

                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
            }
        });

注意:img 已經聲明並初始化。 此外,在控制台中正確打印了 asolute 圖像位置。

您不能在不轉義的情況下在列名中使用點。 最好重命名該列,使其不再使用點,盡管您也可以嘗試在列名中使用反引號,如下所示:

btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try
                {
                    String sql="INSERT into data (name, username, password, `ph.no.`, profile) VALUES (?,?,?,?,?);";
                    PreparedStatement st=conn.prepareStatement(sql);
                    String name=namef.getText(),usnm=usnmf.getText(),pswd=pswdf.getText(),ph=phf.getText();
                    st.setString(1, name);
                    st.setString(2, usnm);
                    st.setString(3, pswd);
                    st.setString(4, ph);
                    st.setString(5, img);
                    st.executeUpdate();

                    JOptionPane.showMessageDialog(null, "SUCESS! Data inserted. Try Logging In.");

                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM