我用java连接数据库并将数据库中的两列数据存储成数组的形式调用
结果在循环的时候只是一直在最后一个数据里循环,请问这是为什么?
代码如下
List<HashStore> hashStores = new ArrayList();
HashStore hashStore = new HashStore();
// List<String> HC = new ArrayList<String>();
// List<String> ID = new ArrayList<String>();
// public static void main(String[] args)
{
try
{ Connection conn = null;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/pic_manager?user=root&password=123&useUnicode=true&characterEncoding=gb2312";
conn= DriverManager.getConnection(url);
// System.out.println("连接一次,连接成功!");
Statement stat = (Statement) conn.createStatement();
ResultSet rs =(ResultSet) stat.executeQuery("select * from pictures ");
while (rs.next()){
String a =rs.getString(1);
String d= rs.getString(10);
hashStore.setNum(a);
System.out.println(a);
hashStore.setValue(d);
// System.out.println(d);
hashStores.add(hashStore);
}
rs.close();
stat.close();
conn.close();
conn = null;
}
这个是连接数据库,并封装数组的代码
conn k = new conn();
List<HashStore> hashCodes = k.aaa();
String sourceHashCode = "80f008f83b08cff0";
List<Integer> differences = new ArrayList<Integer>();
for (int i = 1; i < hashCodes.size(); i++)
{
int difference = hammingDistance(sourceHashCode, hashCodes.get(i).getValue());
differences.add(difference);
System.out.println("一共有:" +hashCodes.size());
System.out.println("这张图片ID 是:" +hashCodes.get(i).getNum());
System.out.println("这张图片ID 是:" +i);
if(difference==0 ){
System.out.println("这两张图片相同!");
}else if (difference<=5 ){
System.out.println("这两张图片相似!");
}else if (difference<=10 ){
System.out.println("这两张图片类似!");
}else {
System.out.println("这两张图片不相似!");
}
System.out.println(difference);//显示对比结果的值
}