Search code examples
javams-accessjdbcjdbc-odbc

How to create a new field in access through JDBC


I am looking for a way to insert a field/column into a record in MS-Access through Java. I believe that I need to use executeUpdate(INSERT...), however I cannot seem to get my program to add new fields/columns. Any help would be appreciated, and an example of the correct line would be even better.

Thanks, ~Crash


Solution

  • To add a new field (column) to an existing table you can use an ALTER TABLE statement like this:

    String connectionString = 
            "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + 
            "DBQ=C:\\Users\\Public\\Database1.accdb;";
    Connection con = DriverManager.getConnection(connectionString);
    Statement st = con.createStatement();
    st.execute("ALTER TABLE Customers ADD COLUMN Address TEXT(255)");
    

    For more information on ALTER TABLE see

    ALTER TABLE Statement (Microsoft Access SQL)