导入Excel工作表数据并将其插入到java中的mysql数据库中

时间:2013-11-25 06:55:18

标签: java mysql sql excel apache

我有一个代码,它将输入作为excel文件,并在mysql数据库中创建具有相同名称的表,但我无法将值插入到数据库中。 期间A B总体而言 2008 70 60 65 2009 50 60 55 2010 80 70 75 2011 90 80 85 这些是值,但我只在数据库中获取表头,但不是值。 这是代码。

 @SuppressWarnings({ "unchecked", "unused" })
 private static HashMap parseExcelData (List sheetData)
{
        HashMap<String,Integer> tableFields = new HashMap();
        List list = (List) sheetData.get(0);
        for (int j=0; j<list.size(); j++){
            Cell cell=(Cell) list.get(j);
         //   System.out.println("Values"+cell);
            tableFields.put(cell.getStringCellValue(),cell.getCellType());
           // System.out.println("Values"+cell.getStringCellValue());
    }

        return tableFields;

    }

private static String getCreateTable(String tablename, HashMap<String, Integer> tableFields){
    Iterator iter = tableFields.keySet().iterator();
    String str="";
    String[] allFields = new String[tableFields.size()];
    //System.out.println("all fields"+allFields);
    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;
        //System.out.println("all fields"+allFields);
    }
    try 
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/tutorial","root", "ranjini123");
        Statement  stmt = con.createStatement();
        try
        {
            System.out.println( "Use the database..." );
            stmt.executeUpdate( "USE tutorial;" );
        }
        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 " + "Book1" + " (" + 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 )
    {
        System.out.println( ((SQLException) e).getSQLState() );
        System.out.println( e.getMessage() );
        e.printStackTrace();
    }
    return str; 
}

@SuppressWarnings({ "unused", "unused", "unused", "unused" })
private Statement createStatement() {
    return null;
}

}

提前致谢

0 个答案:

没有答案