Sunday, 22 July 2012

Eclipse and DB2 Express-C configuration


DB2 Express-C


DB2 Express-C is database provided by IBM. It uses Standard SQL.
To work with DB2 in Eclipse, you need following settings.
First of all you must create database.
Go to db2cmd (prompt to issue commands for db2), then issue the command “db2 create database databasename”. Then you can use this database, I mean you can create tables under newly created database by using the command “db2 connect to databasename”.
Here while giving any command you have to use db2 prefix, you can avoid this by simply giving command “db2” press enter, now you can directly give the command without giving prefix db2.
Now you can create any number of tables under this database.
After you have done with creation of tables you can do following configurations in Eclipse so that you can interact with DB2.
Right click on your project folder (left side in eclipse)...click build path...click configure build path...click java build path....go to libraries....click add external JAR's...brows c:/program files/IBM/SQLLIB/java/db2java.zip......click open...OK....now go to "JDBC Connection" in the same window...select "name: your database name"...edit runtime connection details to jdbc:db2://localhost:50000/databasename...OK....this is for type 2(there are types of drivers as type1, type2, type3 and type4)....
(still if you are unable then do one thing…..after adding external jar files follow these steps……. right click on your project -> build path -> configure build path -> jdbc connections -> runtime connection details -> edit -> select use "data source connection” -> ok)
Use following statements in your code
Driver driver = (Driver) Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection conn=
DriverManager.getConnection("jdbc:db2://localhost:50000/dbname","username","password");
Statement stmt = conn.createStatement();
ResultSet result=stmt.executeQuery("query");

Now you can manipulate this resultset.
In your application you will frequently need to use these statements in many files, instead of that I would like to suggest you to create one .java file I mean java class file that will contain above code.
e.g. DBConnection.java
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;
class DBConnection
{
    public DBConnection()
       {
              Driver driver = (Driver) Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
              Connection conn=
   DriverManager.getConnection("jdbc:db2://localhost:50000/dbname","username","password");
              Statement stmt = conn.createStatement();
        }
}

Now in your programs you can create object of this class and use it.
e.g.
DBConnection conn=new DBConnection();
ResultSet result=conn.stmt.executeQuery("query");


                

2 comments:

  1. hii i wana make chatting app , can it be possible by using this?

    ReplyDelete
  2. Thanks a Lot... That was really Great... :)

    ReplyDelete