24, అక్టోబర్ 2022, సోమవారం

 

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());}}

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

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