可以使用jackcess从Java连接到MS Access但不能与ucanaccess连接

时间:2015-07-22 20:07:01

标签: java ms-access-2007 ucanaccess

我将不得不从java 6迁移到java 8,所以必须用其他东西替换jdbc:odbc。

我尝试了ucanaccess,但有例外:

net.ucanaccess.jdbc.UcanaccessSQLException:数据异常:数值超出范围

readAllSites2 [ucanaccess] (C:\Mines\TestDBase.mdb)
driver: net.ucanaccess.jdbc.UcanaccessDriver
database: jdbc:ucanaccess://C:\Mines\TestDBase.mdb
readAllSites2 ERROR: net.ucanaccess.jdbc.UcanaccessSQLException: data exception: numeric value out of range
net.ucanaccess.jdbc.UcanaccessSQLException: data exception: numeric value out of range
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:247)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Aura.ReadSites.readAllSites2(ReadSites.java:92)
    at Aura.ReadSites.readAllSites(ReadSites.java:26)

Caused by: java.sql.SQLDataException: data exception: numeric value out of range
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.throwError(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setObject(Unknown Source)
    at net.ucanaccess.converters.LoadJet.execInsert(LoadJet.java:1326)
    at net.ucanaccess.converters.LoadJet.access$7(LoadJet.java:1322)
    at net.ucanaccess.converters.LoadJet$TablesLoader.loadTableData(LoadJet.java:775)
    at net.ucanaccess.converters.LoadJet$TablesLoader.loadTablesData(LoadJet.java:928)
    at net.ucanaccess.converters.LoadJet$TablesLoader.loadTables(LoadJet.java:972)
    at net.ucanaccess.converters.LoadJet$TablesLoader.access$3(LoadJet.java:966)
    at net.ucanaccess.converters.LoadJet.loadDB(LoadJet.java:1361)
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:236)
    ... 20 more
Caused by: org.hsqldb.HsqlException: data exception: numeric value out of range
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.types.NumberType.toDouble(Unknown Source)
    at org.hsqldb.types.NumberType.convertToDouble(Unknown Source)
    at org.hsqldb.types.NumberType.convertToDefaultType(Unknown Source)
    ... 30 more

尝试连接时。 使用jackcess的查询工作,但显然不是保存。 这是代码:

private int readAllSites1(String dbName) 
{
  System.out.println("readAllSites1 [jdbc.odbc] ("+dbName+")");
  vAllSites.removeAllElements();
  String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + dbName + ";DriverID=22;READONLY=true}";  
  System.out.println("driver: "+driver);
  System.out.println("database: "+database);
  String tableName = "Configurations";
  try 
  {
     Class.forName(driver);  
     Connection con = DriverManager.getConnection( database ,"",""); 
     Statement s = con.createStatement();
     s.execute("select SiteID,SiteName,PositionX,PositionY,PositionZ,Pvelocity,Svelocity from \""+tableName+"\" order by SiteID");
     ResultSet r = s.getResultSet(); 
     if (r == null) 
     { 
        s.close(); con.close(); 
        System.out.println("readAllSites1: ResultSet is null  exiting..."); 
        return(-1);
     }
     while ( r.next() ) 
     {
        ASite ae = new ASite();
        ae.ID = r.getInt("SiteID");
        ae.Name = r.getString("SiteName");
        double X = r.getDouble("PositionX"); 
        double Y = r.getDouble("PositionY"); 
        double Z = r.getDouble("PositionZ");
        ae.convertCoords(X,Y,Z);
        ae.PVel = r.getFloat("Pvelocity");
        ae.SVel = r.getFloat("Svelocity");
        vAllSites.addElement(ae);
     }
     s.close(); 
     con.close(); 
  }
  catch (Exception ex) 
  { 
     System.out.println("readAllSites1 ERROR: " + ex); 
     ex.printStackTrace();
     vAllSites.removeAllElements();
     return(-1);
  }
  return(vAllSites.size());
}
private int readAllSites2(String dbName) 
{
  System.out.println("readAllSites2 [ucanaccess] ("+dbName+")");
  vAllSites.removeAllElements();
  String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
  String database = "jdbc:ucanaccess://" + dbName;  
  System.out.println("driver: "+driver);
  System.out.println("database: "+database);
  String tableName = "Configurations";
  try 
  {
     Class.forName(driver);
     Connection con = DriverManager.getConnection( database ,"",""); //Line 92 - here is exception
     System.out.println("connected ");
     Statement s = con.createStatement();
     System.out.println("Statement created ");
     s.execute("select SiteID,SiteName,PositionX,PositionY,PositionZ,Pvelocity,Svelocity from \""+tableName+"\" order by SiteID"); // select the data from the table
     System.out.println("Statement executed ");
     ResultSet r = s.getResultSet(); 
     if (r == null) 
     { 
        s.close(); con.close(); 
        System.out.println("readAllSites2: ResultSet is null  exiting..."); 
        return(-1);
     }
     while ( r.next() ) 
     {
        ASite ae = new ASite();
        ae.ID = r.getInt("SiteID");
        ae.Name = r.getString("SiteName");
        double X = r.getDouble("PositionX"); 
        double Y = r.getDouble("PositionY"); 
        double Z = r.getDouble("PositionZ");
        ae.convertCoords(X,Y,Z);
        ae.PVel = r.getFloat("Pvelocity");
        ae.SVel = r.getFloat("Svelocity");
        vAllSites.addElement(ae);
     }
     s.close(); 
     con.close(); 
     printAllSites();
  }
  catch (Exception ex) 
  { 
     System.out.println("readAllSites2 ERROR: " + ex); 
     ex.printStackTrace();
     vAllSites.removeAllElements();
     return(-1);
  }
  return(vAllSites.size());
}
private int readAllSites3(String dbName) 
{
  System.out.println("readAllSites3 [jackcess] ("+dbName+")");
  vAllSites.removeAllElements();
  String tableName = "Configurations";
  try
  {    
     File dbFile = new File(dbName);
     Database db = new DatabaseBuilder().setReadOnly(true).open(dbFile);
//   Database db = DatabaseBuilder.open(dbFile);
     Table table = db.getTable(tableName);
     for (Row r : table) 
     {
        ASite ae = new ASite();
        ae.ID = r.getInt("SiteID");
        ae.Name = r.getString("SiteName");
        double X = r.getDouble("PositionX"); 
        double Y = r.getDouble("PositionY"); 
        double Z = r.getDouble("PositionZ");
        ae.convertCoords(X,Y,Z);
        ae.PVel = r.getFloat("Pvelocity");
        ae.SVel = r.getFloat("Svelocity");
        vAllSites.addElement(ae);
     }
     db.close();
  }
  catch (Exception ex) 
  { 
     System.out.println("readAllSites3 ERROR: " + ex); 
     ex.printStackTrace();
     vAllSites.removeAllElements();
     return(-1);
  }
  return(vAllSites.size());
}

readAllSites1使用jdbc:odbc并且工作

readAllSites2使用ucanaccess但不起作用

readAllSites3使用jackcess并正常工作

异常数值超出范围是奇怪的,因为它在尝试连接时发生,而不是在读取

期间

0 个答案:

没有答案