用于将xls数据导入mysql数据库的Java代码 [英] java code for importing xls data into mysql database
本文介绍了用于将xls数据导入mysql数据库的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将xls
文件导入到mysql
中,但出现错误Nullpointer EXCEPTION
,请给我解决方案
I'm trying to import xls
file into mysql
but getting an error of Nullpointer EXCEPTION
please give me the solution
package abhi;
public class ImportData {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test","root","abhi1402");
con.setAutoCommit(false);
PreparedStatement pstm = null ;
FileInputStream input = new FileInputStream("D:/New Folder/apnakhata1.xls");
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Row row;
for(int i=1; i<=sheet.getLastRowNum(); i++){
row = sheet.getRow(i);
String CountryName = row.getCell(1).getStringCellValue();
String CountryCode = row.getCell(2).getStringCellValue();
String BankName = row.getCell(3).getStringCellValue();
String Website = row.getCell(4).getStringCellValue();
String Name = row.getCell(5).getStringCellValue();
String SMS = row.getCell(6).getStringCellValue();
int SMSNumber = (int) row.getCell(7).getNumericCellValue();
int CustomerCare = (int) row.getCell(8).getNumericCellValue();
String sql = "INSERT INTO Bank VALUES('"+CountryName+"','"+CountryCode
+"','"+BankName+"','"+Website+"','"+Name+"','"+SMS+"','"+SMSNumber
+"','"+CustomerCare+"')";
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");
}catch(ClassNotFoundException e){
System.out.println(e);
}catch(SQLException ex){
System.out.println(ex);
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
}
}
推荐答案
您必须放入
String CountryName = row.getCell(0).getStringCellValue();
String CountryCode = row.getCell(1).getStringCellValue();
String BankName = row.getCell(2).getStringCellValue();
String Website = row.getCell(3).getStringCellValue();
String Name = row.getCell(4).getStringCellValue();
这篇关于用于将xls数据导入mysql数据库的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文