2. • You need MySQL database before proceeding.
• After installation, run the PHPMyAdmin from XAMPP Control Panel.
• Type: http://localhost/phpmyadmin in URL of browser.
• Now you have to create database in XAMPP Server. Create it.
• Now it’s time to add JDBC library. Visit following link.
• https://dev.mysql.com/downloads/connector/j/ To download official JDBC driver for MySQL. Unzip it. It is MySQL
JDBC Driver.
• Now configure the above file. For this purpose, put this file into your desired folder, and set ClassPath.
• Suppose we put it in a folder named MySQLConnector in c drive. The path is:
• C:MySQLConnector
• Accordingly, set CLASSPATH variable to C:MySQLConnectormysql-connector-java-5.1.8-bin.jar
• Your jdk path and driver versions of both jdk and driver may change.
• Through CommandPrompt you can compile and run your file:
• javac filename.java
• java –classpath .; C:MySQLConnectormysql-connector-java-5.1.8-bin.jar filename
Database Connectivity – Prerequisites
Instructor: Tanzila Kehkashan
2
3. JDBC Programming
• Java Database Connectivity (JDBC) is a Java-based data access
technology that defines how a client may access a database. It provides
methods for querying and updating data in a database. The JDBC
classes are contained in the Java package java.sql and javax.sql.
• JDBC (Java Database Connectivity) is the specification of a Java API
that allows Java programs to access DBMSs.
• The JDBC API consists of a set of interfaces and classes written in the
Java programming language.
• Using these standard interfaces and classes, programmers can write
applications that connect to databases, send queries written in
structured query language (SQL), and process the results.
JDBC Programming
Steps
1) Create a
Connection to the
database
1) Create a Statement
2) Query the database
1) Close the result set
2) Close the statement
3) Close the connection
Connect
Query
Process Results
Close
Register 1) Register the
driver
1) Get a ResultSet
2) Assign results to
Java variables
Instructor: Tanzila Kehkashan
3
4. • A JDBC driver is needed to connect to a database.
• Registering a driver requires the class name of the driver.
• Example) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver
• Oracle driver: oracle.jdbc.driver.OracleDriver
• MySQL: com.mysql.jdbc.Driver
Loading the driver class
• java.lang.Class (no need to import)
• Class.forName(String);
• It is possible to load several drivers.
• The class DriverManager manages the loaded driver(s).
Example
Execptions by java.lang.ClassNotFoundException
• com.mysql.jdbc.Driver
• This exception comes if your driver is not cofigured. (classpath is wrongly set)
Database Connectivity – STEP 1: Register
Class.forName("com.mysql.jdbc.Driver");
Instructor: Tanzila Kehkashan
4
5. JDBC URL for a database
• Identifies the database to be connected
• Consists of three-parts:
• <protocol>:<subprotocol>:<subname>
• jdbc:<DBMS>://<HOSTNAME>:<PORT_NUMBER>/YOUR_DATABASE_NAME
java.sql.DriverManager
• You automatically get both JDBC packages java.sql and javax.sql, when you install JDK.
• The DriverManager allows you to connect to a database using the specified JDBC driver, database location,
database name, username and password.
• It returns a Connection object which can then be used to communicate with the database.
• Connection getConnection(String) //url
• Connection getConnection(String, String, String) //url, username, password
• Protocol: JDBC is the
only protocol in JDBC
• Protocol: JDBC is the
only protocol in JDBC
• Subname: indicates the location and name
of the database to be accessed. Syntax is
driver specific
• Subname: indicates the location and name of the database to be
accessed. Syntax is driver specific. DSN name should be used.
• Sub-protocol: identifies
a database driver
Database Connectivity - STEP 2: Connect
Instructor: Tanzila Kehkashan
5
6. Database Connectivity - STEP 2: Connect
Example
Exceptions
• getConnection method can throw the following exception:
• com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure
• Or
• SQLException: Connection refused or SQLException: Connection timed out
• All the possible causes are:
• IP address or hostname in JDBC URL is wrong.
• Hostname in JDBC URL is not recognized by local DNS server.
• Port number is missing or wrong in JDBC URL.
• DB server is down.
• DB server doesn't accept TCP/IP connections.
• Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
String strDbURL="jdbc:mysql://localhost:3306/testdb";
Connection con=DriverManager.getConnection(strDbURL,"root","");
Instructor: Tanzila Kehkashan
6
7. java.sql.Connection
• Statement createStatement()
• Statement createStatement(int resultSetType, int resultSetConcurrency)
Example
java.sql.Statement
• ResultSet executeQuery(String) //SQL queries of DML that returns a resultset
• int executeUpdate(String) //SQL DDL queries that returns no resultset (for example; INSERT, UPDATE, DELETE)
SQL Queries
• The WHERE clause is used to extract only those records that fulfill a specified criterion.
• SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Example
Database Connectivity - STEP 3: Query
Statement stmt = con.createStatement(); //or
//stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs=stmt.executeQuery("select * from stubio");
Instructor: Tanzila Kehkashan
7