简体   繁体   中英

What is wrong with my insert method?

I am trying to insert some data to a database. To achieve this i made a method that returns a boolean and receives an ArrayList of Objects of type Point . Everything runs fine until i checked my table and i discovered that it is just one point which has been inserted(the first point each time) although i have a plenty of points( arrayList.size() > 0 ). I can reach for example arrayList.get(2).getXcoordinate() if my arrayList.size(2) , but my method insert just the first index( arrayList.size(0) ). I simply can not figure out where i am doing wrong. Any help or hints is very appreciated. Thanks, Carl - My method:

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

This code is not the way you should do this.

Please use PreparedStatement to bind variables.

Your Connection and PreparedStatement should not be class members. The Connection should be instantiated elsewhere and passed in.

The PreparedStatement should have local scope.

You should close resources in individual try/catch blocks in the finally method. Write static methods to reuse and keep the code small.

You might also consider batching these calls so you INSERT all points in a single round trip. It'll be more performant.

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

The following:

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

will exit the method after inserting the first value, returning either true or false based on the return of executeUpdate .

Maybe something more like this instead

 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;

this is problematic

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

just remove both the return statement and use a flag or counter

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM