如何让uCanAccess使用Samba身份验证,用户名或密码中包含特殊字符?

问题描述 投票:1回答:1

TL; DR:创建数据库对象时,我应该为MS Access 2000-2003数据库使用什么Database.FileFormat常量?

我使用jCIFS构建了一个SAMBA测试应用程序。如果给出正确的身份验证凭据,它允许我创建/覆盖文件,无论我使用它在哪个域中的PC。

我还有一个使用uCanAccess / jackcess连接到网络共享上的MDB的应用程序。但是(根据我的理解),它使用登录用户的凭据,其中许多人具有只读访问权限。只有系统/网络管理员具有写入权限。

有问题的数据库没有密码保护。 (打开时我不需要输入密码。)

我的目的是让应用程序在写入数据库之前询问管理员的Samba凭据,使用uCanAccess连接中的那些,这样它就不会抛出java.nio.channels.NonWritableChannelException,如下面的堆栈跟踪:

java.nio.channels.NonWritableChannelException
  at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:747)
  at com.healthmarketscience.jackcess.impl.PageChannel.writePage(PageChannel.java:310)
  at com.healthmarketscience.jackcess.impl.PageChannel.writePage(PageChannel.java:247)
  at com.healthmarketscience.jackcess.impl.TableImpl.writeDataPage(TableImpl.java:1980)
  at com.healthmarketscience.jackcess.impl.TableImpl.addRows(TableImpl.java:2229)
  at com.healthmarketscience.jackcess.impl.TableImpl.addRow(TableImpl.java:2067)
  at net.ucanaccess.converters.UcanaccessTable.addRow(UcanaccessTable.java:44)
  at net.ucanaccess.commands.InsertCommand.insertRow(InsertCommand.java:101)
  at net.ucanaccess.commands.InsertCommand.persist(InsertCommand.java:148)
  at net.ucanaccess.jdbc.UcanaccessConnection.flushIO(UcanaccessConnection.java:315)
  at net.ucanaccess.jdbc.UcanaccessConnection.commit(UcanaccessConnection.java:205)
  at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:217)
  at net.ucanaccess.jdbc.Execute.execute(Execute.java:46)
  at net.ucanaccess.jdbc.UcanaccessPreparedStatement.execute(UcanaccessPreparedStatement.java:228)
  at myapp.db.Digger.addTransaction(Digger.java:993)
  at myapp.tasks.TransactionRunnable.run(TransactionRunnable.java:42)
  at java.lang.Thread.run(Thread.java:745)

更新:我尝试过使用Gord Thompson和J. T. Alhborn,smbFileChannelshown here课程。我的代码基于该答案中显示的主类,如下所示:

// Ask the user for login credentials and the path to the database
String smbURL = (chosenDir.endsWith("/") ? chosenDir : chosenDir + '/') 
  + dbName;
System.out.println("DB Path to use for URL: " + smbURL);
URL u = new URL(smbURL);
try (
  // construct the SMB DB URL
  SmbFileChannel sfc = new SmbFileChannel(smbURL);
  Database db = new DatabaseBuilder().setChannel(sfc)
    .setFileFormat(Database.FileFormat.GENERIC_JET4).create();
) {
  // Model the table
  Table tbl = new TableBuilder("Transactions")
    .addColumn(new ColumnBuilder("TransactionID", DataType.LONG).setAutoNumber(true))
    .addColumn(new ColumnBuilder("ControllerID", DataType.LONG).setAutoNumber(false))
    .addColumn(new ColumnBuilder("ReaderID", DataType.LONG).setAutoNumber(false))
    .addColumn(new ColumnBuilder("Event", DataType.LONG).setAutoNumber(false))
    .addColumn(new ColumnBuilder("Timestamp", DataType.SHORT_DATE_TIME).setAutoNumber(false))
    .addColumn(new ColumnBuilder("Number", DataType.LONG).setAutoNumber(false))
    .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME).addColumns("TransactionID").setPrimaryKey())
    .toTable(db);
  // Add the row
  Map<String, Object> values = new HashMap<>();
  values.put("ControllerID", cid);
  values.put("ReaderID", rid);
  values.put("Event", evtNum);
  values.put("Timestamp", ts); // Long; must be converted to DataType.SHORT_DATE_TIME
  values.put("Number", accNum);
  tbl.addRowFromMap(values);
} catch (IOException IOEx) {
  System.err.println(
    "Failed to write record to Transactions table in database: " 
    + IOEx.getMessage()
   );
   IOEx.printStackTrace(System.err);
  } catch (Exception ex) {
    System.err.println(
      '[' + ex.getClass().getSimpleName() + "]: Failed to write record to "
      + "Transactions table in database: " + ex.getMessage()
    );
    ex.printStackTrace(System.err);
  }

执行上面的代码会产生以下输出:

DB Path to use for URL: smb://machine.vpnName/Storage/me/dbs/DBName.mdb
Failed to write record to Transactions table in database: Logon failure: account currently disabled.
jcifs.smb.SmbAuthException: Logon failure: account currently disabled.
   at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:546)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
    at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:390)
    at jcifs.smb.SmbSession.send(SmbSession.java:218)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:957)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.open0(SmbFile.java:975)
    at jcifs.smb.SmbFile.open(SmbFile.java:1009)
    at jcifs.smb.SmbRandomAccessFile.<init>(SmbRandomAccessFile.java:57)
    at jcifs.smb.SmbRandomAccessFile.<init>(SmbRandomAccessFile.java:42)
    at samba.SmbFileChannel.<init>(SmbFileChannel.java:30)
    at samba.SambaLanWriteTest.writeTest(SambaLanWriteTest.java:130)
    at samba.SambaLanWriteTest.main(SambaLanWriteTest.java:181)

使用Windows文件资源管理器时,我可以访问相关数据库文件的测试副本。我在提示时选择那个。

更新2:我意识到我忽略了将我的用户名和密码添加到smb:// URL,正如Thompson的例子所示。我改为代码:

String smbCred = "smb://" + auth.getUsername() + ":" + auth.getPassword() + "@",
  fixer = chosenDir.replace("\\", "/").replace("smb://", smbCred),
  smbURL = fixer + dbName;
System.out.println("DB Path to use for URL: " + smbURL);
// URL u = new URL(smbURL);

我遇到的下一个问题是我的密码包含 特别 非法字符(例如'@',':',';','='和'?')。我在java.net.URLEncoder.encode()auth.getUsername()上使用auth.getPassword()逃脱了这些,因此代码在创建MalformedURLException时不会抛出SmbChannel。但是,我遇到的下一个异常如下:

Failed to write record to Transactions table in database: File format GENERIC_JET4 [VERSION_4] does not support file creation for null
java.io.IOException: File format GENERIC_JET4 [VERSION_4] does not support file creation for null
    at com.healthmarketscience.jackcess.impl.DatabaseImpl.create(DatabaseImpl.java:444)

在创建Database.FileFormat对象时,我应该为MS Access 2000-2003数据库使用什么Database常量?

java-7 jcifs jackcess
1个回答
1
投票

事实证明,我需要使用Database.FileFormat.V2000

在那之后,它一帆风顺(虽然我仍然需要弄清楚如何让Long时间戳正确转换)。

© www.soinside.com 2019 - 2024. All rights reserved.