JDBC (Java DataBase Connectivity)

/*
I. Getting ready:


	Once :
	- A database program is installed (We work here with Oracle database), 
	- The ORACLE_HOME path is set (ORACLE_HOME C:\oraclexe\app\oracle\product\10.2.0\server), 
	- The correct related driver jdbc (here: ojdbc14.jar) is downloaded and its CLASSPATH is set 
		(C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;); 
			The complete classpath looks like:
			The related classpath is then:
			CLASSPATH =.;C:\Program Files\Java\jre1.5.0_14\lib\ext\dnsns.jar;
			C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servletapi.jar;
			C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;
			C:\oraclexe\app\oracle\product\10.2.0\server\jlib\orai18n.jar
	- The http port is set at 1521:
		From SQL command line:
		SQL> -- set http port and ftp port
		SQL>  begin
		  2  dbms_xdb.sethttpport ('1521');
		  3  dbms_xdb.setftpport ('1520');
		  4  end;
		  5  /
		SQL> quit
		- A table is created and completed,
	we can start connecting to the databse, sending requests, and 
	getting results via Java code.


II. Test the connectivity:
*/
		import java.sql.*;
		import oracle.jdbc.*;
		import oracle.jdbc.OracleDriver;

		public class OracleConnect
		{
		public static void main(String[] args)throws SQLException
		{
		try
		{
		Class.forName("oracle.jdbc.driver.OracleDriver");	
		 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@HOST:1521:XE","User","Password");
		if(conn != null)
		{ System.out.println("I am Connected to oracle database");
		}
		conn.close();
		} catch (Exception e) {
		System.out.println("ERROR : " + e);
		e.printStackTrace(System.out);
		}
		}
		}

/*
III. The main related code:

	We have, generally three kind of requests:
	-  execute Query();where we SELECT to return the ResultSet,
	-  executeUpdate ();  where we CREATE TABLE, UPDATE, INSERT, DROP, or  DELETE, 
	which return a confirmation digit,
	-  execute(); 

	The main related code is the following:

	Statement stmt = conn.createStatement();
	ResultSet rst = stmt.executeQuery("SELECT * (or other field) FROM TABLE"); or
	ResultSet rst = stmt.executeUpdate ("CREATE TABLE, UPDATE, INSERT, DROP, or  DELETE requests");

	We can therafter access to the related columns of the table by codes, 
	such as:
	int Value = rst.getInt(n);//get the int from the nth columun
	String Data = rst.getString("Data");

	And such as:
	while(rst.next()){
	int value = rst.getInt("culumnA");
	String str = rst.getStrig ("columnB";
	....
	}
	Finally:
	ResultSet.close();
	Statement.close();
	Connection.close();


IV. Queries on a built Students database:
*/

		import java.sql.*;
		import oracle.jdbc.*;
		import oracle.jdbc.OracleDriver;	

		public class showTable{
		public static void main (String args []) throws SQLException
		{
		DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@HOST:1521:XE","User","Password");		
		Statement stmt = conn.createStatement();
		try {
		ResultSet rst = stmt.executeQuery( "SELECT * FROM Students WHERE STUDENTID ='103' " );
		try {
		while ( rst.next() ) {
		int numColumns = rst.getMetaData().getColumnCount();
		for ( int i = 1 ; i <= numColumns ; i++ ) {//Column numbers start at 1.
		System.out.println( "COLUMN " + i + " = " + rst.getObject(i) );
		}
		System.out.println();
		}
		} finally {
		rst.close();
		}
		} finally {
		stmt.close();
		}
		}
		}// End of class showtable


© The Scientific Sentence. 2007