0

conn = Connect.ConnectDB();

    String sql = "insert into Ianouarios ("
            +"id,"
            +"Hmerominia,"
            +"Agores,"
            +"Pliromes,"
            +"Eksoda,"
            + "Zhta,"
            +"Metaforika,"
            +"Pliromimetafo,"
            +"Epitages,"
             +"Xondriki,"
            +"Noiki,"
            +"Plirominoiki)"
            + "values("+a1.getText()+ ",'"+a2.getText()+"','"+a3.getText()+"','"+a4.getText()+"','"+a5.getText()+"','"+a6.getText()+"','"+a7.getText()+"','"+a8.getText()+"','"+a9.getText()+"','"+ a10.getText()+ "','"+a11.getText()+"','"+a12.getText()+"')" ;
    try{
        pst = conn.prepareStatement(sql);
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "Saved");
        //UpdateJTable();
        //conn.close();
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex);

I have an ms access database and i am updating it with the jdbc-odbc driver.

Can anyone show me how to do the same to work with ucanaccess driver?

I tried their ucanaccess page examples but no luck.

Update: I did this:

conn = Connect.ConnectDB();

    try{




  String sql = "INSERT INTO Synola(id,Hmerominia,Agores,Pliromes,Eksoda,Zhta,Metaforika,Pliromimetafo,Epitages,Xondriki,Noiki,Plirominoiki) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)" ;


   /* SimpleDateFormat ddmmyyyyFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    Timestamp ddmmyyyy = new Timestamp(ddmmyyyyFormat.parse(a2.getText()).getTime()); */
   // String s=ddmmyyyy.toString();



        pst = conn.prepareStatement(sql);
        pst.setString(1, a1.getText());
        pst.setString(2,a2.getText());
        pst.setString(3, a3.getText());
        pst.setString(4, a4.getText());
        pst.setString(5, a5.getText());
        pst.setString(6, a6.getText());
        pst.setString(7, a7.getText());
        pst.setString(8, a8.getText());
        pst.setString(9, a9.getText());
        pst.setString(10, a10.getText());
        pst.setString(11, a11.getText());
        pst.setString(12, a12.getText());
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "Saved");
       //UpdateJTableSynola();
       // conn.close();
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex);

    }

And now the problem is the date from the a2.getText() field which i put into database as (dd/MM/yyyy).The error is unparseable date.How can i fix this?

asd32
  • 45
  • 3
  • 10
  • You should be using a *parameterized query* (` ... VALUES (?,?, ...` along with `pst.setString`, `pst.setInt`, etc.) to avoid SQL injection and literal quoting issues, but apart from that the code should be essentially identical. Please [edit] your question to show the errors, if any, that you receive. – Gord Thompson Jun 15 '16 at 23:40
  • I made the connection with the database and i can read when i press the button the last id of table but when i am pressing the button to insert into table data with the above code the program just freezes and no error is showing.When i was using jdbc-odbc this code worked.I can not figure what to write into name and how to write values with ucanaccess drive. – asd32 Jun 16 '16 at 10:51
  • I fixed it after combining many previous answers regarding the ucanaccess and experiments with the ms database values. – asd32 Jun 17 '16 at 16:55

1 Answers1

3

As JDBC-ODBC Bridge has been removed in JDK8, what you can do is to use JDBC along with UCanAccess API to connect to an MSAccess database.

connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");

Here is a small example(tested in Java 8) which connects to a remote MS-Access DB using JDBC/UCanAccess API. Make sure you have the following jars in your project build path.

  1. commons-lang-2.6.jar
  2. commons-logging-1.1.1.jar
  3. hsqldb.jar
  4. jackcess-2.1.0.jar
  5. ucanaccess-2.0.9.5.jar

Code:

/**
* Connects to a remote MS-Access DB using JDBC/UCanAccess API.
*/
package miscellaneous;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ConnectRemoteDB {
    public static void main(String[] args) {
        initializeConnection();
    }

    /**
    * Initializes remote database connection and inserts a record and closes the connection.
    */
    private static void initializeConnection() {
        System.out.println("Attempting Database Connection...");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DriverManager
            .getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
            System.out.println("CONNECTION ESTABLISHED....");
            String insertTableSQL = "INSERT INTO Table1" + "(Name) VALUES"
            + "(?)";
            preparedStatement = connection.prepareStatement(insertTableSQL);
            preparedStatement.setString(1, "Sandeep");
            preparedStatement.executeUpdate();
            System.out.println("RECORD INSERTED...");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                System.out.println("CONNECTION CLOSED...");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
  • Can you show me a small example with my code about name and values.Thanx – asd32 Jun 16 '16 at 10:52
  • 1
    Hi, pls change your `String sql`(Look at the `insertTableSQL` in this example and compare it with yours) first. Spend some time how to work with [PreparedStatements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html#supply_values_ps). Revert back with *your* updated code. – Sandeep Chatterjee Jun 16 '16 at 11:28