Java 为什么putHashmap没有';不显示系列数据吗?

Java 为什么putHashmap没有';不显示系列数据吗?,java,mysql,hashmap,Java,Mysql,Hashmap,我必须制作一个程序,从excel文件中读取数据,并将这些数据加载到数据库的表中。表格将具有excel文件的名称,表格的字段将是excel文件第一行中的数据。我已经编写了读取excel文件的代码,并用我想要的名称创建了表。我还设法将数据存储在表中。但是对于这两个操作,对于创建字段和在表中存储数据,我都使用Hashmap。但我在表格中得到的结果与excel文件中的结果顺序不同 例如,我的excel文件中的数据是: ID姓名工资 5克里斯汀2349000 6波琳娜1000 7劳拉12587458 8

我必须制作一个程序,从excel文件中读取数据,并将这些数据加载到数据库的表中。表格将具有excel文件的名称,表格的字段将是excel文件第一行中的数据。我已经编写了读取excel文件的代码,并用我想要的名称创建了表。我还设法将数据存储在表中。但是对于这两个操作,对于创建字段和在表中存储数据,我都使用Hashmap。但我在表格中得到的结果与excel文件中的结果顺序不同

例如,我的excel文件中的数据是:

ID姓名工资

5克里斯汀2349000

6波琳娜1000

7劳拉12587458

8 efi 34567

43吉姆45878

但当我运行我的程序时,我在我的基础上得到的是:

姓名ID工资

克里斯汀2349005

波琳娜61000

等等,数据弄混了

