Java odbc in translation to do table lookup

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Java odbc in translation to do table lookup

  • Creator
    Topic
  • #53814
    John Hamilton
    Participant

      Server AIX 6.0

    Viewing 1 reply thread
    • Author
      Replies
      • #79030
        bill bearden
        Participant

          This should get you started. I copied (and modified) SampleXLT from the CL 6.0 documentation. The first import statement had to be modified and I had to change “CLOVERLEAFException” to “CloverleafException” to get it to compile. I think that is all I had to fix. After that, I added the code to access a table in a MS SQL Server database. This is a DSN we have set up on our test server (Windows Server 2008) in ODBC today. We use Tcl to access this database connection, not Java.

          Since you specified ODBC, the connection in the example uses the JDBC-ODBC Bridge. (But if you have ODBC, why not use Tcl?) There is also an option to make the connection without ODBC if you have the native, type-4, JDBC driver for the database you are connecting to.

          Make sure the compiled .class file is in one of the java_uccs directories. I used one of the site-specific java_uccs dirs since I was just testing. Then you can just click the Java radio button on a Copy operation in an xlate, click the edit button, and enter the name of your class. The input value should be what gets looked up and the output value should be what comes out.

          Oh, yeah. You’ll have to change ODBCDSNString to whatever your DSN is. And you’ll have to change colName, tableName, and otherColName. Hope this helps.

          Code:

          import com.quovadx.cloverleaf.upoc.*;
          import java.sql.*;

          public class SampleJDBCXLT extends XLTString
          {
          public String xlateString(CloverEnv env, Xpm xpm, String inVal)
             throws CloverleafException
             {
                 System.out.println(”inVal=” + inVal);
                 String result = null;
                 try {
                     String connString = “jdbc:odbc:ODBCDSNString”;
                     Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);
                     Connection conn = DriverManager.getConnection(connString, “userName”, “password”);
                     Statement st = conn.createStatement();
                     String select = “SELECT colName FROM tableName WHERE otherColName=” + inVal;
                     ResultSet rs = st.executeQuery(select);
                     rs.next();
                     result = rs.getString(”colName”);
                     System.out.println(result);
                     conn.close();
                 } catch (Exception e) {};

                 return result;
             }
          }

        • #79031
          John Hamilton
          Participant

            Thank You.

            I’m just now realizing how much this is really going to take.

            But that just gives me lots to learn.  ðŸ˜€

        Viewing 1 reply thread
        • The forum ‘Cloverleaf’ is closed to new topics and replies.