JP 6 1 Practice Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Java Programming

6-1: JDBC Introduction


Practice Solutions

Lesson Objectives:
• Describe the JDBC

• Introduce Oracle JDBC Driver

• Outline the steps in JDBC programming

• Describe the JDBC Statement

Vocabulary:
Identify the vocabulary word for each definition below.

Type 1 Driver The JDBC-ODBC Bridge plus ODBC Driver

Statement The interface used for executing a static SQL statement and returning the results it
produces

DriverManager The class which manages a set of JDBC Driver

Connection The interface which is able to provide information for the tables, stored procedures and so
on.

Try It/Solve It:

1. Set the JDBC runtime environment:

ORACLE_HOME/jdbc/lib/ojdbc8.jar
ORACLE_HOME/jlib/orai18n.jar

2. Identify the structure of the database tables that you will be working with.

a) Open SQLPlus and log in using the orcluser account.

b) Identify the structure of each of the tables by running a DESCRIBE command on the table. Identify column names and data
types.

The syntax for a DESCRIBE command is:


Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
DESCRIBE tablename;
Given Tables:
• job_grades;
• job_history;
• employees;
• jobs;
• departments;
• locations;
• countries;
• regions;

3. Write a java application to display the information in the JOBS table to the console.
The application should print out the JOB ID, JOB TITLE, MIN SALARY and MAX SALARY columns.

package jobdetails;

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

import oracle.jdbc.pool.OracleDataSource;

public class JobDetails {

public static void main(String[] args) throws SQLException {


OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:orcluser/jdbcuser@localhost:1521:xepdb1");
Connection conn = ods.getConnection();

Statement stmt=conn.createStatement();

// Execute a statement
ResultSet rset= stmt.executeQuery("Select JOB_ID, JOB_TITLE,
MIN_SALARY,MAX_SALARY from jobs");

// Iterate through the result and print the employee names and ID
System.out.println("Job ID" + "\t" +"JOB Title"+"\t"+"Min Salary"+"\t"
+ "Max Salary");
System.out.println();
while (rset.next())
System.out.println(rset.getString(1)
+ "\t" +rset.getString(2)
+ "\t"+rset.getString(3)
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

2
+ "\t"+rset.getString(4));
//endwhile

//close the resources


rset.close();
stmt.close();
}//end method main
}//end class JobDetails

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like