我的程序是要从从数据库里读数据,并导入另外一个表,现在问题是读出来的时候结果都是一样,没有按记录读出来,是HashMap这个类不能同名关键字吗,请指教!谢谢,在线import java.util.*;
import java.sql.*;
import java.lang.*;
import java.io.*;public class testdb {
public static void main(String[] args) {
try {
String id;
String title;
String version;
String up_time;
String dept;
int file_size;
String file_name;
String pub_sts;
String pub_scope;
String file_lang;
String zhaiyao;
String cate;
String check_sts;
String memo;
String user;
String del_flag;
ArrayList s_list = new ArrayList();
int count = 0;
HashMap map = new HashMap();
HashMap tempMap = new HashMap();
Iterator temp;//////////////////////////////////////////////////////////////////////////
// 数据库连接,并读出数据
//////////////////////////////////////////////////////////////////////////
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=olympic";
String userDb = "sa";
String password = "";
Connection conn = DriverManager.getConnection(url, userDb, password);
Statement stmt_select = conn.createStatement(ResultSet.
TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql_select = "select * from content";
ResultSet rs_select = stmt_select.executeQuery(sql_select);
while (count <= 2197) {
if (rs_select.next()) {
map.put("id", rs_select.getString("datee"));
map.put("title", rs_select.getString("title"));
map.put("version", "V1.0");
map.put("up_time", rs_select.getString("datee"));
map.put("dept", rs_select.getString("dept"));
//file_size = rs.getString("[size]");
map.put("file_name", rs_select.getString("addr"));
map.put("pub_sts", rs_select.getString("status"));
map.put("pub_scope", rs_select.getString("status"));
map.put("file_lang", rs_select.getString("lang"));
map.put("zhaiyao", rs_select.getString("id"));
map.put("cate", "0400");
map.put("check_sts", rs_select.getString("status"));
map.put("memo", rs_select.getString("memo"));
//user = rs.getString();
map.put("del_flag", "0");
//String path="c:\\root\\config.properties";
//OutputStream os=new FileOutputStream(path);
}
s_list.add(count, map);
count++;
}
temp = s_list.iterator();
while (temp.hasNext()) {
tempMap = (HashMap) temp.next();
System.out.println(tempMap.get("title"));
//tempMap.get("title"))这里输出来的是同一个字段,为什么
//tempMap.get("title"))这里输出来的是同一个字段,为什么
} //Iterator temp = s_list.iterator();
//System.out.print(s_list.get(3).toString());
//System.out.print(s_list.get(4).toString());
System.out.print("数据库操作成功,恭喜你");
rs_select.close();
rs_insert.close();
stmt_select.close();
stmt_insert.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.print(e.getMessage().toString());
}
}
}
import java.sql.*;
import java.lang.*;
import java.io.*;public class testdb {
public static void main(String[] args) {
try {
String id;
String title;
String version;
String up_time;
String dept;
int file_size;
String file_name;
String pub_sts;
String pub_scope;
String file_lang;
String zhaiyao;
String cate;
String check_sts;
String memo;
String user;
String del_flag;
ArrayList s_list = new ArrayList();
int count = 0;
HashMap map = new HashMap();
HashMap tempMap = new HashMap();
Iterator temp;//////////////////////////////////////////////////////////////////////////
// 数据库连接,并读出数据
//////////////////////////////////////////////////////////////////////////
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=olympic";
String userDb = "sa";
String password = "";
Connection conn = DriverManager.getConnection(url, userDb, password);
Statement stmt_select = conn.createStatement(ResultSet.
TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql_select = "select * from content";
ResultSet rs_select = stmt_select.executeQuery(sql_select);
while (count <= 2197) {
if (rs_select.next()) {
map.put("id", rs_select.getString("datee"));
map.put("title", rs_select.getString("title"));
map.put("version", "V1.0");
map.put("up_time", rs_select.getString("datee"));
map.put("dept", rs_select.getString("dept"));
//file_size = rs.getString("[size]");
map.put("file_name", rs_select.getString("addr"));
map.put("pub_sts", rs_select.getString("status"));
map.put("pub_scope", rs_select.getString("status"));
map.put("file_lang", rs_select.getString("lang"));
map.put("zhaiyao", rs_select.getString("id"));
map.put("cate", "0400");
map.put("check_sts", rs_select.getString("status"));
map.put("memo", rs_select.getString("memo"));
//user = rs.getString();
map.put("del_flag", "0");
//String path="c:\\root\\config.properties";
//OutputStream os=new FileOutputStream(path);
}
s_list.add(count, map);
count++;
}
temp = s_list.iterator();
while (temp.hasNext()) {
tempMap = (HashMap) temp.next();
System.out.println(tempMap.get("title"));
//tempMap.get("title"))这里输出来的是同一个字段,为什么
//tempMap.get("title"))这里输出来的是同一个字段,为什么
} //Iterator temp = s_list.iterator();
//System.out.print(s_list.get(3).toString());
//System.out.print(s_list.get(4).toString());
System.out.print("数据库操作成功,恭喜你");
rs_select.close();
rs_insert.close();
stmt_select.close();
stmt_insert.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.print(e.getMessage().toString());
}
}
}
Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced. 看看这个就明白了,要是后面插入的值的键与前面的相同,就覆盖前面的。
而且key的不能重复,所以不要用相同的key存不同的value
这里加上map=new HashMap();
你没有new对象,当然只有一个
等 级:
信 誉 值: 59 ---------------(垃圾)这种人,以后帮他了。垃圾。