简体   繁体   English

Java将Excel数据导入有关编码的MySQL

[英]Java Import Excel Data into MySQL about encoding

here my source!! 这是我的资料!!

String url    =   "jdbc:mysql://localhost/dahan?characterEncoding=euckr";
String user    =   "root";
String password   =   "pass";

    try{
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con = (Connection)                                                      DriverManager.getConnection(url,user,password);  
        con.setAutoCommit(false);  
        PreparedStatement pstm = null ; 
        PreparedStatement pstm1 = null ; 
        PreparedStatement pstm2 = null ; 
        FileInputStream input = new FileInputStream("C:/Users/hyunwoo/Downloads/Personal Contacts.xls");  
        POIFSFileSystem fs = new POIFSFileSystem( input );  
        HSSFWorkbook wb = new HSSFWorkbook(fs);  
        HSSFSheet sheet = wb.getSheetAt(0);
    Row row;
    String del = "DROP TABLE IF EXISTS `dahanMail`";
    String cre = "CREATE TABLE `dahanMail` (`전체이름`  varchar(50),`성`  varchar(50),`이름`  varchar(50) ,`닉네임`  varchar(50),"
            + "`회사`  varchar(500),`부서`  varchar(500),`직급`  varchar(500),`메일주소1`  varchar(50),"
            + "`메일주소2`  varchar(50),`전화번호1`  varchar(50),`전화번호2`  varchar(50),`전화번호3`  varchar(50),"
            + "`휴대폰1`  varchar(50),`휴대폰2`  varchar(50),`팩스1`  varchar(50),`팩스2`  varchar(50),"
            + "`주소`  varchar(50),`웹사이트`  varchar(50),`id`  int NOT NULL ,PRIMARY KEY (`id`))";
    pstm1 = (PreparedStatement) con.prepareStatement(del);
    pstm2 = (PreparedStatement) con.prepareStatement(cre);
    pstm1.execute();
    pstm2.execute();
for(int i=0; i<=sheet.getLastRowNum(); i++){
        row = sheet.getRow(i);
        String fullname = row.getCell(0).getStringCellValue();
        String lastname = row.getCell(1).getStringCellValue();
        String firstname = row.getCell(2).getStringCellValue();
        String nickname = row.getCell(3).getStringCellValue();
        String company = row.getCell(4).getStringCellValue();
        String sub = row.getCell(5).getStringCellValue();
        String hei = row.getCell(6).getStringCellValue();
        String mailaddress1 = row.getCell(7).getStringCellValue();
        String mailaddress2 = row.getCell(8).getStringCellValue();
        String cnumber1 = row.getCell(9).getStringCellValue();
        String cnumber2 = row.getCell(10).getStringCellValue();
        String cnumber3 = row.getCell(11).getStringCellValue();
        String pnumber1 = row.getCell(12).getStringCellValue();
        String pnumber2 = row.getCell(13).getStringCellValue();
        String fax1 = row.getCell(14).getStringCellValue();
        String fax2 = row.getCell(15).getStringCellValue();
        String address = row.getCell(16).getStringCellValue();
        String website = row.getCell(17).getStringCellValue();
        int id = (int) row.getCell(18).getNumericCellValue();

        String sql = "INSERT INTO dahanmail VALUES('"+fullname+"','"+lastname+"','"+firstname+"'"
                        + ",'"+nickname+"','"+company+"','"+sub+"','"+hei+"','"+mailaddress1+"','"+mailaddress2+"','"+cnumber1+"','"+cnumber2+"','"+cnumber3+"'"
                                        + ",'"+pnumber1+"','"+pnumber2+"','"+fax1+"','"+fax2+"',"
                                                + "'"+address+"','"+website+"','"+id+"')";

       pstm = (PreparedStatement) con.prepareStatement(sql);

       pstm.execute();
       System.out.println("Import rows "+i);
       }
       con.commit();
       pstm.close();
       con.close();
       input.close();
       System.out.println("Success import excel to mysql table");

I have saved the excel data to mysql using the POI in Java and It works well! 我已经使用Java中的POI将excel数据保存到mysql,并且效果很好! so I checked the mysql table in cmd but console window still show broken language like "???" 所以我检查了cmd中的mysql表,但是控制台窗口仍然显示像“ ???”这样的损坏语言 it seems to be encoding problem... How can I change the source to fix the encoding problem? 似乎是编码问题...如何更改源以解决编码问题?

What you have looks like it's close to working, but there are some things I would change: 您所拥有的似乎已经接近工作状态,但是有些事情我会改变:

Firstly, declare your SQL before the loop, with ?'s for value placeholders: 首先,在循环之前声明SQL,对于值占位符使用?'s:

String sql = "INSERT INTO dahanmail VALUES(?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);

(I have not put enough ? s in the code above, there should be one for each value) (我在上面的代码中没有放入足够的? ,每个值应该有一个)

Then, in the loop, you can set the parameters: 然后,在循环中,您可以设置参数:

pstm.setString(1,row.getCell(0).getStringCellValue());
pstm.setString(2,row.getCell(2).getStringCellValue());
//one for each value. You could use another loop.

Then (still in the loop), execute the statement: 然后(仍然在循环中)执行以下语句:

pstm.execute();

Finally clear the parameters for next time round: 最后清除下一次参数:

pstm.clearParameters();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM