Java 在Microsoft Access数据库中插入迭代器值
在这里,我正在阅读excel文件并尝试将值插入数据库Java 在Microsoft Access数据库中插入迭代器值,java,jsp,jdbc,Java,Jsp,Jdbc,在这里,我正在阅读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"));
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();
}
连接文件:
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();
}
}
excel文件似乎已找到,但您的程序似乎无法找到(或打开)MS Access数据库文件。你能在MyConnection类(new MyConnection())中添加一个main方法来打开数据库文件吗?@claj:谢谢,我不明白你想说什么。你能告诉代码当你运行MyConnection类时会发生什么吗?你也有同样的错误吗?@claj:是的,文件名有错误。我更新
String connURL=“jdbc:odbc:DRIVER={Microsoft Access驱动程序(*.mdb,*.accdb)};DBQ=“+accessFileName+”.mdb;”代码>@claj:没有问题了。它不会插入数据库,也不会像代码中那样打印值
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();
}
}