我正在尝试将数据从 MySQL 数据库读取到数组中,以便可以在 View 中输出数据。
到目前为止我的代码是:
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/database","localhost","root");
PreparedStatement stmt = con.prepareStatement("select * from stock");
ResultSet result = stmt.executeQuery("select * from stock");
ArrayList<int[]> stockNo = new ArrayList<int[]>();
ArrayList<String[]> stockName = new ArrayList<String[]>();
while(result.next()){
stockNo.add(result.getInt(1));
stockName.add(result.getString(2));
}
}
但是我在 .add 方法上收到一条错误消息: “ArrayList 类型中的 add(int,int[]) 方法不适用于争论 (int)”。 有谁知道我该如何解决这个问题?是因为我没有定义数组的大小吗?
最佳答案
这是因为您定义了 List 来存储整数数组,而不是整数。
替换这个:
ArrayList<int[]> stockNo = new ArrayList<int[]>();
通过这个:
ArrayList<Integer> stockNo = new ArrayList<Integer>();
关于java - 从MySQL读取数据到eclipse中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387890/