Hello everyone, can someone help me with this please?
Create class which will create tables in data base (Microsoft Access). About the tables: number of columns, type of data, lenght of the data.
And mantaining the information (the data) in the tables- add data, delete data and edit data.
So far I have codes which are connected with SQL, and I need them in Access. Here are some codes.

import java.sql.*;

public class CreateTable{
  public static void main(String[] args) {
    System.out.println("Table Creation Example!");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driverName = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";
    try{
      Class.forName(driverName).newInstance();
      con = DriverManager.getConnection(url+dbName, userName, password);
      try{
        Statement st = con.createStatement();
        String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
        st.executeUpdate(table);
        System.out.println("Table creation process successfully!");
      }
      catch(SQLException s){
        System.out.println("Table all ready exists!");
      }
      con.close();
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

I can post more codes but they're all with SQL.
Any help?

Recommended Answers

All 8 Replies

Will this work?:

import java.sql.*;

public class dbAccess
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = 
              "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;";
            Connection conn = DriverManager.getConnection(database, "", "");
            Statement s = conn.createStatement();
            
            // create a table
            String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
            String createTable = "CREATE TABLE " + tableName + 
                                 " (id Integer, name Text(32))";
            s.execute(createTable); 
            
            // enter value into table
            for(int i=0; i<25; i++)
            {
              String addRow = "INSERT INTO " + tableName + " VALUES ( " + 
                     String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " + 
                     String.valueOf(Math.random()) + "')";
              s.execute(addRow);
            }
            
            // Fetch table
            String selTable = "SELECT * FROM " + tableName;
            s.execute(selTable);
            ResultSet rs = s.getResultSet();
            while((rs!=null) && (rs.next()))
            {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }
            
            // drop the table
            String dropTable = "DROP TABLE " + tableName;
            s.execute(dropTable);
            
            // close and cleanup
            s.close();
            conn.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Will this work?:

Try yourself.

commented: Seems like a good place to start :) +9

The question is this: I work in Eclipse. How to connect Eclipse with Access?
The code is just fine, it has no errors.
The errors that the code is giving me are:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
	at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at dbAccess.main(dbAccess.java:12)

google search --> Database Access Eclipse tutorial

My result:

run:
java.sql.SQLException: [Microsoft][Sterownik ODBC Microsoft Access ] Nie mo?na znale?? pliku '(nieznane)'.//unknown
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
        at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
        at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at didi00.dbAccess.main(dbAccess.java:23)

where is your "sun.jdbc.odbc.JdbcOdbcDriver" ?

Thanks. One more question:
Should I create ODBC entry for the data base this way:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
I'm new in data bases so any help will work.
Thanks

Thanks. One more question:
Should I create ODBC entry for the data base this way:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
I'm new in data bases so any help will work.
Thanks

Did it work for you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.