我的程序如下。谁能帮我解释一下为什么我的结果会出现这种情况? 先谢谢你

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class readexcel {
    static HashMap<String, Integer> tFields = new HashMap();
    static HashMap[] tData;

    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/kainourgia", "root", "root");
        Statement stmt = con.createStatement();
        // String filename = "C:\\Users\\Efi\\Documents\\test6.xls";
        String fullPath = "C:\\Users\\Efi\\Documents\\test9.xls";
        String Path = "C:\\Users\\Efi\\Documents\\";
        String filename = "test9.xml";
        String[] parts = filename.split("\\.");
        String tablename = parts[0];

        //Create an ArrayList to store the data read from excel sheet.
        List sheetData = new ArrayList();
        FileInputStream fis = null;
        try {
            //Create a FileInputStream that will be use to read the
            // excel file.
            fis = new FileInputStream(fullPath);
            //Create an excel workbook from the file system
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            //Get the first sheet on the workbook.
            HSSFSheet sheet = workbook.getSheetAt(0);

            //store the data read on an ArrayList so that we can printed the
            // content of the excel to the console.
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }
                sheetData.add(data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        showExcelData(sheetData);
        tFields = parseExcelColumnTitles(sheetData);
        String str = getCreateTable(con, tablename, tFields);
        tData = parseExcelColumnData(sheetData);
        fillTable(con, tablename, tData);
    }

    // Iterates the data and print it out to the console.
    private static void showExcelData(List sheetData) {
        // HashMap<String, String> tableFields = new HashMap();
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(cell.getNumericCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(cell.getRichStringCellValue());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(cell.getBooleanCellValue());
                }
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }
    }

    @SuppressWarnings( { "unchecked", "unused" })
    private static HashMap parseExcelColumnTitles(List sheetData) {
        // εδώ διαβάζω μόνο την γραμμή 0 κάθε φύλλου για να πάρω τους τίτλους
        // των πεδίων

        List list = (List) sheetData.get(0);
        HashMap<String, Integer> tableFields = new HashMap(list.size());
        for (int j = 0; j < list.size(); j++) {
            Cell cell = (Cell) list.get(j);
            tableFields.put(cell.getStringCellValue(), cell.getCellType());
        }

        return tableFields;

    }

    @SuppressWarnings( { "unchecked", "unused" })
    private static HashMap[] parseExcelColumnData(List sheetData) {
        // εδω πρέπει να μπει μια επανάληψη, από την γραμμή 1 μέχρι την
        // τελευταία γραμμή του κάθε φύλλου
        HashMap[] tousRows = new HashMap[sheetData.size() - 1];
        for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) {

            List list = (List) sheetData.get(rowCounter);

            HashMap<String, Integer> tableFields = new HashMap(list.size());
            String str;
            String[] tousFields = new String[list.size()];
            int i = 0;

            for (int j = 0; j < list.size(); j++) {
                Cell cell = (Cell) list.get(j);
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        tableFields.put(String.valueOf(cell
                                .getNumericCellValue()), cell.getCellType());
                    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        tableFields.put(cell.getStringCellValue(), cell
                                .getCellType());
                    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        tableFields.put(String.valueOf(cell
                                .getBooleanCellValue()), cell.getCellType());
                    }
                }

            }
            tousRows[rowCounter - 1] = tableFields;
        }

        return tousRows;

    }

    private static String getCreateTable(Connection con, String tablename,
            HashMap<String, Integer> tableFields) {
        Iterator iter = tableFields.keySet().iterator();
        Iterator cells = tableFields.keySet().iterator();
        String str = "";
        String[] allFields = new String[tableFields.size()];
        int i = 0;
        while (iter.hasNext()) {
            String fieldName = (String) iter.next();
            Integer fieldType = (Integer) tableFields.get(fieldName);

            switch (fieldType) {
            case Cell.CELL_TYPE_NUMERIC:
                str = fieldName + " INTEGER";
                break;
            case Cell.CELL_TYPE_STRING:
                str = fieldName + " VARCHAR(255)";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                str = fieldName + " INTEGER";
                break;
            }
            allFields[i++] = str;
        }
        try {
            Statement stmt = con.createStatement();
            // try
            // {
            // System.out.println( "Use the database..." );
            // stmt.executeUpdate( "USE kainourgia;" );
            // }
            // catch( SQLException e )
            // {
            // System.out.println( "SQLException: " + e.getMessage() );
            // System.out.println( "SQLState: " + e.getSQLState() );
            // System.out.println( "VendorError: " + e.getErrorCode() );
            // }
            try {
                String all = org.apache.commons.lang3.StringUtils.join(
                        allFields, ",");
                String createTableStr = "CREATE TABLE IF NOT EXISTS "
                        + tablename + " (" + all + ")";

                System.out.println("Create a new table in the database");
                stmt.executeUpdate(createTableStr);
            } catch (SQLException e) {
                System.out.println("SQLException: " + e.getMessage());
                System.out.println("SQLState:     " + e.getSQLState());
                System.out.println("VendorError:  " + e.getErrorCode());
            }
        } catch (Exception e) {
        }
        return str;
    }

    private static void fillTable(Connection con, String fieldname,
            HashMap[] tableData) {
        for (int row = 0; row < tableData.length; row++) {
            HashMap<String, Integer> rowData = tableData[row];
            Iterator iter = rowData.entrySet().iterator();
            String str;
            String[] tousFields = new String[rowData.size()];
            int i = 0;
            while (iter.hasNext()) {
                Map.Entry pairs = (Map.Entry) iter.next();
                Integer fieldType = (Integer) pairs.getValue();
                String fieldValue = (String) pairs.getKey();
                switch (fieldType) {
                case Cell.CELL_TYPE_NUMERIC:
                    str = fieldValue;
                    break;
                case Cell.CELL_TYPE_STRING:
                    str = "\'" + fieldValue + "\'";
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    str = fieldValue;
                    break;
                default:
                    str = "";
                    break;
                }
                tousFields[i++] = str;
            }

            try {
                Statement stmt = con.createStatement();
                System.out.println("Use the table");
                String all = org.apache.commons.lang3.StringUtils.join(
                        tousFields, ",");
                String sql = "INSERT INTO " + fieldname + " VALUES (" + all
                        + ")";
                stmt.executeUpdate(sql);
            } catch (SQLException e) {
                System.out.println("SQLException: " + e.getMessage());
                System.out.println("SQLState: " + e.getSQLState());
                System.out.println("VendorError: " + e.getErrorCode());
            }

        }

        // return str;
    }

    private Statement createStatement() {
        return null;
    }

}
import java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Iterator;
导入java.util.List;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.commons.lang3.StringUtils;
导入org.apache.poi.ss.usermodel.Cell;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.SQLException;
导入java.sql.Statement;
公共类readexcel{
静态HashMap tFields=newhashmap();
静态HashMap[]tData;
公共静态void main(字符串[]args)引发异常{
Class.forName(“com.mysql.jdbc.Driver”);
连接con=(连接)DriverManager.getConnection(
“jdbc:mysql://localhost:3306/kainourgia“,”根“,”根“);
语句stmt=con.createStatement();
//String filename=“C:\\Users\\Efi\\Documents\\test6.xls”;
String fullPath=“C:\\Users\\Efi\\Documents\\test9.xls”;
String Path=“C:\\Users\\Efi\\Documents\\”;
字符串filename=“test9.xml”;
String[]parts=filename.split(“\\”);
字符串tablename=parts[0];
//创建ArrayList以存储从excel工作表读取的数据。
List sheetData=new ArrayList();
FileInputStream fis=null;
试一试{
//创建一个FileInputStream,用于读取
//excel文件。
fis=新文件输入流(完整路径);
//从文件系统创建excel工作簿
HSSF工作手册=新的HSSF工作手册(fis);
//获取工作簿上的第一张工作表。
HSSFSheet sheet=工作簿。getSheetAt(0);
//将读取的数据存储在ArrayList上,以便打印
//将excel的内容发送到控制台。
迭代器行=sheet.rowIterator();
while(rows.hasNext()){
HSSFRow行=(HSSFRow)行。下一步();
迭代器单元格=行。单元格迭代器();
列表数据=新的ArrayList();
while(cells.hasNext()){
HSSFCell单元格=(HSSFCell)单元格。下一步();
添加数据(单元格);
}
sheetData.add(数据);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(fis!=null){
fis.close();
}
}
showceldata(sheetData);
t字段=列标题(sheetData);
字符串str=getCreateTable(con,tablename,tFields);
tData=解析数据(sheetData);
fillTable(con、tablename、tData);
}
//迭代数据并将其打印到控制台。
专用静态void showExcelData(列表数据){
//HashMap tableFields=新HashMap();
对于(int i=0;i