Search code examples
javams-accessjdbcucanaccess

CREATE TABLE statements silently failing under executeBatch


Here is my part of a code. In this when I execute sql statements, the create table is not creating any tables in the database and there are no errors thrown. Why is this?

I'm using NetBeans with JDK SE 8 with MS-Access.

String sql1="Insert into Signup_Login (UserName,DOB,EmailId,Password) values ('"+name.getText()+"','"+dob.getText()+"','"+emailId.getText()+"','"+pass.getText()+"')";
String sql2="create table "+emailId.getText()+"Inbox(InMsgs varchar(1000), primary key(InMsgs))";
String sql3="create table "+emailId.getText()+"Outbox(OutMsgs varchar(1000), primary key(OutMsgs))";

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/LENOVO/Documents/Email.accdb");
st=con.createStatement();
st.addBatch(sql1);
st.addBatch(sql2);
st.addBatch(sql3);
st.executeBatch();

Solution

  • Using UCanAccess 3.0.3.1 I was able to reproduce your issue with trying to run CREATE TABLE statements using addBatch()/executeBatch(). However, they seem to work okay if each one is run independently using executeUpdate().

    Also, because the VARCHAR columns are wider than 255 characters your CREATE TABLE statements will actually create tables with a "Memo" (a.k.a. "Long Text") field as their primary key. Creating such a table is not allowed in the Access UI and therefore should probably be considered "Not Recommended". Instead, use something like this to create a table with an AutoNumber primary key:

    String sql2="CREATE TABLE ["+emailId.getText()+"Inbox] (id COUNTER PRIMARY KEY, InMsgs MEMO)";
    

    (... ignoring, for the moment, the fact that creating separate identical tables for each emailId is almost certainly a bad idea.)