这是我面临的困境,我正在使用JXl从电子表格导入数据并插入到sqlite表中,按照我的方式,用户将选择电子表格文件并指定要从中导入的表格,然后指定要从中开始的行和要结束的行,我已经尝试了所有我知道哪个不太顺便(新手程序员),程序有时会插入大部分在200个范围内的所有行并且有时只会在崩溃前将不超过27行插入数据库:
" SEVERE:null java.sql.SQLException:无法打开数据库文件"
我想知道为什么有些人会工作,有时候不这样做。下面是我在提取和插入数据库时使用的方法。我将非常感谢你的帮助。提前谢谢。
/**
*
* @param filename: the file to read from
* @param sheetn: the sheet to read from
*/
public void readXLS(String filename, String sheetn) {
try {
Workbook wb = null;
wb = Workbook.getWorkbook(new File(filename));
Sheet sheet = wb.getSheet(sheetn);
JOptionPane.showMessageDialog(null, "Extracting!");
//define sql statement for extracting form the xls and insert into the DB
String sqlInsertString = "insert into DEDUCTION(STAFFNUM,M_SAV,COM_PP1,COM_PP2,COM_PP3,COM_PP4,COM_PP5,CPP,DEDUCT,CHARGES,E_LOAN,O_LOAN,A_DEDUC,SHARES,DEPOSIT,COUNTER,PP,DEDUCTION,DED_DATE,ORGANISATION) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
System.out.println("test");
int startRow = Integer.parseInt(txtStartrow.getText());
int i = startRow - 1;//for traversal
int endRow = Integer.parseInt(txtEndrow.getText());
int x = endRow - 1;
String date = txtDate.getText().toUpperCase();
String org = comboOrg.getSelectedItem().toString();
pst = conn.prepareStatement(sqlInsertString);
for (int row = i; row <= x; row++) {
//get values from the sheet in the workbook for the deduction
Cell staffNumber = sheet.getCell(1, row);//staff number
pst.setString(1, staffNumber.getContents());
Cell month = sheet.getCell(2, row);//monthly savings
pst.setString(2, month.getContents());
Cell comp1 = sheet.getCell(3, row);//commodity 1
pst.setString(3, comp1.getContents());
Cell compp2 = sheet.getCell(4, row);//commodity 2
pst.setString(4, compp2.getContents());
Cell compp3 = sheet.getCell(5, row);//commodity 3
pst.setString(5, compp3.getContents());
Cell compp4 = sheet.getCell(6, row);//commdity 4
pst.setString(6, compp4.getContents());
Cell com5 = sheet.getCell(7, row);//commodity 5
pst.setString(7, com5.getContents());
Cell cppp = sheet.getCell(8, row);//cpp
pst.setString(8, cppp.getContents());
Cell ded = sheet.getCell(9, row);//deductions colum
pst.setString(9, ded.getContents());
Cell chg = sheet.getCell(10, row);//charges colum
pst.setString(10, chg.getContents());
Cell el = sheet.getCell(11, row);//emergency column
pst.setString(11, el.getContents());
Cell ol = sheet.getCell(12, row);// other loan row
pst.setString(12, ol.getContents());
Cell ad = sheet.getCell(13, row);// adeduct column
pst.setString(13, ad.getContents());
Cell sh = sheet.getCell(14, row);//shares colomn
pst.setString(14, sh.getContents());
Cell dep = sheet.getCell(15, row);//deposit column
pst.setString(15, dep.getContents());
Cell gp = sheet.getCell(16, row);//gp column
pst.setString(16, gp.getContents());
Cell payp = sheet.getCell(17, row);// payment period of loan if any
pst.setString(17, payp.getContents());
Cell dedu = sheet.getCell(18, row);// deducted amount
pst.setString(16, dedu.getContents());
//now fix the values into the deduction table in the database
pst.setString(19, date);
pst.setString(20, org);
pst.executeUpdate();
}
pst.close();
conn.close();
JOptionPane.showMessageDialog(null, "Accounts updated!");
} catch (SQLException ex) {
Logger.getLogger(Deductions.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
} catch (BiffException ex) {
Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
}
}