Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的插入方法有什么问题?_Java_Android_Arraylist - Fatal编程技术网

Java 我的插入方法有什么问题?

Java 我的插入方法有什么问题?,java,android,arraylist,Java,Android,Arraylist,我正在尝试向数据库中插入一些数据。为了实现这一点,我创建了一个方法,该方法返回一个布尔值,并接收类型为点的对象的数组列表。一切都正常,直到我检查了我的表,发现它只插入了一个点(每次都是第一个点),尽管我有很多点(arrayList.size()>0)。例如,如果我的arrayList.size(2),我可以访问arrayList.get(2).getXcoordinate(),但我的方法只插入第一个索引(arrayList.size(0))。 我根本不知道我哪里做错了。非常感谢您的任何帮助或提示

我正在尝试向数据库中插入一些数据。为了实现这一点,我创建了一个方法,该方法返回一个
布尔值
,并接收类型为
对象的
数组列表
。一切都正常,直到我检查了我的表,发现它只插入了一个点(每次都是第一个点),尽管我有很多点(
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;
     }
公共布尔插入点(ArrayList arr){
试一试{
con=this.getConnection();
st=con.createStatement();
如果(con!=null){
对于(int i=0;i0)
返回true;
返回false;
}
}否则{
System.err.println(“无连接,con==null…”);
返回false;
}
}捕获(例外e){
System.err.println(“异常:+e.getMessage());
返回false;
}最后{
试一试{
如果(rs!=null)rs.close();
如果(st!=null)st.close();
如果(con!=null)con.close();
}捕获(SQLException sqle){
System.err.println(“SQLException:+sqle.getMessage());
}
}
返回false;
}
以下内容:

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;
for(int i=0;i
这是有问题的

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

只需删除return语句并使用标志或计数器

这段代码不应该这样做

请使用
PreparedStatement
绑定变量

您的
连接
PreparedStatement
不应是类成员。连接应该在别处实例化并传入

PreparedStatement
应具有本地范围

您应该在finally方法中关闭各个try/catch块中的资源。编写静态方法以重用代码并保持代码的小型化

你也可以考虑分批调用,这样你就可以把所有的点插入到一个往返的行程中。这样会更有效

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

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;
    }
公共布尔插入点(ArrayList arr)
{
尝试
{
con=this.getConnection();
st=con.createStatement();
如果(con!=null)
{
布尔成功=真;
对于(int i=0;i
为什么不使用类型
void
的方法呢?这很聪明。谢谢你的帮助。我也要感谢大家。