Create an Access database file (.mdb or .accdb) using Java

后端 未结 5 652
走了就别回头了
走了就别回头了 2020-12-06 07:02

Currently I have one application in which I am able to access .mdb or .accdb file with JdbcOdbcDriver to append some data.

Class.forName(\"sun.jdbc.odbc.Jdb         


        
5条回答
  •  失恋的感觉
    2020-12-06 07:51

    Update for Jackcess 2.x: Databases are now created (or opened) using DatabaseBuilder, so to create a new database file we do

    import java.io.File;
    import java.io.IOException;
    
    import com.healthmarketscience.jackcess.Database;
    import com.healthmarketscience.jackcess.Database.FileFormat;
    import com.healthmarketscience.jackcess.DatabaseBuilder;
    
    public class JackcessDemoMain {
    
        public static void main(String[] args) {
            String dbPath = "C:/Users/Public/newDb.accdb";
            // using try-with-resources is recommended to ensure that 
            //   the Database object will be closed properly
            try (Database db = DatabaseBuilder.create(FileFormat.V2010, new File(dbPath))) {
                System.out.println("The database file has been created.");
            } catch (IOException ioe) {
                ioe.printStackTrace(System.err);
            }
    
        }
    
    }
    

    Original answer for Jackcess 1.x (deprecated):

    If you would like to create the “.mdb” file through java, you can use the Jackcess Java library which is one of the pure Java Library for reading from and writing to MS Access databases. Currently supporting versions include 2000-2007 I guess. Please have a look at the below example for better understanding:

    1. Download Jackcess Java library (jackcess-1.2.6.jar) from http://jackcess.sourceforge.net/ and commons-logging-1.1.jar from http://commons.apache.org/logging/download_logging.cgi and commons-lang-2.0.jar from http://www.findjar.com/index.x?query=commons-lang
    2. Add both jars to your classpath.
    3. Try the below code to create a database automatically:

    package com.jackcess.lib;
    
    
    import com.healthmarketscience.jackcess.ColumnBuilder;
    import com.healthmarketscience.jackcess.Database;
    import com.healthmarketscience.jackcess.Table;
    import com.healthmarketscience.jackcess.TableBuilder;
    import java.io.File;
    import java.io.IOException;
    import java.sql.SQLException;
    import java.sql.Types;
    
    /**
     *
     * @author sarath_ivan
     */
    public class JackcessLibrary {
    
        private static Database createDatabase(String databaseName) throws IOException {
            return Database.create(new File(databaseName));
        }
    
        private static TableBuilder createTable(String tableName) {
            return new TableBuilder(tableName);
        }
    
        public static void addColumn(Database database, TableBuilder tableName, String columnName, Types sqlType) throws SQLException, IOException {
            tableName.addColumn(new ColumnBuilder(columnName).setSQLType(Types.INTEGER).toColumn()).toTable(database);
        }
    
        public static void startDatabaseProcess() throws IOException, SQLException {
            String databaseName = "C:/Users/compaq/Desktop/employeedb.mdb"; // Creating an MS Access database
            Database database = createDatabase(databaseName);
    
            String tableName = "Employee"; // Creating table
            Table table = createTable(tableName)
                    .addColumn(new ColumnBuilder("Emp_Id").setSQLType(Types.INTEGER).toColumn())
                    .addColumn(new ColumnBuilder("Emp_Name").setSQLType(Types.VARCHAR).toColumn())
                    .addColumn(new ColumnBuilder("Emp_Employer").setSQLType(Types.VARCHAR).toColumn())
                    .toTable(database);
    
            table.addRow(122875, "Sarath Kumar Sivan","Infosys Limited.");//Inserting values into the table
        }
    
        public static void main(String[] args) throws IOException, SQLException {
            JackcessLibrary.startDatabaseProcess();
        }
    }
    

提交回复
热议问题