[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

Experiment 12

Experiment

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Experiment 12

Experiment

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 12

Aim: To study the Java Database connectivity.

Problem Statement:
Write a Java program for Database connectivity with MySQL.

Objective: To learn JDBC connectivity for Database.

Theory: Java Database Connectivity: JDBC stands for 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. It basically acts as an interface (not the one we
use in Java) or channel between your Java program and databases i.e it establishes a
link between the two so that a programmer could send data from Java code and store
it in the database for future use.
Purpose of JDBC: JDBC (Java Database Connectivity) is used for efficient
interaction between Java EE enterprise applications and databases, allowing them to
store and manage data. It uses ODBC drivers to communicate with databases like
Oracle, MySQL, MS Access, and SQL Server.
JDBC Architecture consists:
● Application: Java applets or servlets that communicate with databases.
● JDBC API: Allows Java programs to execute SQL statements and retrieve
results.
● DriverManager: Manages database-specific drivers for connectivity.
● JDBC Drivers: Convert Java program requests into a protocol understood by
the DBMS.
Types of JDBC Drivers:
Type-1 (JDBC-ODBC Bridge Driver): Uses ODBC driver for database
connectivity.
Type-2 (Native-API Driver): Uses client-side database libraries.
Type-3 (Network Protocol Driver): Uses middleware to convert JDBC calls into
vendor-specific protocols.
Type-4 (Thin Driver): Directly converts JDBC calls into database-specific
protocols.
The java.sql package contains classes and interfaces for JDBC API.
A list of popular interfaces of JDBC API are given below:
Interfaces: Driver, Connection, Statement, PreparedStatement, CallableStatement,
ResultSet, ResultSetMetaData, DatabaseMetaData, RowSet.
Classes: DriverManager, Blob, Clob, Types.
Java Database Connectivity-Steps:
1. Import packages.
2. Load drivers using Class.forName().
3. Register drivers with DriverManager.
4. Establish connection using a Connection object.
5. Create a statement.
6. Execute SQL queries.
7. Close the connection.

Outcome: Able to develop database connectivity using JDBC and MySQL.

Conclusion: This experiment helps the students to understand the concept of Java
Database connectivity (JDBC).

Code:
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Display User Info</title>
</head>
<body>
<!-- Create a form with a button to trigger the data retrieval
--> <form method="post">
<input type="submit" value="Click Me">
</form>
<%
// This block will execute when the form is submitted (via
POST method) if
(request.getMethod().equalsIgnoreCase("POST")) { %>
<table border="1">
<tr>
<th>ID</th>
<th>Username</th>
<th>email</th>
</tr>
<%
// JDBC URL, username, and password of MySQL server
String jdbcUrl = "jdbc:mysql://localhost:3306/conn";
String jdbcUser = "root";
String jdbcPassword = "root";
// Declare JDBC objects
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish the connection
conn = DriverManager.getConnection(jdbcUrl, jdbcUser,
jdbcPassword); // Create a statement
stmt = conn.createStatement();
// Execute a query to retrieve data
String sql = "SELECT * FROM userInfo";
rs = stmt.executeQuery(sql);
// Process the result set and display data in input text boxes
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("email");
// Output user data in text boxes
%>
<tr>
<td><input type="text" name="id" value="<%= id %>" readonly></td>
<td><input type="text" name="username" value="<%= username %>"
readonly></td>
<td><input type="text" name="password" value="<%= password %>"
readonly></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close JDBC resources
if (rs != null) try { rs.close(); } catch (SQLException e) {
e.printStackTrace(); } if (stmt != null) try { stmt.close(); } catch
(SQLException e) {
e.printStackTrace(); }
if (conn != null) try { conn.close(); } catch (SQLException e) {
e.printStackTrace(); }
}
%>
</table>
<%
} // End of form submission check
%>
</body>
</html>

Create Database conn;


Use conn;
Create Table admin(
id int primary key,
username varchar(50),
password varchar(50)
);
Insert into admin values(10, “ABC”,”ok”);
Insert into admin values(11, “Abhi”, “Bull”);
Insert into admin values(12,”virat”,”kohli”);
Select * from admin;
Output:

Hamza Selanawala
SY\I.T.\B\37

You might also like