mallett76 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, the below code works in Java, but it is not working using Inline Java, was wondering why

Below is the code. Any ideas or suggestions would be appreciated

use Inline Java => <<'END_OF_JAVA_CODE' ; import java.sql.*; public class T20000JD { // Name of the user able to create, drop, and manipulate tables public static String sUser = "xxxxx"; public static String sPassword = "xxxxxx"; public static void main(String args[]) throws ClassNotFoundException { // Creation of URL to be passed to the JDBC driver //commented out on 12/19/23 String url = "jdbc:teradata://whom +ooz/TMODE=ANSI,CHARSET=UTF8"; //commented out on 12/21/23 String url = "jdbc:teradata:// +172.28.130.20/LOGMECH=LDAP"; String url = "jdbc:teradata://000.00.000.00/TMODE=TERA,DBS_PORT=1025, +CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; // Statements used in table creation String sDropTbl = "DROP TABLE NED_COLL_TABLES.T_PM_TEST_TMP"; String sCreateTbl = "CREATE TABLE NED_COLL_TABLES.T_PM_TEST_TM +P (empID INTEGER NOT NULL, " + "empName VARCHAR(30) NOT NULL, empDept VARCHAR(50) NOT NUL +L, " + "empJob VARCHAR(300), PRIMARY KEY(empID))"; // Statements used in index creation. Both unique and non-uniq +ue indexes // will be created. Please note that these may not result in o +ptimal // performance. String sCreateIdx = "CREATE INDEX (empName) ON employee"; String sCreateIdx2 = "CREATE UNIQUE INDEX (empName, empDept) O +N employee"; try { System.out.println("\n Sample T20000JD: \n"); System.out.println(" Looking for the Teradata JDBC driver. +.. "); // Loading the Teradata JDBC driver Class.forName("com.teradata.jdbc.TeraDriver"); System.out.println(" JDBC driver loaded. \n"); // Attempting to connect to Teradata System.out.println(" Attempting to connect to Teradata via +" + " the JDBC driver..."); // Creating a Connection object. A Connection represents a + session // with a specific database. Within the context of a Conne +ction, // SQL statements are executed and results are returned. // Creating a database connection with the given database +URL, // user name, and password Connection con = DriverManager.getConnection(url, sUser, s +Password); System.out.println(" User " + sUser + " connected."); System.out.println(" Connection to Teradata established. \ +n"); try { // Creating a statement object from an active connecti +on. // A Statement object is used for executing a static S +QL // statement and obtaining the results produced by the + statement. // Statement.executeUpdate method is used to execute a + SQL // INSERT, UPDATE or DELETE statement. SQL statements +that // return nothing, such as DDL statements, can be exec +uted // as well. The method will return the row count for I +NSERT, // UPDATE, or DELETE, or zero for statements that retu +rn nothing. Statement stmt = con.createStatement(); System.out.println(" Statement object created. \n"); try { // Cleanup procedure: // If the sample table already exists, drop it. // Please note that the "no table present" excepti +on // will be raised if and only if no table "employe +e" already // exists, as during the very first execution of t +his // example. If a user wants to replicate this erro +r multiple // times, the table will have to be manually remov +ed before // running this example again in order for the err +or to // occur. try { System.out.println(" Dropping table if present +: " + sDropTbl); // Executing the drop table command stmt.executeUpdate(sDropTbl); System.out.println(" Table dropped.\n"); } catch (SQLException ex) { // If the table did not exist, no drop is requ +ired. // Ignore the raised "no table present" except +ion by // printing out the error message and swallowi +ng the // exception. If multiple exceptions are chain +ed // together, display information on all of the +m. // The following code will demonstrate some of + the // methods available for retrieving informatio +n about // SQLExceptions, some inherited from the Thro +wable // class. Please refer to JDBC API for other // method descriptions. while (ex != null) { // Display a short description of the exce +ption. // Returns information generated by the to +String() // method: a concatenation of three string +s: // - Name of the actual class of this obje +ct // - ": " (a colon and a space) // - Result of the getMessage() method for + this // object System.out.println("\n Drop table exceptio +n " + "ignored: " + ex); // Display vendor-specific exception code +for this // SQLException object. System.out.println(" Error code: " + ex.getErrorCode()); // Display the SQLState for this SQLExcept +ion object. System.out.println(" SQL State: " + ex.getSQLState()); // Display the detail message string, if a +ny. System.out.println(" Message: " + ex.getMessage()); // Display the localized description of th +is // exception, if any. Default implementati +on returns // the same result as getMessage(). System.out.println(" Localized Message: " + ex.getLocalizedMessag +e()); // Print this error and its backtrace to t +he // standard error stream. System.out.println(" Stack Trace: "); ex.printStackTrace(); // Retrieve the exception chained to this // SQLException object, if any. ex = ex.getNextException(); } System.out.println(" Table could not be droppe +d." + " Execution will continue.. +.\n"); } // Create the sample table System.out.println(" Creating table: " + sCreateTb +l); stmt.executeUpdate(sCreateTbl); System.out.println(" Sample table created. \n"); // Create table indexes System.out.println(" Creating table indexes: " + sCreateIdx + " " + sCreateIdx +2); stmt.executeUpdate(sCreateIdx); stmt.executeUpdate(sCreateIdx2); System.out.println(" Table indexes created."); } finally { // Close the statement stmt.close(); System.out.println("\n Statement object closed. \n +"); } } finally { // Close the connection System.out.println(" Closing connection to Teradata... +"); con.close(); System.out.println(" Connection to Teradata closed. \n +"); } System.out.println(" Sample T20000JD finished. \n"); } catch (SQLException ex) { // A SQLException was generated. Catch it and display // the error information. // Note that there could be multiple error objects chained // together. System.out.println(); System.out.println("*** SQLException caught ***"); while (ex != null) { System.out.println(" Error code: " + ex.getErrorCode() +); System.out.println(" SQL State: " + ex.getSQLState()); System.out.println(" Message: " + ex.getMessage()); ex.printStackTrace(); System.out.println(); ex = ex.getNextException(); } throw new IllegalStateException ("Sample failed.") ; } } // End main } // End class T20000JD END_OF_JAVA_CODE

Replies are listed 'Best First'.
Re: Inline Java not working
by stevieb (Canon) on Dec 28, 2023 at 19:55 UTC

    "not working" is exceptionally vague. That's very specific to your environment Java code that we won't be able to test, so you must explain to us exactly what you expect to happen, and more importantly, what problems are occurring, including error messages.

    Also, the post will be much easier to follow if you remove all of the commented lines.

      I have updated my code. Unfortunately, I have not been able to get this program to run. However, at least I now have an error message

      I am receiving the following error message:

        main::java::lang::ClassNotFoundException=HASH(0x33f4400)

      Here is my updated code

      use strict; use warnings; use Inline Java => <<'END'; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class TeradataTableCreator { public static void createTable(String jdbcUrl, String username, Stri +ng password) throws Exception { // Load Teradata JDBC driver Class.forName("com.teradata.jdbc.TeraDriver"); // Establish the connection Connection connection = DriverManager.getConnection(jdbcUrl, usern +ame, password); // Create a Statement Statement statement = connection.createStatement(); // Execute the SQL statement to create a sample table String createTableSQL = "CREATE TABLE SampleTable (ID INT, Name VA +RCHAR(255))"; statement.executeUpdate(createTableSQL); // Close resources statement.close(); connection.close(); } } END # Specify your Teradata connection details my $jdbcUrl = "jdbc:teradata://000.00.000.00/TMODE=TERA,DBS_PORT=1025, +CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; my $username = "XXXXX"; my $password = "XXXXXX"; # Call the Java method to create the table TeradataTableCreator->createTable($jdbcUrl, $username, $password);

      Will look into posting with cleaner code

      However, when I say that it is not working, it is not doing what it is designed to do, which is to create a table - and this same code in JAVA does create a table fine

      The perl script does not yield any error messages

      However, as I'm typing this response, I noticed that it doesn't have a "strict" in the beginning of the code, so I'll look into adding and re-testing to help trouble shoot

      Yahoo! - I solved the problem - wasn't easy, but I did it. I'll share my code in case anyone else has the issue. The big item was I had to point to my class path jar file to point to terajdbc4.jar

      use strict; use warnings; use Inline Java => <<'END' => CLASSPATH => 'C:/jars/terajdbc4.jar'; import java.sql.Connection; import java.util.*; import java.sql.DriverManager; import java.sql.SQLException; import java.io.PrintWriter; import java.sql.*; public class TeradataConnectionTest { public static void testConnection(String jdbcUrl, String username, Str +ing password) { try { System.out.println("Test up to part 1"); //e.printStackTrace(); //DriverManager.registerDriver(new com.teradata.jdbc.TeraDriver()); //DriverManager.registerDriver(new com.teradata.jdbc.TeraDriver()); Di +d not work Class.forName("com.teradata.jdbc.TeraDriver"); //Did not work // Class.forName("com.ncr.teradata.TeraDriver"); Did not work Connection connectionA = DriverManager.getConnection(jdbcUrl, username +, password); System.out.println("Test up to part 2"); System.out.println("Connected to Teradata!"); // Perform your database operations here connectionA.close(); } catch (SQLException e) { // Print the stack trace or error message e.printStackTrace(); System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { // Handle other exceptions e.printStackTrace(); } } } END # Your Teradata connection details #COMMENTED OUT ON 2/14/24 my $jdbcUrl = "jdbc:teradata://XXX.XX.XXX.XX +/TMODE=TERA,DBS_PORT=1025,CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; #Here is my JAVA connection String url = "jdbc:teradata://XXX.XX.XXX. +XX/TMODE=TERA,DBS_PORT=1025,CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; #commented out on 2/16/24 my $jdbcUrl = "jdbc:teradata://XXX.XX.XXX.XX +/TMODE=TERA,DBS_PORT=1025,CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW;Ter +aDriver=com.teradata.jdbc.TeraDriver"; my $jdbcUrl = "jdbc:teradata://XXX.XX.XXX.XX/TMODE=TERA,DBS_PORT=1025, +CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; #my $jdbcUrl = "jdbc:teradata://XXX.XX.XXX.XX/TMODE=TERA,DBS_PORT=1025 +,CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; my $username = "XXXXX"; my $password = "XXXXXX"; # Call the Java method to test the connection TeradataConnectionTest->testConnection($jdbcUrl, $username, $password) +;

      I removed the comments to make it easier to read.

      Here is the updated code.

      use Inline Java => <<'END_OF_JAVA_CODE' ; import java.sql.*; public class T20000JD { // Name of the user able to create, drop, and manipulate tables public static String sUser = "XXXXX"; public static String sPassword = "XXXXX"; public static void main(String args[]) throws ClassNotFoundException { String url = "jdbc:teradata://172.28.130.20/TMODE=TERA,DBS_PORT=1025, +CHARSET=UTF8,LOGMECH=LDAP,SSLMODE=ALLOW"; String sDropTbl = "DROP TABLE NED_COLL_TABLES.T_PM_TEST_TMP"; String sCreateTbl = "CREATE TABLE NED_COLL_TABLES.T_PM_TEST_TM +P (empID INTEGER NOT NULL, " + "empName VARCHAR(30) NOT NULL, empDept VARCHAR(50) NOT NUL +L, " + "empJob VARCHAR(300), PRIMARY KEY(empID))"; String sCreateIdx = "CREATE INDEX (empName) ON employee"; String sCreateIdx2 = "CREATE UNIQUE INDEX (empName, empDept) O +N employee"; try { System.out.println("\n Sample T20000JD: \n"); System.out.println(" Looking for the Teradata JDBC driver. +.. "); Class.forName("com.teradata.jdbc.TeraDriver"); System.out.println(" JDBC driver loaded. \n"); System.out.println(" Attempting to connect to Teradata via +" + " the JDBC driver..."); Connection con = DriverManager.getConnection(url, sUser, s +Password); System.out.println(" User " + sUser + " connected."); System.out.println(" Connection to Teradata established. \ +n"); try { Statement stmt = con.createStatement(); System.out.println(" Statement object created. \n"); try { try { System.out.println(" Dropping table if present +: " + sDropTbl); // Executing the drop table command stmt.executeUpdate(sDropTbl); System.out.println(" Table dropped.\n"); } catch (SQLException ex) { while (ex != null) { System.out.println("\n Drop table exceptio +n " + "ignored: " + ex); System.out.println(" Error code: " + ex.getErrorCode()); System.out.println(" SQL State: " + ex.getSQLState()); System.out.println(" Message: " + ex.getMessage()); System.out.println(" Localized Message: " + ex.getLocalizedMessag +e()); System.out.println(" Stack Trace: "); ex.printStackTrace(); ex = ex.getNextException(); } System.out.println(" Table could not be droppe +d." + " Execution will continue.. +.\n"); } System.out.println(" Creating table: " + sCreateTb +l); stmt.executeUpdate(sCreateTbl); System.out.println(" Sample table created. \n"); System.out.println(" Creating table indexes: " + sCreateIdx + " " + sCreateIdx +2); stmt.executeUpdate(sCreateIdx); stmt.executeUpdate(sCreateIdx2); System.out.println(" Table indexes created."); } finally { stmt.close(); System.out.println("\n Statement object closed. \n +"); } } finally { System.out.println(" Closing connection to Teradata... +"); con.close(); System.out.println(" Connection to Teradata closed. \n +"); } System.out.println(" Sample T20000JD finished. \n"); } catch (SQLException ex) { System.out.println(); System.out.println("*** SQLException caught ***"); while (ex != null) { System.out.println(" Error code: " + ex.getErrorCode() +); System.out.println(" SQL State: " + ex.getSQLState()); System.out.println(" Message: " + ex.getMessage()); ex.printStackTrace(); System.out.println(); ex = ex.getNextException(); } throw new IllegalStateException ("Sample failed.") ; } } // End main } // End class T20000JD END_OF_JAVA_CODE
Re: Inline Java not working
by InfiniteSilence (Curate) on Dec 29, 2023 at 03:41 UTC

    I don't get why you even want to do this with Java at all.

    The code is just dropping a table if it exists but methinks most modern databases have a nice feature whereas you can say,

    DROP table xzy IF EXISTS ...
    

    I don't know if your db has such a feature but I would check. This makes it so you can avoid all of the error handling stuff.

    Next, the code just creates a table. Pardon me for stating the obvious but this reads a lot like, "hey, somebody said I had to use Perl but since I hate to learn new stuff I'm just gonna wrap my Java stuff..."

    If so that is sad. Perl has much more to offer you in exchange for minimal effort.

    Celebrate Intellectual Diversity

      This code isn't my end goal. This code was only being used as a test script. If successful, I was going to move to more complex scripts.

Re: Inline Java not working
by karlgoethebier (Abbot) on Dec 30, 2023 at 05:58 UTC

    See also

    «The Crux of the Biscuit is the Apostrophe»

      FYI, you don't need to write [href://http://..., you can just put [http://.... TIA.

        According to the docs the syntax is right. - href only does not work.

        «The Crux of the Biscuit is the Apostrophe»

Re: Inline Java not working
by Bod (Parson) on Jan 02, 2024 at 23:10 UTC
    Any ideas or suggestions would be appreciated

    Don't use Java for this...
    Instead, use the very capable DBI module.

    I'm genuinely curious why you are trying to do this in this way...

      I was having issues getting the DBI module to work with JDBC and Teradata. I thought that Java could be used as alternative.

        According to this source, there is a Teradata DBI driver that "is now a standalone XS module; Inline::C no longer required"

Re: Inline Java not working
by bliako (Abbot) on Jan 03, 2024 at 11:54 UTC
    Class.forName("com.teradata.jdbc.TeraDriver");

    is this failing? can it be found? Also, does the program fails at compilation or runtime?

    Edit: It looks there is at least 1 Perl DB Driver Module (very old) on CPAN offering connection to Teradata. There is another deprecated offering a newer version outside CPAN. I would investigate this route rather than inling all code to JAVA. Limited mileage but the first thing to investigate.

    Edit: note to fellow monks: teradata db access needs a proprietary driver just as mysql, sqlite et al. do. The only hope on this front is if Teradata::SQL can be made to work.

      teradata db access needs a proprietary driver just as mysql, sqlite et al. do.

      I'm not sure what you mean by this. Both MySQL and SQLite can be accessed perfectly well using open drivers - no requirement for proprietary ones.


      🦛

        I mean Perl's DBD/DBI will not provide access to a database without the driver, any driver, opensource or otherwise. 'proprietary' is the wrong word, I mean supplied externally.