HOME

FORUM

UPLOAD SOURCE

RPGLE/RPG

CLLE

SQLRPGLE

DDS

API

OTHER

JAVA

IFS

HTML

JAVA SCRIPT

PHP

MYSQL

XML

OLE DB




    JAVA  - Get your feet wet with Java on the iSeries.
Posted By: CJFerris   Contact

Here is a Java program the will prompt you for a table name and then display its metadata. Just copy the source to a folder in the IFS system and then from QSH compile it and then run it. Before you run it, update the IP address, user id and password in the source. You can run this remotely too. In this example I created a folder called JavaDemo. > pwd /JavaDemo >javac -classpath /JavaDemo:/QIBM/ProdData/Http/Public/jt400/lib/jt400.jar JDBCMetaData.java $ >java -classpath /JavaDemo:/QIBM/ProdData/Http/Public/jt400/lib/jt400.jar JDBCMetaData Make sure the jt400.jar file is actually in the path shown above. the source: /* * JDBCMetaData.java * * Created on December 29, 2004, 3:56 PM */ /** * * @author cferris */ import java.io.*; import java.util.*; import java.net.*; import java.sql.*; /** * * @author * @version */ public class JDBCMetaData { // Create a reader to get input from the user. static BufferedReader console = new BufferedReader(new InputStreamReader(System.in),1); // Define public variables here. Connection the400 = null; Properties prop = null; DatabaseMetaData dmd = null; static String fileName = ""; static String libName = "QGPL"; // static String IPaddress = "111.111.111.111"; static String IPaddress = "xxx.xx.xxx.xx"; // static String Schema = ""; static String Schema = "QGPL"; //*********************************************************** // Define and establish connection //*********************************************************** public void setConnection () { try { // Create a properties object and set the user id and password. prop = new Properties(); prop.put("user", "your user id here"); prop.put("password", "your password here"); // Now connect the400 = DriverManager.getConnection("jdbc:as400://" + IPaddress ,prop); } catch (Exception e) { // If any of the above operations failed then print error message System.out.println ("\nError in setConnection"); System.out.println ("ERROR: " + e.getMessage()); System.out.println ("\nTerminating program"); System.exit(1); } } // end of setConnection //*************************************************************** // Define method to prompt the user for a table name. //*************************************************************** private String getName(String whatItIs, String defaultName) { // String name = ""; System.out.println(); if (defaultName.length() > 0) { System.out.println("Enter a " + whatItIs + " if different from - " + defaultName); } else { System.out.print("Enter " + whatItIs + " - "); } try { name = console.readLine(); } catch(IOException e) { // Ignore for now, this is just a demo. } // Print a line feed. System.out.println(); if (name.length() == 0) { // Return the default return defaultName; } else { return name; } } // of getName //*********************************************************** // Get DB location/system table.and other information //*********************************************************** public void getInfo () { System.out.println("In getInfo now"); try { dmd = the400.getMetaData(); } catch (Exception e) { // If any of the above operations failed then print error message System.out.println ("\nError in getInfo"); System.out.println ("ERROR: " + e.getMessage()); } } // end of getInfo //*********************************************************** // Display information //*********************************************************** public void displayInfo () { System.out.println("In display info now"); try { System.out.println(" Data base : " + dmd.getDatabaseProductName()); System.out.println(" Version : " + dmd.getDatabaseProductVersion()); System.out.println(" Catalog term: " + dmd.getCatalogTerm()); System.out.println(" Catalog : " + the400.getCatalog()); // ResultSet resultSet = dmd.getTables("System","Library","%",null); ResultSet resultSet = dmd.getColumns(the400.getCatalog(), libName, fileName,"%"); ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); int columnCount = resultSetMetaData.getColumnCount(); if (columnCount == 0) { System.out.println("\nNo columns returned."); } else { System.out.println("\nGot " + columnCount + " columns"); for (int j = 1; j <= columnCount; ++j) { System.out.println(j + " Catalog: " + resultSetMetaData.getCatalogName(j)); System.out.println(j + " Table : " + resultSetMetaData.getTableName(j)); System.out.println(j + " Schema : " + resultSetMetaData.getSchemaName(j)); System.out.println(j + " Column : " + resultSetMetaData.getColumnName(j)); } System.out.println("\nPrinting rows now for " + fileName); while (resultSet.next ()) { for (int i = 1; i <= columnCount; ++i) { String value = resultSet.getString(i); if (resultSet.wasNull ()) value = ""; System.out.print(value + " "); } // end of for System.out.println(""); } // end of while } } catch (Exception e) { // If any of the above operations failed then print error message System.out.println ("\nError in displayInfo"); System.out.println ("ERROR: " + e.getMessage()); } } // end of displayInfo //*********************************************************** // Close the connection method. //*********************************************************** public void closeConnection () { if (the400 != null) { try { the400.close(); System.out.println(""); System.out.println("Connection closed"); } catch(SQLException e) { System.out.println ("\nError trying to close connection"); System.out.println ("ERROR: " + e.getMessage()); } } } // end of closeConnectoin //*********************************************************** // Define the constructor. //*********************************************************** private JDBCMetaData () { System.out.println("Setting driver"); try { // Set driver DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ()); this.setConnection(); } catch (Exception e) { // If any of the above operations failed then print error message System.out.println ("\nError in constructor"); System.out.println ("ERROR: " + e.getMessage()); } } // end of constructor //*********************************************************** // Begin main method/program //*********************************************************** public static void main(String args[]) { System.out.println("In JDBCMetaData now"); JDBCMetaData metaData = new JDBCMetaData(); try { // Get a file name if (args.length > 0) fileName = args[0]; // Prompt user for a table name. libName = metaData.getName("library name", libName); // Prompt user for a table name. fileName = metaData.getName("table name", fileName); // inputStream.readLine(); System.out.println("Making connection"); metaData.getInfo(); metaData.displayInfo(); // DatabaseMetaData dmd = the400.getMetaData (); } catch (Exception e) { // If any of the above operations failed then print error message System.out.println ("\nError in main"); System.out.println ("ERROR: " + e.getMessage()); } metaData.closeConnection(); System.exit(0); } // end of main } // end of class


    JAVA  - Getting started with DB2 access from Java for ILE programmers
Posted By: CJFerris   Contact

Connecting to a DB2 table or native as400 from Java - There are two ways to do this: 1) Using JDBC which is basically SQL for Java. For “select” statements you simply get a ResultSet object which you can loop through for each record/row. 2) Using record level access using IBM's Java classes. This requires creating objects for each field and for each record format. However it does offer methods similar to chain, read, update and write. For me this requires jumping through to many hoops, plus it’s is distinctly for DB2 tables only. I prefer to use JDBC because it is much easier, plus it’s universal. By simply changing the database driver you can connect to any SQL database, such as MySQL. Before getting started with a Java JDBC program for processing DB2 files there are a few assumptions and steps I’d like to cover first. a) Java and its toolkit has been loaded on your AS400, iSeries, i5 or what ever new name IBM will think of next. b) All development, unit testing and debug is initially done on a remote PC using Eclipse, NetBeans or Websphere. (or even TextPad). Eventually your Java code can end up running on the AS400 in the IFS system and QSH environment, but as far as I know, there is no good IDE that runs directly over the IFS system on the AS400. I use NetBeans, it’s free and does just about everything, including JSP and Web apps. c) You need the jt400.jar file in your class path which means you need to copy it to your local hard drive. This has all the classes and drivers needed to connect to the AS400 for just about everything. Warning – it is big. Use Navigator or FTP to copy it down. You can find it in the IFS system on your 400 and the path maybe different for various releases. For release 5.2 you can find it in /QIBM/ProdData/Http/Public/jt400/lib/jt400.jar And while your at it, copy db2_classes.jar too. You should be able to find it in /QIBM/ProdData/Java400/ext/db2_classes.jar I’ll discuss this jar file later, but might as well copy it now while you’re copying the other. After you have copied the jar files you need to add them you your classpath. Do this within your IDE under your current project. There should be options under properties to add jar files. Just take that option and point it to where you copied them to. d) CLASSPATH – Think of the classpath as a combination of library list and binding directory, but a little trickier to get use to. When you add a jar file to your classpath you’re basically binding it to your project. When you add a directory to your classpath you’re basically adding a library to your library list. e) Making the connection: You will need the following to make the connection to the AS400. a. IP address b. User profile c. Password d. Schema – Think of the schema as the library your files/table are in. Otherwise you have to qualify them in the SQL statements. e. File/tables to query. Note: the user profile and password are only needs when connecting remotely from your PC. When running on the 400 you are already logged in so you do not need to set them. There is a Boolean method in the AS400 class called “isLocal”. I use this in my Java code to determine when and if I need to set the user id and password. I also use this to determine which database driver to use. If running remotely I use “jt400.jar”, if running local (on the 400) I use “db2_classes.jar”. Why “db2_classes.jar” you ask?. It is lighting fast compared to “jt400.jar” however it does not worked remotely. You need the jar on your workstation to compile the Java code but it will not work. At least I could not get it to work. Also the increased in speed is mostly realized with insert and updates, not so much for select statements. For example: I’ve written a multi-thread Java program that takes incoming xml documents and then writes data out to a set of DB2 table. Using db2_classes.jar it can insert 1,500 to 2,000 records per second. Using jt400.jar is like 400 records per second. f) The AS400 class vs. Connection class. These are two classes that can be used to connect to the AS400 and each one has a different purpose. In my example I use both. The Connection class is used just for JDBC and running sql statements. The AS400 class is used for everything else like calling ILE programs, access to Data Queues and Data Areas and other AS400 specific functions. Each one requires a user id and password to connect. If all you are running is sql then you do not need the AS400 class. However I use it primarily for the “isLocal” method. Plus there is a method for validating user profiles too. g) Steps needed to connect and run a sql statement. a. Create new objects for AS400 and Connection (AS400 is optional for sql) b. Create a new properties object and set user id, password and schema for it. c. Establish the connection to the database using the connection, database driver and properties objects. d. Query the table/file i. Prepare an sql statement ii. Execute the sql statement which creates a ResultSet object. iii. Process the results and then close the cursor e. Close the connection(s). Take the JDBC sample program below and copy it into your IDE. There are constants in this program where you put your settings like ip address, user id, password, schema and the sql select statement. The steps described above are grouped into three subroutines. This example will list the records to the console. Give it a try. Note: If you did not load the db2_connection.jar file then rem out the reference to it. /* * MyAS400jdbc.java * * Created on June 15, 2005, 1:25 PM */ import java.io.*; import java.util.*; import java.net.*; import java.sql.*; // Import the i5 tool kit. import com.ibm.as400.access.*; public class MyAS400jdbc { // Define instance fields private AS400 server = null; private Connection connection = null; private Properties prop = null; // Define the constants private static final String IP_ADDRESS = "what ever it is"; private static final String USER_ID = "what ever it is"; private static final String PASSWORD = "what ever it is"; private static final String SCHEMA = "what ever it is"; private static final String QUERY = "select * from yourfile"; /** Creates a new instance of MyAS400jdbc */ public MyAS400jdbc() { } //*********************************************************** // Define method to set items in the optional connection // properties object. //*********************************************************** public Properties setProperties(boolean local) { // Create a properties object and if this in NOT // running on the local system then user id and password // to skip the sign-on prompting.. Properties prop = new Properties(); prop.put("naming", "system"); prop.put("errors", "full"); prop.put("libraries", SCHEMA + ", *LIBL"); if (!local) { prop.put("user", USER_ID); prop.put("password", PASSWORD); } return prop; } // serverProperties //*********************************************************** // Define method make the connection //*********************************************************** public void makeConnection() { // Create a server sign-on object to manage a set of socket // connections to the iSeries, and to set sign-on or connection // behavior. server = new AS400(IP_ADDRESS); // Setup the host connection // Next see if this is running locally on // the AS400. If not set login information. if (!server.isLocal()) { try { System.out.println("Program is running remotely to the server/iSeries."); server.setUserId(USER_ID); server.setPassword(PASSWORD); // Start services. server.connectService(AS400.DATABASE); } catch(Exception bummer) { System.out.println("Error: Creating a AS400 connection object failed: " + bummer.getMessage() + " - Program is terminating."); System.exit(3); } } // Next steps are defining the driver and then // creating the connection with it. The driver // and connection can be set in one of two ways: // 1) native JDBC or 2) AS400 driver. if (server.isLocal()) { // Connect using native JDBC drivers try { // Use the native JDBC driver. DriverManager.registerDriver(new com.ibm.db2.jdbc.app.DB2Driver()); prop = setProperties(server.isLocal()); // Now connect connection = DriverManager.getConnection("jdbc:db2://" + IP_ADDRESS, prop); System.out.println("Connection established using the JDBC native driver(s)"); } catch(Exception jdbc) { System.out.println("Error setting driver and connection using " + "JDBC native driver. The error message is as follows: " + jdbc.getMessage()); System.exit(2); } } else { // Connect using AS400 drivers try { // Use the AS400 driver. DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); prop = setProperties(server.isLocal()); // Now connect connection = DriverManager.getConnection("jdbc:as400://" + IP_ADDRESS, prop); System.out.println("Connection established using the AS400 driver(s)"); } catch(Exception as400) { System.out.println("Error setting driver and connection using " + "the AS400 drivers. The error message is as follows: " + as400.getMessage()); System.exit(2); } } } // of connectionToDataBase //*********************************************************** // This method will execute the SQL select statement // and then print the records to the screen. //*********************************************************** public void processSQL() { // Trap the SQL exceptions. try { // Prepare the statement. PreparedStatement select = connection.prepareStatement(QUERY); // Now call the method with the prepared statement. ResultSet qryResult = select.executeQuery(); if (qryResult == null) { System.out.println("result is null"); return; } // Get the column count to the table we just queried. int columnCount = qryResult.getMetaData().getColumnCount(); System.out.print("\n"); // Now loop through the rows. while (qryResult.next()) { // Loop though all the columns for this record. for (int j = 1; j <= columnCount; j++) System.out.print(qryResult.getString(j) + " "); // End of record, start a new line. System.out.print("\n"); } // Done, close the cursor. qryResult.close(); } catch(SQLException nuts) { System.out.println("Error trying to prepare and run a select statement. " + "The following is the error message " + nuts.getMessage()); } // Done } // of processSQL //*********************************************************** // Define method close the connection(s) //*********************************************************** public void closeConnection() { // Try closing the iSeries connection. if (connection != null) { try { server.disconnectAllServices(); connection.close(); System.out.println("Connection closed"); } catch(Exception ugh) { System.out.println("Error trying to close connection(s): " + ugh.getMessage()); } } } // of closeConnection //*********************************************************** // Begin main method/program //*********************************************************** public static void main(String args[]) { System.out.println("In MyAS400jdbc now"); MyAS400jdbc iSeries = new MyAS400jdbc(); iSeries.makeConnection(); iSeries.processSQL(); iSeries.closeConnection(); System.exit(0); } // end of main }


    JAVA  - rpg
Posted By: dave blais   Contact

i need a bot please

About Code400.com | resume | Search | Site Map | Suggestions
© Copyright 2003-2008 Code400.com



Thursday Sep 09, 2010 @ 9:15 PM