Monday, June 8, 2009

Connecting to a Database via JDBC Architecture

JDBC

• It is an API Specification developed by Sun MicroSystems

• It is a Core part of the standard Java Platform.

• It defines a uniform interface for accessing different relational databases .

• It is included in the standard JDK.

Accessing a Database [ Connecting to a Database via JDBC Architecture ]

1. Register the JDBC driver with the DriverManager

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

2. Establish a Database Connection using Database URL jdbc::

Connection con = DriverManager.getConnection("jdbc:odbc:dsname");

3. Execute an SQL Statement

i. Create a Statement object using Connection object's createStatement() method.

Statement stmt = con.createStatement();

ii. Query can be executed by using the Statement object's executeQuery()
method. It returns a ResultSet object that encapsulates the retrieved
data.

ResultSet rs = stmt.executeQuery("Query");

4. Process the Results

i. ResultSet object's next()& previous() methods can be used to
iterate through the rows of the result set.

ii. ResultSet object's getString() method is used to extract the value of
specified fields.


while(rs.next()){
System.out.println(rs.getString("fieldName"));
}

5. Close the Database Connection.

Database Connections should be closed after completing transactions.

rs.close();
stmt.close();
con.close();

[Written on: Monday, August 22, 2005 3:02:34 PM]

ADO.NET Database connections: SQLClient, OleDb, Odbc

ADO.NET
ADO.NET is a set of classes that expose data access services to the .NET programmer. ADO.NET provides a rich set of components for creating distributed, data-sharing applications. It is an integral part of the .NET Framework, providing access to relational data, XML, and application data. ADO.NET supports a variety of development needs, including the creation of front-end database clients and middle-tier business objects used by applications, tools, languages, or Internet browsers.

Code Samples of ADO.NET application that retrieves data from a database and returns it to the console.

SqlClient

using System;
using System.Data;
using System.Data.SqlClient;

class Sample
{
public static void Main()
{
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand catCMD = nwindConn.CreateCommand();
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories";

nwindConn.Open();

SqlDataReader myReader = catCMD.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}

myReader.Close();
nwindConn.Close();
}
}

OleDb


using System;
using System.Data;
using System.Data.OleDb;

class Sample
{
public static void Main()
{
OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

OleDbCommand catCMD = nwindConn.CreateCommand();
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories";

nwindConn.Open();

OleDbDataReader myReader = catCMD.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}

myReader.Close();
nwindConn.Close();
}
}

Odbc

using System;
using System.Data;
using System.Data.Odbc;

class Sample
{
public static void Main()
{
OdbcConnection nwindConn = new OdbcConnection("Driver={SQL Server};Server=localhost;" +
"Trusted_Connection=yes;Database=northwind");

OdbcCommand catCMD = new OdbcCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

nwindConn.Open();

OdbcDataReader myReader = catCMD.ExecuteReader();

while (myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}

myReader.Close();
nwindConn.Close();
}
}

Installing Linux along with Windows - How?

Q.How to install a workable Linux environment along with existing Windows OS without losing the existing data on the hard disk?

For linux installation , a hard disk should be partitioned in to two or more partitions. First partition is the root (/) partition (indicated as /) and a swap partition. Other partitions are optional such as /usr , /home , /boot , etc. Because if you given only two partitions as root (/) and swap , the linux os will create the necessary directories such as /usr , /home , etc in the root (/) partition itself. The Swap partition should be twice of the system's RAM size. For example , if you are having 256 MB RAM then it should be allotted a 512MB size for the Swap partition.

For Installing Linux along with existing Windows OS , Consider the following situation, a hard disk with a capacity of 40GB  which is divided into 2 partitions as one primary 10GB and another extended partition 30GB. The Extended partition consists of three logical partitions each of size 10GB. see figure1.
 
First the last drive in the extended partition should be deleted. Take necessary backups before deleting that logical drive partition. Restart the machine and boot using the Linux installation CD. Partitioning is the major bottle neck in Linux Installation , other processes are simple , So select DiskDruid to manually partition the hard disk. Select the last drive indicated by hdax , where x is the big number (say hda7 ) ,i.e., it should be the free space allotted by deleting the last drive of your windows. Create the swap space by specifying its size. Create the root partition with size 6 to 8GB as per your requirements . Create other partitions if you have some space and if you feel they are necessary. A complete 3CD kit of Redhat Fedora requires 6GB hard disk space plus Swap space. See figure2.

I will always prefer Mandrake Linux to any one who is willing to shift to the rocksolid Linux platform from windows. Because , Mandrake Linux is the friendliest Linux Distribution. Its very simple, speedy and Lovable. Distributions Like RedHat are some what difficult to understand for a native windows user. Its User Interface is poor. It does not provide any good multimedia player. You have to type commands to use your cdrom and floppy disk. But in case of Mandrake Linux , It provides good Audio and Video software tools , Image editing softwares , Developer tools. It provides the supermounting facility; you can directly access the cds and floppies as like in windows. You can access your windows partitions without typing any commands.It provides a rich set of developer tools such as Qt , Python , Tcl/Tk , Java , C , C++ , PHP , Perl , MySQL , etc. Gimp – an excellent image editing software is available . Totempotent ,Mplayer – media players are available. Wine – a program that is used to run windows applications under Linux platform is provided. Mandrake provides an excellent User Interface which the native windows user doesn't find any difficulty to do his day-to-day jobs.

Note : This How to documentation is truly by my own experience. Any mishap or loss of data due to attempting to do linux installation as per this document is purely based on your own risk. If you have any doubts regarding this procedure ask me. If you success fully installed Linux then congrats. try to share your Linux experience with your friends.

My Linux experience started as an adventure. First of all i didn't know about partitions , I got a Mandrake Linux 8.2 CD from digit magazine. Its a single CD i.e., first CD only provided. So i tried by reading some instructions given in the magazine . My motherboard is intel 845GL for which mandrake 8.2 doesn't provide sound support and graphics support. Continuously the X-Windowing system crashed and only text mode is available for me .So I repeatedly installed linux after the Xserver crashes to get the GUI. Later i know about the vga ram setting of the BIOS and had made changes to get the stable system. Later i got the Mandrake Linux 10.1 Community Edition DVD. And my box is now powered with Mandrake Linux
[Written on: Wednesday, August 24, 2005 11:33:56 AM]