Connecting to 4D from Java
Before you can connect your Java application to 4D by using the JDBC-ODBC Bridge, you need to configure a 4D ODBC driver data source. An ODBC data source stores the connection details for the target database (in this case, 4D) and the ODBC driver that is required to connect to it (in this case, the 4D ODBC driver).
- Start ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:
%windir%\syswow64\odbcad32.exe
- In the ODBC Data Source Administrator, choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose the 4D ODBC Driver, and then choose Finish.
- Complete the 4D ODBC Driver configuration dialog box.
- Download the Easysoft JDBC-ODBC Bridge.
- Install and license the Easysoft JDBC-ODBC Bridge on the machine where you created the data source.
For installation instructions, refer to the Easysoft JDBC-ODBC Bridge documentation.
- Copy
EJOB.jar
to a machine where you have a Java Development Kit installed.If the Easysoft JDBC-ODBC Bridge is already installed on this machine, skip this step.
EJOB.jar
is installed in the following location on the machine where you install the Easysoft JDBC-ODBC Bridge:easysoft_install\Jars
The default location for easysoft_install is
drive:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge
. - Add this Java code to a file named
ConnectTo4D.java
:import easysoft.sql.*; import java.sql.*; import java.util.Calendar; import java.util.Date; public class ConnectTo4D { public static void main(String[] args) throws Exception { String[] szTableTypes; szTableTypes = new String[100]; String[] szTableNames; szTableNames = new String[1000]; int[] szTypeCount; szTypeCount = new int[100]; int intTableTypes; try { Properties p = System.getProperties (); p.put ("jdbc.drivers", "easysoft.sql.jobDriver"); System.setProperties (p); String jobUrl= "jdbc:easysoft://localhost/My4DSystemDSN:logonuser=mywindowsuser:logonpassword=mywindowspassword"; Connection con = DriverManager.getConnection(jobUrl, "my4Duser", "my4Dpassword"); System.out.println(" "); System.out.println("------------------ Method Summary ------------------"); try { int i; boolean bl; String st; DatabaseMetaData dbMD = null; dbMD = con.getMetaData(); //------------------------------------------------------------------------ try { ResultSet rt = dbMD.getTableTypes(); intTableTypes=0; st=""; while (rt.next()) { szTableTypes[intTableTypes]=rt.getString(1); intTableTypes++; } for (i=0; i<intTableTypes; i++) { st=st + szTableTypes[i] + "; "; } System.out.println("Table types (" + intTableTypes + ") : "+ st ); // Go get the tables try { int x; ResultSet rs = dbMD.getTables( null, null, "%", null ); while (rs.next()) { for (x=0; x<intTableTypes; x++) { System.out.println("TABLE_CAT : " + rs.getString(1)); System.out.println("TABLE_SCHEM : " + rs.getString(2)); System.out.println("TABLE_NAME : " + rs.getString(3)); System.out.println("TABLE_TYPE : " + rs.getString(4)); System.out.println("REMARKS : " + rs.getString(5)); System.out.println("------------------------------------------------------"); } } } catch (Exception e) { System.out.println("getTables error : " + e ); } } catch (Exception e) { System.out.println("getTableTypes error : " + e ); } //------------------------------------------------------------------------ } catch(Exception e) { System.out.println("db Meta Data exception: " + e); e.printStackTrace(); } } catch(Exception e){ System.out.println("Java code error : " + e); } } }
- In a command window, add
EJOB.jar
to the Java class path. For example:set CLASSPATH="%CLASSPATH%;C:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge\Jars\EJOB.jar"
cd
to the directory whereConnectTo4D.java
is located, and compile and run the Java file. For example:"C:\Program Files\Java\jdk1.8.0_144\bin\javac.exe" ConnectTo4D.java java ConnectTo4D