Connecting java program to oracle databse


This code is working fine. here, in connection string test is the name of database, system is user and 123456 is password.
To run this program you need to use a jar file into classpath atteched here. that jar file may be found in oracle installation directory : oracle/jdbc/lib/


import
 java.sql.*;
class OraDataBaseDemo1{
    public static void main(String ar[]){
        try{
            //loading driver
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //Here we are getting connection object
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","123456");
            //Statement object will be used to execute SQL queries
            Statement st=con.createStatement();
            String query="select * from address";
            ResultSet rs=st.executeQuery(query);
            while(rs.next()){
                System.out.println(rs.getString(1)+"\t\t"+rs.getString(2)+"\t\t"+rs.getString(3));
            }
        }catch(Exception e){
            System.out.println(e);   
        }
    }
}


No comments:

Popular Posts