Java Y Oracle

5,063 views
4,967 views

Published on

Ejemplo sencillo para conectarse a oracle

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
5,063
On SlideShare
0
From Embeds
0
Number of Embeds
3,175
Actions
Shares
0
Downloads
91
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Y Oracle

  1. 1. Java y Oracle <ul><li>Vamos a Generar una aplicación para extraer informacion de la base de datos, utilizando java, la base de datos oracle 10g, NetBeans. </li></ul>
  2. 2. Java y Oracle <ul><li>Abrimos el netbeans </li></ul><ul><li>Nueva clase(Para hacer una aplicación de consola) </li></ul><ul><li>Importamos las librerias necesarias de acceso a datos: </li></ul><ul><ul><ul><li>import java.sql.DriverManager; </li></ul></ul></ul><ul><ul><ul><li>import java.sql.Connection; </li></ul></ul></ul><ul><ul><ul><li>import java.sql.Statement; </li></ul></ul></ul><ul><ul><ul><li>import java.sql.ResultSet; </li></ul></ul></ul><ul><ul><ul><li>import java.sql.Date; </li></ul></ul></ul><ul><ul><ul><li>import java.sql.SQLException; </li></ul></ul></ul>
  3. 3. Java y Oracle <ul><li>Dentro de main comenzamos a declarar la conexión: </li></ul>/ / STEP 1:Declare a Connection Variable Connection conn = null; try{ / / STEP 2: Register JDBC driver String driver = &quot;oracle.jdbc.driver.OracleDriver&quot;; Class.forName(driver); //STEP 3: Open a connection System.out.println(&quot;Connecting to database...&quot;); / / Print Connecting to Database String jdbcUrl = &quot;jdbc:oracle:thin:@3.211.62.156:1521:orcl&quot;; / / database host String user = &quot;hr&quot;; / / Database User String password = “tiger&quot;; / / Database Password conn = DriverManager.getConnection(jdbcUrl,user,password); //STEP 4: Execute a query Statement stmt = conn.createStatement(); String sql; sql = &quot;SELECT * from jobs&quot;; / / Querie statement ResultSet rs = stmt.executeQuery(sql); / / Executing user
  4. 4. Java y Oracle <ul><li>Se imprimen los resultados </li></ul>//STEP 5: Printing the result while(rs.next()){ //Retrieve by column name String ssn= rs.getString(1); String name = rs.getString(2); System.out.println(&quot;Job ID: &quot; + ssn); System.out.println(&quot;, Name: &quot; + name); } //Se cierran las conexiones rs.close(); stmt.close(); conn.close(); }
  5. 5. Java y Oracle <ul><li>Bloque Catch </li></ul><ul><li>Para el caso de algun error, vamos a incluir el bloque catch </li></ul>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(conn!=null) conn.close(); } catch(SQLException se) { se.printStackTrace(); }//end finally try }//end try System.out.println(“Error, revisar codigo!!!&quot;); }//end main }//end FirstQuery
  6. 6. Java y Oracle <ul><li>Ejecutando nuestro programa </li></ul>
  7. 7. Java y Oracle <ul><li>Fin </li></ul><ul><li>Preguntas: [email_address] </li></ul><ul><li>[email_address] </li></ul>
  8. 8. Java y Oracle <ul><li>Fin </li></ul>

×