The Artima Developer Community
Sponsored Link

Java Buzz Forum
JDBC : JDBC DataSource [Oracle DB]

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
JDBC : JDBC DataSource [Oracle DB] Posted: Sep 6, 2014 10:04 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: JDBC : JDBC DataSource [Oracle DB]
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement























Click here to watch in Youtube : https://www.youtube.com/watch?v=I3BRId1c5zY

Click the below Image to Enlarge
JDBC DataSource [Oracle DB]
JDBC DataSource [Oracle DB]
JDBC DataSource [Oracle DB]
JDBC DataSource [Oracle DB]
OracleDataSource.java
import javax.sql.DataSource;

public class OracleDataSource
{

public static DataSource getOracleDataSource()
{

oracle.jdbc.pool.OracleDataSource oracleDS = null;
try
{
oracleDS = new oracle.jdbc.pool.OracleDataSource();
oracleDS.setURL("jdbc:oracle:thin:@localhost:1521:xe");
oracleDS.setUser("hr");
oracleDS.setPassword("hr");
}
catch( Exception e )
{
e.printStackTrace();
}

return oracleDS;
}

}
JDBCDataSourceDemo.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

public class JDBCDataSourceDemo
{

public static void main( String[] args )
{
JDBCDataSourceDemo jdbcDataSourceDemo = new JDBCDataSourceDemo();
jdbcDataSourceDemo.getEmployeeInformation();

}

private void getEmployeeInformation()
{
Connection connection = null;
Statement stmt = null;
/*
* Get the OracleDataSource
*/

DataSource dataSource = OracleDataSource.getOracleDataSource();
try
{
/*
* Get connection from the DataSource
*/

connection = dataSource.getConnection();

/*
* Execute the Query
*/


stmt = connection.createStatement();
String sql = "select employee_id,first_name,last_name,email,phone_number from employees";
ResultSet rs = stmt.executeQuery(sql);

/*
* Iterate the ResultSet and get each row
* Information.
*/

while( rs.next() )
{
/*
* Retrieve by column name
*/

int id = rs.getInt("employee_id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
String email = rs.getString("email");
String phoneNumber = rs.getString("phone_number");

/*
* Display values
*/

System.out.print("employee_id: " + id);
System.out.print(", first_name: " + firstName);
System.out.print(", last_name: " + lastName);
System.out.print(", email: " + email);
System.out.println(", phone_number: " + phoneNumber);
}
rs.close();
}
catch( SQLException se )
{
se.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}

finally
{
/*
* finally block used to close resources
*/

try
{
if( stmt != null )
{
stmt.close();
}
}
catch( SQLException sqlException )
{
sqlException.printStackTrace();
}
try
{
if( connection != null )
{
connection.close();
}
}
catch( SQLException sqlException )
{
sqlException.printStackTrace();
}

}

}
}

Output
employee_id: 100, first_name: Steven, last_name: King, email: SKING, phone_number: 515.123.4567
employee_id: 101, first_name: Neena, last_name: Kochhar, email: NKOCHHAR, phone_number: 515.123.4568
employee_id: 102, first_name: Lex, last_name: De Haan, email: LDEHAAN, phone_number: 515.123.4569
employee_id: 103, first_name: Alexander, last_name: Hunold, email: AHUNOLD, phone_number: 590.423.4567
employee_id: 104, first_name: Bruce, last_name: Ernst, email: BERNST, phone_number: 590.423.4568
employee_id: 105, first_name: David, last_name: Austin, email: DAUSTIN, phone_number: 590.423.4569
employee_id: 106, first_name: Valli, last_name: Pataballa, email: VPATABAL, phone_number: 590.423.4560
employee_id: 107, first_name: Diana, last_name: Lorentz, email: DLORENTZ, phone_number: 590.423.5567
employee_id: 108, first_name: Nancy, last_name: Greenberg, email: NGREENBE, phone_number: 515.124.4569
employee_id: 109, first_name: Daniel, last_name: Faviet, email: DFAVIET, phone_number: 515.124.4169

--
--
Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCDataSourceDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCDataSourceDemoApp-Oracle.zip?attredirects=0&d=1

External Urls

http://docs.oracle.com/cd/E18283_01/appdev.112/e13995/oracle/jdbc/pool/OracleDataSource.html

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Read: JDBC : JDBC DataSource [Oracle DB]

    Topic: Use Cases for Elasticsearch: Geospatial Search Previous Topic   Next Topic Topic: Groovy Software Developer, Engage PSG, Munich, Germany

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use