簡體   English   中英

我的插入方法有什么問題?

[英]What is wrong with my insert method?

我試圖將一些數據插入數據庫。 為了實現這一點,我做了一個方法,返回一個boolean並接收一個Point類型的ObjectsArrayList 一切都運行良好,直到我檢查我的表,我發現它只是插入的一個點(每次的第一個點),雖然我有很多點( arrayList.size() > 0 )。 如果我的arrayList.size(2) ,我可以達到例如arrayList.get(2).getXcoordinate() ,但我的方法只插入第一個索引( arrayList.size(0) )。 我根本無法弄清楚我做錯了什么。 任何幫助或提示非常感謝。 謝謝,卡爾 - 我的方法:

public boolean insertPoints(ArrayList<Point> arr) {
       try {
            con = this.getConnection();
            st = con.createStatement();
            if (con != null) {
                for (int i = 0; i < arr.size(); i++) {
                    String id = arr.get(i).getId();
                    String x = arr.get(i).getXcoordinate();
                    String y = arr.get(i).getYcoordinate();
                    if (st.executeUpdate("INSERT INTO table (Id, X, Y)
                                 VALUES (" + id + "," + x + "," + y + ")") > 0)
                        return true;
                    return false;
                }
            } else {
                System.err.println("No connection, con == null ...");
                return false;
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            return false;
        } finally {
            try {
                if (rs != null) rs.close();
                if (st != null) st.close();
                if (con != null) con.close();
            } catch (SQLException sqle) {
                System.err.println("SQLException: " + sqle.getMessage());
            }
        }
         return false;
     }

此代碼不是您應該這樣做的方式。

請使用PreparedStatement綁定變量。

您的ConnectionPreparedStatement不應是類成員。 Connection應該在其他地方實例化並傳入。

PreparedStatement應具有本地范圍。

您應該在finally方法中關閉各個try / catch塊中的資源。 編寫靜態方法以重用並保持代碼較小。

您也可以考慮對這些調用進行批處理,以便在一次往返中插入所有點。 它會更高效。

// Your table's name is table?  Terrible.
private static final String INSERT_SQL = "INSERT INTO table(Id, X, Y) VALUES (?, ?, ?)";

下列:

if (st.executeUpdate("INSERT INTO table (Id, X, Y) VALUES (" + id + "," + x + "," + y + ")") > 0)
    return true;
return false;

將在插入第一個值后退出該方法,根據executeUpdate的返回返回true或false。

也許更喜歡這樣的東西

 for (int i = 0; i < arr.size(); i++) {
      String id = arr.get(i).getId();
      String x = arr.get(i).getXcoordinate();
      String y = arr.get(i).getYcoordinate();
      if (!st.executeUpdate("INSERT INTO table (Id, X, Y) VALUES (" + id + "," + x + "," + y + ")") > 0)
          return false;
 }
 return true;

這是有問題的

if (st.executeUpdate("INSERT INTO table (Id, X, Y)
                                 VALUES (" + id + "," + x + "," + y + ")") > 0)
                        return true;
                    return false;

只需刪除return語句並使用標志或計數器

嘗試這個:

public boolean insertPoints(ArrayList<Point> arr)
    {
        try
        {
            con = this.getConnection();
            st = con.createStatement();
            if (con != null)
            {
                boolean success = true;
                for (int i = 0; i < arr.size(); i++)
                {
                    String id = arr.get(i).getId();
                    String x = arr.get(i).getXcoordinate();
                    String y = arr.get(i).getYcoordinate();
                    if (st.executeUpdate("INSERT INTO table (Id, X, Y) VALUES (" + id + "," + x + "," + y + ")") > 0)
                    {
                        continue;
                    }
                    else
                    {
                        success = false;
                        break;
                    }
                }
                return success;
            }
            else
            {
                System.err.println("No connection, con == null ...");
                return false;
            }
        }
        catch (Exception e)
        {
            System.err.println("Exception: " + e.getMessage());
            return false;
        }
        finally
        {
            try
            {
                if (rs != null) rs.close();
                if (st != null) st.close();
                if (con != null) con.close();
            }
            catch (SQLException sqle)
            {
                System.err.println("SQLException: " + sqle.getMessage());
            }
        }
        return false;
    }

暫無
暫無

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

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