28, మార్చి 2012, బుధవారం

JAVA DATABASE CONNECTIVITY IMPLEMENTATION

Mr.D.Ramanjaneyulu you can reach me at d_ramu2002@yahoo.com  program-2


import java.io.*;   
import java.sql.*;
public class ramuexce
{
                public static void main(String s[])  throws Exception
                {
try
{
                                   //general method-1
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                Connection con=DriverManager.getConnection("Jdbc:Odbc:oradsn","ramu","ram");
                                  //method-2

                                  Statement st=con.createStatement();
                                ResultSet rs=st.executeQuery("select * from student");
/*for(int i=1;i<=3;i++) //the index must start from the 1 only generally people are facing trouble by putting the value for index '0'
                                                {
                                                                Object o=rs.getObject(i);
                                                                System.out.println("result:"+o);}*/
// 2nd method this is the polished way of writing CODE to retrive  the data from the database
while(rs.next())
                                {
System.out.println(rs.getInt(1));// if we put beyond the existing column we will get an error which will totally stop the program even no partial execuation
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));// here i am mentaioning INT datatype insted of Float datatype now you will get the warning which allows you partial execuation
System.out.print("\n");
                                }
//implementing the SQLEXCEPTION  class for knowing the EXCEPTION for knowing  details
//implementing SQLWARNING  CLASS for knowing SQLWARNINGS AT COMPILETIME
SQLWarning w= rs.getWarnings();

System.out.println("now you are in warnings blockTHE FIRST WARNING IS ");
System.out.println("THE MESSAGE IS"+w.getMessage());
System.out.println("THE STATE IS "+w.getSQLState());
System.out.println("THE ERROR NUMBER IS "+w.getErrorCode());
w.getNextWarning();
System.out.println("THE NEXT WARNING IS ");
System.out.println("THE MESSAGE IS"+w.getMessage());
System.out.println("THE STATE IS "+w.getSQLState());
System.out.println("THE ERROR NUMBER IS "+w.getErrorCode());
                                                               
rs.close();
st.close();                              
con.close();
}//end of try
catch(SQLException e)
{
System.out.println("THE EXCPTIONS ARE  "+e.getMessage());
}// end of exception
}// end of main method
}//end of main class

import java.io.*;
import java.sql.*;
class ramuorcl{
public static void main(String s[])  //throws Exception{
try
{             //general method-1
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                Connection con=DriverManager.getConnection("Jdbc:Odbc:oradsn","ramu","ram");
                                  //method-2   Statement st=con.createStatement();
                                ResultSet rs=st.executeQuery("select * from student");
/*for(int i=1;i<=3;i++) //the index must start from the 1 only generally people are facing trouble by putting the value for index '0'
                                                {
                                                                Object o=rs.getObject(i);
                                                                System.out.println("result:"+o);

                                                }*/
//implementing the DATABASEMETADATA class for knowing the database details
DatabaseMetaData dbmd=con.getMetaData();
System.out.println("THE PRODUCTNAME IS "+dbmd.getDatabaseProductName());
System.out.println("URL IS"+dbmd.getURL());
System.out.println("THE USERNAME IS "+dbmd.getUserName());
System.out.println("THE DRIVER NAME IS "+dbmd.getDriverName());
System.out.println("THE VERSION NAME IS "+dbmd.getDriverVersion());
System.out.println("----------------------------------------------------");                  
// to know about these total details we must put the array and retrive with help of  a while loop until return the null by the loop
//implementing the RESULTSETMETADATA class for knowing the TABLE SCHEMA details
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("URL IS"+rsmd.getColumnName(1));
System.out.println("THE USERNAME IS "+rsmd.getColumnTypeName(1));
System.out.println("THE COLUMN COUNT IS "+rsmd.getColumnCount());
System.out.println("----------------------------------------------------");  
}
//implementing the SQLEXCEPTION  class for knowing the EXCEPTION for knowing  details
//implementing SQLWARNING  CLASS for knowing SQLWARNINGS AT COMPILETIME
SQLWarning w= rs.getWarnings();
while(w !=null)
{
System.out.println("THE MESSAGE IS"+w.getMessage());
System.out.println("THE STATE IS "+w.getSQLState());
System.out.println("THE ERROR NUMBER IS "+w.getErrorCode());
w.getNextWarning();
}
//this willnot work because it has been defined in ORACLE       
// 2nd method this is the polished way of writing CODE to retrive  the data from the database
System.out.println(rs.getInt(1)); System.out.println(rs.getString(2)); System.out.println(rs.getFloat(3));
System.out.print("\n");
                                }                                                              
rs.close();  st.close();   con.close();
}}
catch(SQLException e)
{
System.out.println("THE EXCPTIONS ARE  "+e.getMessage());}}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి