[go: up one dir, main page]

0% found this document useful (0 votes)
94 views12 pages

JDBC

JDBC, or Java Database Connectivity, is an API that enables Java applications to interact with various databases using a set of classes and interfaces. To connect to a MySQL database, specific information such as the driver class, connection URL, username, and password is required. Key classes in JDBC include Connection, Statement, and ResultSet, which facilitate database operations and transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views12 pages

JDBC

JDBC, or Java Database Connectivity, is an API that enables Java applications to interact with various databases using a set of classes and interfaces. To connect to a MySQL database, specific information such as the driver class, connection URL, username, and password is required. Key classes in JDBC include Connection, Statement, and ResultSet, which facilitate database operations and transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JDBC

●The term JDBC stands for Java Database


Connectivity and it is a specification from Sun
microsystems.
●2. JDBC is an API (Application programming
interface) in Java that helps users to interact
or communicate with various databases.
●3. The classes and interfaces of JDBC API
allow the application to send the request to
the specified database.
●4. Using JDBC, we can write programs
required to access databases. JDBC and
the database driver are capable of
accessing databases and spreadsheets.
●5. Interacting with the database requires
efficient database connectivity, which we can
achieve using ODBC (Open database
connectivity) driver. We can use this ODBC
Driver with the JDBC to interact or
communicate with various kinds of
databases, like Oracle, MS Access, MySQL,
and SQL, etc.
JDBC CONNECTION
●To connect Java application with the MySQL
database, we need to follow 5 following
steps.
●In this example we are using MySql as the
database. So we need to know following
informations for the mysql database:
●1. Driver class: The driver class for the
mysql database is com.mysql.jdbc.Driver.
● 2. Connection URL: The connection URL for the
mysql database is
jdbc:mysql://localhost:3306/lakshman where jdbc is
the API, mysql is the database, localhost is the
server name on which mysql is running, we may also
use IP address, 3306 is the port number and aditya is
the database name. We may use any database, in
such case, we need to replace the aditya with our
database name.
● 3. Username: The default username for the mysql
database is root.
● 4. Password: It is the password given by the user at
the time of installing the mysql database. In this
example, we are going to use root as the password.
Key Classes in JDBC
●Connection
● need to create an instance of this class when
establishing a connection to the database
●Statement
● for issuing SQL statements
●ResultSet (interface)
● a ResultSet object represents the table returned
by an SQL select statement
Establishing a Connection

● import java.sql.*;
● class MysqlCon
● {
● public static void main(String args[])
● {
try
● {
● Class.forName("com.mysql.jdbc.Driver");
● Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/lakshman","root","root");
● //here lakshman is database name, root is username and password
Creating Statement
● Statement stmt=con.createStatement();
● ResultSet rs=stmt.executeQuery("select * from emp");
● while(rs.next())
● System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
● con.close();
● }
● catch(Exception e)
● {
● System.out.println(e);
● }
● }
● }
Transaction Properties
● import java.sql.*;
● class FetchRecords
● {
● public static void main(String args[])throws Exception
● {
● Class.forName("oracle.jdbc.driver.OracleDriver");
● Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","s
ystem","oracle");
● con.setAutoCommit(false);
● Statement stmt=con.createStatement();
● stmt.executeUpdate("insert into user420 values(190,'abhi',40000)");
● stmt.executeUpdate("insert into user420 values(191,'umesh',50000)");
● con.commit();
● con.close();
● }}
Summary
●JDBC allows you to write Java programs that
manipulate a database
●A driver (often a separate product) is required
that facilitates access
●Key classes: Connection, Statement,
PreparedStatement, and ResultSet
●Other features: metadata and stored-proc
invocation

You might also like