Professional Documents
Culture Documents
providing a structured and efficient means of storing and managing data. In a Java
assessment, understanding relational databases involves familiarity with concepts like
database design, SQL (Structured Query Language), JDBC (Java Database Connectivity),
and ORM (Object-Relational Mapping) frameworks. Let's explore these aspects in detail.
1. Database Design:
Database design is the process of defining the structure that will organize and store
data efficiently. It involves defining tables, relationships, constraints, and keys. Key
components include:
Tables: Entities are represented as tables, and each table consists of rows and
columns.
Columns: Represent attributes or fields of an entity, defining the type of data it
can hold.
Rows: Records or instances of an entity, where each row corresponds to a unique
record in the table.
Primary Key: A unique identifier for each row in a table, ensuring data integrity.
Example:
sqlCopy code
-- Creating a table CREATE TABLE Users ( UserID INT PRIMARY KEY, UserName VARCHAR ( 50 ), Email
VARCHAR ( 100 ) ); -- Inserting data INSERT INTO Users (UserID, UserName, Email) VALUES ( 1 ,
'JohnDoe' , 'john@example.com' ); -- Querying data SELECT * FROM Users WHERE UserID = 1 ;
3. Java Database Connectivity (JDBC):
JDBC is a Java-based API that enables Java applications to interact with relational
databases. Key JDBC concepts include:
Example:
javaCopy code
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import
java.sql.ResultSet; import java.sql.SQLException; public class JDBCTutorial { public static void
main(String[] args) { try ( Connection connection =
DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydatabase" , "username" , "password" )) { String
query = "SELECT * FROM Users WHERE UserID = ?" ; try ( PreparedStatement preparedStatement =
connection.prepareStatement(query)) { preparedStatement.setInt( 1 , 1 ); try ( ResultSet resultSet =
preparedStatement.executeQuery()) { while (resultSet.next()) { int userId = resultSet.getInt( "UserID" ); String
userName = resultSet.getString( "UserName" ); String email = resultSet.getString( "Email" );
System.out.println( "User ID: " + userId + ", UserName: " + userName + ", Email: " + email); } } } } catch
(SQLException e) { e.printStackTrace(); } } }
ORM frameworks like Hibernate or JPA (Java Persistence API) simplify database
interactions by mapping Java objects to database tables. Key concepts include:
javaCopy code
@Entity @Table(name = "Users") public class User { @Id @GeneratedValue(strategy =
GenerationType.IDENTITY) @Column(name = "UserID") private int userId; @Column(name = "UserName")
private String userName; @Column(name = "Email") private String email; // Getters and setters }