FT 2 Answer
FT 2 Answer
1.
Persistence: Cookies can be persistent or session-based, which
will be dependent on the time duration of data to last.
Tracking: Cookies are used as trackers to collect user’s
behaviour which will be helpful for marketing strategies, improving
content, etc.
Authentication: Cookies are essential for maintaining the user’s
session and authentication to stay logged in.
2.
Restrictions on data Only ASCII characters No restrictions. Binary data is also allowed
type allowed
5. Java Database Connectivity. JDBC is a Java API to connect and execute the query
with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database. There are four types of JDBC drivers:
10marks
6.
To create a book database with a `members` table and write a JDBC (Java Database
Connectivity) program to query and print all entries in the `members` table, you need
to follow these steps:
1. **Set Up Your Database**:
First, you'll need to set up a database management system (DBMS) such as MySQL,
PostgreSQL, SQLite, etc., and create a database with a `members` table. Here's an
example SQL script to create a simple `members` table in MySQL:
```sql
CREATE DATABASE book_database;
USE book_database;
CREATE TABLE members (
member_id INT PRIMARY KEY AUTO_INCREMENT,
member_name VARCHAR(255) NOT NULL
);
```
This script creates a database called `book_database` and a table called `members`
with two columns: `member_id` and `member_name`.
2. **Install a JDBC Driver**:
You'll need the JDBC driver for your specific database system. Download and add the
JDBC driver JAR file to your Java project's classpath.
3. **Write the JDBC Program**:
Here's a Java program that connects to the database, queries the `members` table, and
prints all entries:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MemberDatabase {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/book_database"; // Replace with your
database URL
String username = "your_username"; // Replace with your database username
String password = "your_password"; // Replace with your database password
try {
// 1. Load the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Establish a database connection
Connection connection = DriverManager.getConnection(jdbcUrl, username,
password);
// 3. Create a SQL statement
Statement statement = connection.createStatement();
// 4. Execute a SQL query
String sqlQuery = "SELECT * FROM members";
ResultSet resultSet = statement.executeQuery(sqlQuery);
// 5. Process the result set
while (resultSet.next()) {
int memberId = resultSet.getInt("member_id");
String memberName = resultSet.getString("member_name");
System.out.println("Member ID: " + memberId + ", Name: " + memberName);
}
// 6. Close the resources
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
Make sure to replace `"your_username"` and `"your_password"` with your
database credentials, and `"jdbc:mysql://localhost:3306/book_database"` with your
actual database URL.
4. **Compile and Run the Program**:
Compile the Java program and run it. It should connect to the database, retrieve
data from the `members` table, and print the results to the console.
Ensure that you have the necessary database and JDBC driver configured correctly
to run this program successfully.
Import the packages − Requires that you include the packages containing the
JDBC classes needed for database programming. Most often, using import java.sql.* will
suffice.
Open a connection − Requires using the DriverManager.getConnection() method
to create a Connection object, which represents a physical connection with the database.
Execute a query − Requires using an object of type Statement for building and
submitting an SQL statement to the database.
Extract data from result set − Requires that you use the
appropriate ResultSet.getXXX() method to retrieve the data from the result set.
Clean up the environment − Requires explicitly closing all database resources
versus relying on the JVM's garbage collection.
import java.sql.*;
public class SelectExample {
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT id, first, last, age FROM Employees";
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
C:\>java SelectExample
Connecting to database...
Creating statement...
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal