To run this program you need :
- Oracle 11g
- My Eclipse
- ojdbc1.6.jar
- If you are facing problem in this then-- Type Services in your search and program files-(appear when you click in windows button)
- start all oracle related services.
Then download the ojdbc.1.6.jar file and place that inside your Project directory.
Copy and Paste this Code and Run.
most probably you find 2 type of error
1.Number format Exception
2.Driver not found
For this your Step3 of this program should be correct.
If incase you are running XE (oracle) then replace orcl to XE in step 3.
hope it works for you ,if you face any problem let me know it..
- Right Click on Project Directory(Inside MyEclipse).
- go to Build-Configure -Libraries-Add jar -then from the following project add the ojdbc1.6.jar
Copy and Paste this Code and Run.
--------------------------------------------------------------------------------------------------------------import java.sql.*; public class RetriveEmpInfo { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn =DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/orcl","HR","sunilsahu"); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT ename,cname,salary FROM emp_company"; ResultSet rs = stmt.executeQuery(sql); //STEP 5: Extract data from result set while(rs.next()){ //Retrieve by column name String ename = rs.getString("ename"); String cname = rs.getString("cname"); int salary = rs.getInt("salary"); // String last = rs.getString("last"); //Display values System.out.println("Ename: " + ename); System.out.println(", Cname: " + cname); System.out.println(", Salary: " + salary); //System.out.println(", Last: " + last); } //STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end FirstExample
most probably you find 2 type of error
1.Number format Exception
2.Driver not found
For this your Step3 of this program should be correct.
If incase you are running XE (oracle) then replace orcl to XE in step 3.
hope it works for you ,if you face any problem let me know it..