这里我正在阅读excel文件并尝试将值插入数据库。
public static void main(String[] args) throws IOException, SQLException {
Myconnection con = new Myconnection();
Statement stmt = null;
FileInputStream fis = new FileInputStream(new File("C:/upload.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator ite = sheet.rowIterator();
PreparedStatement prepStmt = con.getConnection().prepareStatement("insert into ParcelCoordinates(Subdivision,ParcelNo,PointID,Easting,Northing,Height) values (?,?,?,?,?,?)");
while(ite.hasNext()){
Row row = (Row) ite.next();
Iterator<Cell> cite = row.cellIterator();
while(cite.hasNext()){
Cell p = cite.next();
prepStmt.setString(1, p.getStringCellValue());
prepStmt.setString(2,p.getStringCellValue());
prepStmt.setString(3,p.getStringCellValue());
prepStmt.setString(4,p.getStringCellValue());
prepStmt.setString(5,p.getStringCellValue());
prepStmt.setString(6,p.getStringCellValue());
prepStmt.addBatch();
}
System.out.println(cite);
while(cite.hasNext()){
Cell c = cite.next();
System.out.print(c.toString() +" "); //Gives single cell values correctly
}
System.out.println();
}
fis.close();
}
这里有什么问题。收到错误消息:
Exception :java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.Exception in thread "main"
java.lang.NullPointerException
at com.concretepage.poi.upload.main(upload.java:30)
连接文件:
public class Myconnection
{
public Myconnection(){}
public Connection getConnection()
{
Connection con = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// String accessFileName = "C:/Documents and Settings/user/My Documents/CUInfo";
String accessFileName = "C:/CUID/RequestForCUIDInfo";
String connURL="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accessFileName+".accdb;";
con = DriverManager.getConnection(connURL, "","");
System.out.println("connection created successfully");
}
catch (Exception e)
{
System.out.println("Exception :" + e);
}
return con;
}
public static void main(String[] args) {
Myconnection cf = new Myconnection();
cf.getConnection();
}
}