Tuesday, March 20, 2012

Connecting to Database

What are the steps involved for making a connection with a database or how do you connect to a database?

a) Loading the driver : To load the driver, Class. forName() method is used. Class. forName(”sun. jdbc. odbc. JdbcOdbcDriver”); When the driver is loaded, it registers itself with the java. sql. DriverManager class as an available database driver.

b) Making a connection with database: To open a connection to a given database, DriverManager. getConnection() method is used. Connection con = DriverManager. getConnection (”jdbc:odbc:somedb”, “user”, “password”);

c) Executing SQL statements : To execute a SQL query, java. sql. statements class is used. createStatement() method of Connection to obtain a new Statement object. Statement stmt = con. createStatement(); A query that returns data can be executed using the executeQuery() method of Statement. This method executes the statement and returns a java. sql. ResultSet that encapsulates the retrieved data: ResultSet rs = stmt. executeQuery(”SELECT * FROM some table”);

d) Process the results : ResultSet returns one row at a time. Next() method of ResultSet object can be called to move to the next row. The getString() and getObject() methods are used for retrieving column values: while(rs. next()) { String event = rs. getString(”event”); Object count = (Integer) rs. getObject(”count”);

Interface

Can you have a private variable/methods defined in an interface ?
No ( there is no point in defining a private variable/methods in the interface)

Can an interface have an abstract method defined ?
YES

Can a method in an interface be static or final
NO

Can a variable in an interface be static
YES

Setting up TOMCAT on Unix

How To Install Apache Server on your Unix Account

Following are steps to install Apachee Server in your area and you get the chance to become your own server administrator

Step 1: visit http://www.apache.org/

Step 2: Click on Apache Server (seventh item from the left).

Step 3: Click on Download.

Step 4: Click on apache_1.3.28.tar.z this the 12th item from the left and it is for Unix. Remember the place where you saved it on your PC. For the sake of explanation assume that you saved it on the desk_top. Thus on your desk_top now you have apache_1.3.28.tar.z . Notice z tells that the file is zipped and tar tells that the file is an archieve file contains more than one file.

Step 5: log in into your Unix account.

Step 6: type the following two commands to create apache directory and to make it to be the current working directory

mkdir apache

cd apache

Step 7: Use an FTP facility such as CuteFTP 32 or lite Ftp to transfer apache_1.3.28.tar.z from the desk_top to your directory in Unix. To achieve that do the following items:

a. click on the FTP facility

b. Login into your Unix account on FTP,.

c. Click on apache directory in your home directory (in FTp).

d. Click on C: then click on WINDOWS, click on desk_top, click on apache_1.3.28.tar.z on some FTP

facilities you also have to click on -->> to transfer it from desk top to your account. Now you have

apache_1.3.28.tar.z in apache directory on your home directory. Close the FTP facility.

Step 8: Make sure you are at apache directory by typing pwd command. If you are not at apache directory type

command cd apache.

Step 9: To unzip the file issue the command

gunzip apache_1.3.28.tar.z

Now the file is unzipped and if you type ls command you see you have only apache_1.3.28.tar file in apache

directory.

Step 10: To extract files from apache_1.3.28.tar type the following command:

tar xvf apache_1.3.28.tar

Notice xvf are options to tar command.

Step 11: Since files are extracted from apache_1.3.28.tar, now delete apache_1.3.28.tar file by typing:

rm apache_1.3.28.tar

Step 12: type the following command

Change directory to Apache_1.3.28 by executing

cd Apache_1.3.28

Now execute the command

./configure --prefix=/usr/users/USERDIR/apache

Notice replace USERDIR by your home director. Thus this command for user ks6241 will be:

./configure --prefix=/usr/users/students/ks6241/apache

Step 13: Type the command

make

Step 14: Type the command

make install

Step 14a: Type the command - this will save you a significant amount of disk space!

make clean

Step 15: Edit apache/conf/httpd.conf by using an editor such as pico or vi. Assume that we use pico editor, then type

pico apache/conf/httpd.conf

make the following changes while you are at pico:

Listen 3000 Notice any port number above 1000 will be fine.

Now make the following changes:

MinSpareServers 1

MaxSpareServers 2

StartServers 1

If you done every right the following message. Notice the installation takes approximately 40 minutes.

--------------------------------------------------------------------

| You now have successfully built and installed the

| Apache 1.3 HTTP server. To verify that Apache actually

| works correctly you now should first check the

| (initially created or preserved) configuration files

|

| /usr/users/cis/khailany/apache/conf/httpd.conf

|

| and then you should be able to immediately fire up

| Apache the first time by running:

|

| /usr/users/cis/khailany/apache/bin/apachectl start

|

| Thanks for using Apache. The Apache Group

| http://www.apache.org/

----------------------------------------------------------------------

After installing apache server you have to make the following changes

1. Edit 'httpd.conf' file. Assuming that you are at your home directory.

type

pico apache/conf/httpd.conf

use other path if you 'httpd.conf' is in another directory

Notice in pico control c tells in which line the editor is.

2. Comment "Listen" by using '#', since "Listen" is not being used

3. Find value "Port" (line 236) and make sure it is uncommented. The default value is '8080' which is the EMU unix web server, and therefore can not be used. Change that value to a UNIQUE value that is specific to you server.

Notice to find all port numbers which are used type the command:

netstat -a

4. Find values "User" and "Group" (line 250). Change them to your own username and group.

5. Make sure that "world" can access your apache/htdocs directory which is root directory for html web pages. In other words change the protections of apache/htdocs by typing chmod 755 apache/htdocs

6. The default cgi-bin directory is apache/cgi-bin. To change it go to line# 535 and change the value of the directory here and couple of line below to the one that you want to be your cgi-bin.

7. Close pico

8. If your apache server is running stop it by typing

apache/bin/apachectl stop

9. Restart apache by typing

apache/bin/apachectl start

Notice: If someone using your server you may not want to stop it immediately. In such cases use gracefull

command by typing

apache/bin/apachectl gracefull