Chapter 1
Chapter 1
Java Database Connectivity (JDBC) is an application programming interface (API) for Java that
allows Java applications to access and manipulate data stored in relational databases. JDBC provides a
standard way for Java applications to connect to different databases, regardless of the specific
database vendor or product.
There are many reasons why you might want to use JDBC in your Java applications:
Portability: JDBC allows you to write Java applications that can access data stored in a variety of
databases, without having to write separate code for each database.
Ease of use: JDBC provides a simple and easy-to-use API for accessing and manipulating database
data.
Performance: JDBC is a high-performance API that can efficiently access and manipulate data in
large databases.
Pluggability: JDBC is a pluggable API, which means that you can use different JDBC drivers to
connect to different databases.
JDBC works by providing a set of classes and interfaces that represent the different components of a
database connection, such as the database connection, the SQL statement, and the result set. These
classes and interfaces allow Java applications to interact with the database in a standard way,
regardless of the specific database vendor or product.
Driver manager: The driver manager is responsible for loading the appropriate JDBC driver for the
database that you want to connect to.
Connection: A connection represents a connection to a database. You use a connection to execute
SQL statements against the database.
Statement: A statement represents an SQL statement that you want to execute against the database.
ResultSet: A result set contains the results of an SQL query.
How do I use JDBC?
1. Load the JDBC driver for the database that you want to connect to.
2. Establish a connection to the database.
3. Create a statement object.
4. Execute an SQL statement using the statement object.
5. Process the results of the SQL statement.
6. Close the connection to the database.
To connect java application with the oracle database, we need to follow 5 following steps. In this
example, we are using Oracle 10g as the database. So we need to know following information for the
oracle database:
Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
Connection URL: The connection URL for the oracle10G database is
jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver,
localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port
number and XE is the Oracle service name. You may get all these information from the tnsnames.ora
file.
Username: The default username for the oracle database is system.
Password: It is the password given by the user at the time of installing the oracle database.
Create a Table
Before establishing connection, let's first create a table in oracle database. Following is the SQL
query to create a table.
create table emp(id number(10),name varchar2(40),age number(3));
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
}
}
download this example
The above example will fetch all the records of emp table.
To connect java application with the Oracle database ojdbc14.jar file is required to be loaded.
Assigning Roles: Users may be assigned to other roles or to individual users. A user inherits the
privileges that come with the role when they are assigned one.
Users may receive roles directly from other roles or indirectly through other roles. When a role is
granted to another position and the user receives the containing role, this is known as an indirect role
grant.
Roles can be withdrawn from users or from other roles. A role that has been revoked loses all of
its associated privileges, for both the user and the role.
Role nesting is supported by Oracle, allowing for the formation of a hierarchical role structure. In
complicated systems, this can assist organize and simplify privilege management.
i. DBA
This predefined role comes with Oracle upon installation and provides system
administrative privileges for the database; we will not grant this role to any user.
ii. CONNECT
This predefined role allows users to connect to Oracle; we are encouraged not to
use predefined roles, so we won’t.
iii. create_session_role
Our role to allow users to connect to Oracle.
iv. secadm_role
Our security administrator role; we will grant the privileges required to perform
security administration to this role, and we will grant this role to the SECADM
user.
v. hrview_role
Our secure application role for granting access to data in the human resources
(HR) sample schema.
vi. appsec_role
A non-default role used by APPSEC when configuring application security.
vii. appver_role
Secure application role for running application verification.
Privileges: Users assigned the DBA role are granted a wide range of system privileges, which
provide them with full control over the database. These privileges may include, but are not limited
to, the following:
CREATE, ALTER, and DROP any database object, such as tables, views, and indexes.
EXECUTE any PL/SQL or SQL code.
CREATE and manage users and roles.
ALTER and maintain database parameters.
Backup and recovery operations.
Control over database security, including granting and revoking privileges.
Responsibilities: Users with the DBA role are responsible for various tasks related to the
database, including but not limited to:
SYSDBA: This privilege provides unrestricted access to the database. Users with the SYSDBA
privilege can perform any administrative task, including shutting down the database.
SYSOPER: This privilege allows users to perform basic database operations, like starting and
stopping the database.
Security Considerations: Due to its extensive privileges, the DBA role should be assigned with
caution. Only trusted and authorized personnel should have this role to prevent unauthorized
access to sensitive data and critical database operations.
Password Management: It's essential to ensure strong password management for users assigned
the DBA role to prevent unauthorized access. Additionally, two-factor authentication and other
security measures should be considered.
Auditing: Auditing should be enabled to track the activities of users with the DBA role. Audit
logs can help monitor and investigate any suspicious or unauthorized activities.
Separation of Duties: It is a good practice to separate the DBA role from other roles, such as
application developer roles. This separation of duties helps maintain a clear boundary between
those responsible for database administration and those responsible for application development.
EXAMPLE
The below figure shows database administrator role at different levels.
The security administrator will be a separate non-personal user; that is, an account and password
that can be delegated to various people who turn in and out of job responsibility.
• The security administrator will perform tasks that would typically be performed by a DBA or
even SYS, but we will limit the privileges we grant to the security administrator to only aspects
related to application security.
• First of all, as SYS, we will create the user secadm and grant it the create_session_role, in one
step.
• Substitute a real password for “password” in this command:
GRANT create_session_role TO secadm IDENTIFIED BY password
• All the privileges that the security administrator needs will be granted to secadm_role, so we
are going out of our way to protect it.
• The keywords IDENTIFIED USING sys.p_check_secadm_access indicate that when a user
attempts to acquire secadm_role, he will have to get it from the procedure named
p_check_secadm_access, which exists in the SYS schema.
• A stored procedure (procedure) is a named block of Procedural Language/Structured Query
• Language (PL/SQL) code that is stored and run (executed) on the Oracle database. Generally,
a procedure takes parameters and does work.
• It can also return information through its parameters. Oracle there are also stored functions
(functions), which are very similar to procedures except that they usually take values; do research
or calculations; and then return a single value as the result.
• We will be using both procedures and functions.
• The specific procedure used for verifying secadm_role, p_check_secadm_access does not
take any parameters (arguments or values passed to the procedure for evaluation), and does not
return any results.
• Most procedures do take parameters, but they don’t have to we are creating the procedure to
be used for acquiring the security administrator role.
• Note that the simple goal I have for this procedure is to require that the security administrator
be running on the same computer as the Oracle database server (the IP address 127.0.0.1 is also
known as the localhost or the loopback address).
• This requirement may not be appropriate for your system; if not, you can still create the
procedure, but comment out the three lines that start with IF, THEN, and END IF by placing two
dashes (minus signs) in front of it.
• You can execute this command as SYS, and the procedure will be created.
2. Which of the following commands can be used to create a new user role?
A. CREATE ROLE
B. GRANT ROLE
C. REVOKE ROLE
D. ALTER ROLE
3. Which of the following commands can be used to grant a user role to another user?
A. CREATE ROLE
B. GRANT ROLE
C. REVOKE ROLE
D. ALTER ROLE
4. Which of the following commands can be used to revoke a user role from another user?
A. CREATE ROLE
B. GRANT ROLE
C. REVOKE ROLE
D. ALTER ROLE
7. Which of the following commands can be used to grant a schema to a user or role?
A. GRANT SCHEMA
B. GRANT OBJECT
C. REVOKE SCHEMA
D. ALTER SCHEMA
8. Which of the following commands can be used to revoke a schema from a user or role?
A. GRANT SCHEMA
B. GRANT OBJECT
C. REVOKE SCHEMA
D. ALTER SCHEMA
10. Which CIA triangle element provides information protection from loss or destruction?
a) Confidentiality
b) Integrity
c) Availability
13. Which security service ensures that data is not altered during transmission?
a) Authentication
b) Integrity
c) Availability
d) Non-repudiation
14. Which security service provides proof of the origin and integrity of data?
a) Authentication
b) Confidentiality
c) Non-repudiation
d) Availability
15. Which security mechanism is used to prevent unauthorized access to a network?
a) Firewall
b) Public key infrastructure (PKI)
c) Intrusion Detection System (IDS)
d) Virtual Private Network (VPN)
17. Which security service ensures that only authorized users can access resources?
a) Authorization
b) Confidentiality
c) Availability
d) Non-repudiation
18. Which security mechanism provides a secure method for exchanging encryption keys?
a) Digital signature
b) Hash function
c) Key distribution center (KDC)
d) Public key cryptography
19. Which security attack involves the interception and capture of data during transmission?
a) Spoofing attack
b) Replay attack
c) Man-in-the-middle attack
d) Phishing attack
20. Which model is used to describe network security goals and mechanisms?
a) OSI model
b) TCP/IP model
c) Kerberos model
d) CIA model
21. The CIA model stands for:
a) Confidentiality, Integrity, Authentication
b) Confidentiality, Integrity, Availability
c) Cryptography, Intrusion Detection, Authorization
d) Communication, Integrity, Access control
22. Which security goal ensures that data is not disclosed to unauthorized parties?
a) Confidentiality
b) Integrity
c) Availability
d) Non-repudiation
TERMINAL QUESTIONS
1. What are the different types of user roles in Oracle Database?
2. How do you create a new user role?
3. How do you grant a user role to another user?
4. How do you revoke a user role from another user?
5. Summarize DBA Roles with user activities?
6. How do you list all users in an Oracle database using SQL on the command line?
7. How do you create a new user with a specific tablespace in Oracle using SQL on the
command line?
8. How do you grant a user the CONNECT and RESOURCE roles in Oracle using SQL on the
command line?
9. How do you create a schema for a user in Oracle using SQL on the command line?
10. How do you change the password of an existing user in Oracle using SQL on the command
line?
11. How do you list the objects (tables, views, etc.) owned by a specific user in Oracle using
SQL on the command line