JAVA-DB Connectivity

0

Category:

Introduction


JDBC is a set of classes and interfaces written in Java that allows Java programs to access a database. IBM has several types of JDBC drivers, this how-to is refering to IBM's DB2 type 4 thin driver.

Download driver package


At the time of testing, I got a package called db2_jdbc_t4_fp13.zip from www.ibm.com. We can verify the content of the file:

jar -tf db2_jdbc_t4_fp13.zip fm@susie112:~> /usr/java/jdk1.5.0_09/bin/jar tf db2_jdbc_t4_fp13.zip db2_jdbc_t4_fp13/ db2_jdbc_t4_fp13/db2jcc.jar db2_jdbc_t4_fp13/db2jcc_javax.jar db2_jdbc_t4_fp13/db2jcc_license_cu.jar db2_jdbc_t4_fp13/sqlj.zip

Install the driver package


In order to work with the driver, Java must be able to find it when called. We can achieve this by adding the drivers zip file location to the Java classpath, or by simply placing the driver file into the Java standard directory for extensions, for JAVA JRE this is the $JAVA_HOME/jre/lib/ext (i.e. /usr/lib64/jvm/jre-1.6.0-ibm/lib/ext) directory. We need to copy the extracted db2_jdbc_t4_fp13/db2jcc.jar and db2_jdbc_t4_fp13/db2jcc_license_cu.jar.

Use the driver to access DB2 through Java


The following example code JdbcTestDB2.java can be used to quickly access and test the JDBC connection.

vi JdbcTestDB2.java //JdbcTestDB2.java import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.ResultSet ; import java.sql.Statement ; import java.sql.SQLException;  class JdbcTestDB2 {   public static void main (String args[]) {       try {         // use the JDBCtype 4 driver         Class.forName("com.ibm.db2.jcc.DB2Driver");       }       catch (ClassNotFoundException e) {             System.err.println (e) ;             System.exit (-1) ;       }       try {          Connection connection = DriverManager.getConnection(          // open connection to database          // "jdbc:db2://destinationhost:port/dbname", "dbuser", "dbpassword"          // the DB2 internal data catalog is in a database called "toolsdb"          "jdbc:db2://localhost:50002/SAMPLE", "db2inst1", "password1");            // build query, use table "ENV_INST_INFO" in schema "SYSIBM"           String query = "select INST_NAME from SYSIBMADM.ENV_INST_INFO" ;            // execute query           Statement statement = connection.createStatement () ;           ResultSet rs = statement.executeQuery (query) ;            // return query result           while ( rs.next () )               System.out.println ("DB2 Query result: " + rs.getString (1)) ;           connection.close () ;       }       catch (java.sql.SQLException e) {           System.err.println (e) ;           System.exit (-1) ;         }     } }  fm@susie112:~> javac JdbcTestDB2.java  fm@susie112:~> java JdbcTestDB2 DB2 Query result: db2inst1

Should this test fail, typical reasons are:

  • Wrong database port? DB2 typically uses ports 50000 and up
  • Wrong database user, or database user does not have admin rights
  • The test program tries to access the database "sample", it can be installed using the command 'db2sampl'

Java code to connect DB2 Type 4 driver

0

Category:

Java code to connect DB2 Type 4 driver db2jcc.jar

JAVA sample code to connect to DB2 Database using Type 4 Driver

The below sample code shows the simple way of making connection to a DB2 Server Database exists on a remote machine with out installing any db2 client on your machine. But all we need to have is Type 4 drivers to make this connection which we can use by pointing to thedb2jcc.jar and db2jcc_license_cu.jar.
All we need to do is point the java class path to find this jars, in case of eclipse users configure the build path and these 2 jars as external jars from the path they located to the project. change the Host name, port, user name and password like details in the below program and run accordingly.

Syntax for the connection string :
DriverManager.getConnection
("jdbc:db2j:net://<HOSTNAME>:/,">","");

Sample connection string :
DriverManager.getConnection
("jdbc:db2j:net://nc184120.tivlab.austin.ibm.com:50001/CDSDB","db2inst1","db2inst1");
The following is the sample code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Java2DB2 {
public static void main(String[] args)
{
try
{
// load the DB2 Driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
// establish a connection to DB2
Connection db2Conn =
DriverManager.getConnection
("jdbc:db2j:net://nc184120.tivlab.austin.ibm.com:50001/CDSDB","db2inst1","db2inst1");
// use a statement to gather data from the database
Statement st = db2Conn.createStatement();
String myQuery = "SELECT * FROM CDSSCHEMA.ACL_ADMIN";
// execute the query
ResultSet resultSet = st.executeQuery(myQuery);
// cycle through the resulSet and display what was grabbed
while (resultSet.next())
{
String name = resultSet.getString("USER_NAME");
String id = resultSet.getString("USER_ID");
System.out.println("User Name: " + name);
System.out.println("User ID: " + id);
System.out.println("-------------------------------");
}
// clean up resources
resultSet.close();
st.close();
db2Conn.close();
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}

Mastering Spring MVC - JavaWorld

1

Category:

Mastering Spring MVC - JavaWorld

How to change port for GlassFish in NetBean 7.0?

0

Category:

If you already have tomcat server installed than you might need to change the port for Glassfish port for working on NetBean 7.0. I faced this problem and here it the solutions for this.

  1. Find out the folder where GlassFish is installed.

    If you installed GlassFish along with NetBeans, you can find out the folder where GlassFish is installed by using the following procedure.

    • Select Services window by using Window -> Services in NetBeans IDE
    • Expand Servers node and select GlassFish Domain
    • Right click and select Properties option from popup menu.
    • On the right of Domains Folder you can see the folder where GlassFish is installed. For example : C:\netbeans7.0\glassfish-v3\glassfish\domains. You can also see the other details regarding Glassfish such as port number, in the same window.
  2. Go to the folder where Glassfish in installed.
  3. Go into config folder which is as follows: c:\netbeans7.0\glassfish-3\glassfish\domains\domain1\config
  4. Open domain.xml using any text editor.
  5. Look for 8080 and change it to some other port number that doesn’t conflict with other port numbers. I generally change it to 9090.
  6. Save domain.xml.
  7. Now you need to remove GlassFish from NetBeans and add it again so that NetBeans IDE understands the new port number. For this do the following
  8. :
    • In Servers window of NetBeans, remove GlassFish by using RemoveServer button after selecting GlassFish server.
    • Click on AddServer and select GlassFish V2 or GlassFish V3 and click on Next.
    • Select the Installation Location of GlassFish and click on Next.
    • Accept defaults and click on Finish.
  9. Restart GlassFish, if it was already running.

Authorization failed---DFS error

0

Category:

Steps to resolve error---

1-When you are in Composer, with your Documentum Web Service project open, change to the Web perspective. This will allow you to see the referenced DocumentumCoreProject.
2-Navigate down the DocumentumCoreProject as follows: /DocumentumCoreProject/dfs6.5/emc-dfs-sdk-6.5/etc and open the dfc.properties file.
3-Edit this file with the information pertinent to your environment:

dfc.docbroker.host[0]=my_server
dfc.docbroker.port[0]=1489
dfc.globalregistry.repository=documentum
dfc.globalregistry.username=dm_bof_registry
dfc.globalregistry.password=KmYwfPemffMsdfrfprSphRf5Lwazsdfxg\=\=


4-Save the dfc.properties file.
5-Validate and Build your Project
6-Export to an EAR file using the Composer Export Service Archive tool - i.e. right-click your custom Documentum Web Service project and select Export, then choose Documentum/Export Service Archive.