结合使用Jackcess和JCIFS来操纵SMB共享上的Access数据库 [英] Using Jackcess with JCIFS to manipulate an Access database on an SMB share

查看:87
本文介绍了结合使用Jackcess和JCIFS来操纵SMB共享上的Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Jackcess在Java中使用MS Access文件.该文件位于SMB共享上,因此我认为我必须使用JCIFS.

I need to work with an MS Access file in Java using Jackcess. The file is located on an SMB share so I assume I would have to use JCIFS.

我尝试过

    String testdirectory = "smb://" + "file location"; 

    SmbFile testsmbdir = null;

    try{
            testsmbdir = new SmbFile(testdirectory,auth);
    }catch(Exception e){
                e.printStackTrace();
    }


    SmbFileInputStream smbFilestream = new SmbFileInputStream(testsmbdir);
    db = DatabaseBuilder.open(testsmbdir);

但是,它表示SMBFile不能转换为

However, it says SMBFile can not be converted to File for the

db = DatabaseBuilder.open(testsmbdir)" 

行.另外,如果我尝试使用"smbFilestream"来代替,它说它也不能将SmbFileInputStream转换为File.

line. Also if I try using "smbFilestream" instead it says it cannot convert SmbFileInputStream to File either.

我是否必须将文件复制到本地计算机或完全不同的文件?如果可以怎么办?

Do I have to copy the file to the local machine or something completely different? If how can I do so?

(顺便说一句,我是Windows用户.我只是将我的应用程序转换为Mac,如果我的行话关闭了,抱歉.)

(I'm a windows user by the way. I am just converting my application to Mac so sorry if my lingo is off.)

推荐答案

在Jackcess论坛上回复主题

In reply to a thread on the Jackcess forums here, James suggested that

实现与SmbRandomAccessFile一起使用的FileChannel版本应该相对简单

it should be relatively straightforward to implement a version of FileChannel which works with a SmbRandomAccessFile

我刚刚在Eclipse中一个名为 smb4jackcess 的Maven项目中进行了尝试,并且无需编写太多代码就可以使它工作.我创建的类名为 SmbFileChannel :

I just tried it in a Maven project named smb4jackcess in Eclipse, and I got it working without having to write too much code. The class I created is named SmbFileChannel:

// FileChannel using jcifs.smb.SmbRandomAccessFile

package smb4jackcess;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbRandomAccessFile;

public class SmbFileChannel extends FileChannel {
    private final SmbRandomAccessFile _file;
    private long _length;

    public SmbFileChannel(String smbURL) throws SmbException, MalformedURLException, UnknownHostException {
        _file = new SmbRandomAccessFile(smbURL, "rw", SmbFile.FILE_NO_SHARE);
        _length = _file.length();
    }

    @Override
    public void force(boolean metaData) throws SmbException, MalformedURLException, UnknownHostException {
        // do nothing
    }

    @Override
    public FileLock lock(long position, long size, boolean shared) {
        throw new UnsupportedOperationException();
    }

    @Override
    public MappedByteBuffer map(MapMode mode, long position, long size) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long position() throws SmbException {
        return _file.getFilePointer();
    }

    @Override
    public FileChannel position(long newPosition) throws SmbException {
        _file.seek(newPosition);
        return this;
    }

    @Override
    public int read(ByteBuffer dst) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int read(ByteBuffer dst, long position) throws SmbException {
        byte[] b = new byte[dst.remaining()];
        _file.seek(position);
        int bytesRead =_file.read(b);
        dst.put(b);
        return bytesRead;
    }

    @Override
    public long read(ByteBuffer[] dsts, int offset, int length) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long size() throws SmbException {
        return _length;
    }

    @Override
    public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate((int)count);
        int bytesWritten = src.read(bb);
        bb.rewind();
        bb.limit(bytesWritten);
        this.write(bb, position);
        return bytesWritten;
    }

    @Override
    public long transferTo(long position, long count, WritableByteChannel target) {
        throw new UnsupportedOperationException();
    }

    @Override
    public FileChannel truncate(long newSize) throws SmbException {
        if (newSize < 0L) {
            throw new IllegalArgumentException("negative size");
        }
        _file.setLength(newSize);
        _length = newSize;
        return this;
    }

    @Override
    public FileLock tryLock(long position, long size, boolean shared) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int write(ByteBuffer src) throws SmbException {
        throw new UnsupportedOperationException();
    }

    @Override
    public int write(ByteBuffer src, long position) throws SmbException {
        byte[] b = new byte[src.remaining()];
        src.get(b);
        _file.seek(position);
        _file.write(b);

        long endPos = position + b.length;
        if(endPos > _length) {
            _length = endPos;
        }

        return b.length;
    }

    @Override
    public long write(ByteBuffer[] srcs, int offset, int length) {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void implCloseChannel() throws SmbException {
        _file.close();
    }

}

我使用的主要课程是

package smb4jackcess;

import java.nio.channels.FileChannel;

import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Database.FileFormat;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.IndexBuilder;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;

public class Smb4jackcessMain {

    public static void main(String[] args) {

        String smbURL = "smb://gord:mypassword@SERVERNAME/sharename/etc/newdb.accdb";
        try (SmbFileChannel sfc = new SmbFileChannel(smbURL)) {

            // create a brand new database file                
            Database db = new DatabaseBuilder()
                    .setChannel(sfc)
                    .setFileFormat(FileFormat.V2010)
                    .create();

            // add a table to it
            Table newTable = new TableBuilder("NewTable")
                    .addColumn(new ColumnBuilder("ID", DataType.LONG)
                            .setAutoNumber(true))
                    .addColumn(new ColumnBuilder("TextField", DataType.TEXT))
                    .addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME)
                            .addColumns("ID").setPrimaryKey())
                    .toTable(db);

            // insert a row into the table
            newTable.addRow(Column.AUTO_NUMBER, "This is a new row.");

            db.close();
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

    }

}

2016-02-04 更新: 代码改进.非常感谢Dell Boomi的James的帮助!

Updated 2016-02-04: Code improvements. Many thanks to James at Dell Boomi for his assistance!

这篇关于结合使用Jackcess和JCIFS来操纵SMB共享上的Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