我正在尝试迭代我的HashMap并将值保存在MySQL表中。 HashMap包含60个条目,但它只在MySQL表中存储26个条目。我打印了我的HashMap并确认它有60个条目,但在表中插入时只有有限数量(26)的条目被存储,这很奇怪。
public HashMap<String, Product> inserintosql() throws IOException
{
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost:8889/assign";
String USER = "root";
String PASS = "root";
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try
{
saxParser = saxParserFactory.newSAXParser();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
MyHandler handler = new MyHandler();
try
{
saxParser.parse(new File("/usr/local/apache-tomcat-9.0.0.M26/webapps/csj/WEB-INF/ProductCatalog.xml"), handler);
}
catch (SAXException e)
{
e.printStackTrace();
}
HashMap<String,Product> productList = handler.getEmpList();
try
{
//conn.setAutoCommit(false);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
size=productList.size();
String query = " insert into products (prodtype, itemId, imgname, itemName, prodCondition, retailerName, price)" + " values (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(query);
final int batchSize = 60;
int count = 0;
for (String key : productList.keySet() )
{
Product con= productList.get(key);
pst.setString(1,con.getProducttype());
pst.setString(2,con.getId());
pst.setString(3,con.getImage());
pst.setString(4,con.getName());
pst.setString(5,con.getCondition());
pst.setString(6,con.getRetailer());
pst.setDouble(7,con.getPrice());
pst.addBatch();
if (++count == batchSize)
{
pst.executeBatch();
}
}
pst.executeBatch();
}
catch(SQLException se)
{
}
catch(Exception e)
{
}
return productList;
}
在上面的代码中,所有产品数据都存储在XML文件中,该文件使用SAX解析器进行解析并存储在HashMap的“productList”中。我正在迭代hashMap并将数据插入到MySQL表中,但是60个中只有26个条目存储在MySQL表中。
MySQL表格结构
MySQL table structure where hashmap data is being stored
MySQL表的快照显示hashmap迭代数据。
Snapshot of MySQL table display 26 entries received from the hashmap
请你帮我把整个hashmap插入表格或品脱点为什么我在错误的轨道上。