_Csandeep

Remote connect MS-Access DB using JDBC/UCanAccess API.

Jun 9th, 2015 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. /**
  2.  * Connects to a remote MS-Access DB using JDBC/UCanAccess API.
  3.  *
  4.  * ADDITIONAL JARS REQD:
  5.  *
  6.  * commons-lang-2.6.jar
  7.  * commons-logging-1.1.1.jar
  8.  * hsqldb.jar
  9.  * jackcess-2.1.0.jar
  10.  * ucanaccess-2.0.9.5.jar
  11.  *
  12.  */
  13. package miscellaneous;
  14.  
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19.  
  20. /**
  21.  * @author Sandeep Chatterjee
  22.  *
  23.  */
  24. public class ConnectRemoteDB {
  25.  
  26.     /**
  27.      * @param args
  28.      */
  29.     public static void main(String[] args) {
  30.  
  31.         initializeConnection();
  32.  
  33.     }
  34.  
  35.     /**
  36.      * Initializes remote database connection and inserts a record and closes the connection.
  37.      */
  38.     private static void initializeConnection() {
  39.  
  40.         System.out.println("Attempting Database Connection...");
  41.         Connection connection = null;
  42.         PreparedStatement preparedStatement = null;
  43.  
  44.         try {
  45.             connection = DriverManager
  46.                     .getConnection("jdbc:ucanaccess:////192.168.1.181/Prosenjit_H/TestDB.mdb");
  47.             System.out.println("CONNECTION ESTABLISHED....");
  48.  
  49.             String insertTableSQL = "INSERT INTO Table1" + "(Name) VALUES"
  50.                     + "(?)";
  51.             preparedStatement = connection.prepareStatement(insertTableSQL);
  52.             preparedStatement.setString(1, "Santanu");
  53.             preparedStatement.executeUpdate();
  54.             System.out.println("RECORD INSERTED...");
  55.  
  56.         } catch (SQLException e) {
  57.             e.printStackTrace();
  58.         } finally {
  59.             try {
  60.                 connection.close();
  61.                 System.out.println("CONNECTION CLOSED...");
  62.             } catch (SQLException e) {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.     }
  67. }
Add Comment
Please, Sign In to add comment