Вы находитесь на странице: 1из 5

TP 10

Accessing Data Bases with JDBC


10.1 What is JDBC ?
JDBC (Java Data Base Connectivity) provides a standard library for accessing
relational databases. By using the JDBC API, you can access a wide variety of
SQL databases with exactly the same Java syntax. It is important to note that
although the JDBC API standardizes the approach for connecting to databases,
the syntax for sending queries and committing transactions, and the data
structure representing the result, JDBC does not attempt to standardize the
SQL syntax.
According to the database management system (Oracle , MySQL, Access,
FoxPro, DB2) we have two types of JDBC driver implementations (Fig 1):
a. JDBC-ODBC bridge (for Access, DB2, FoxPro)
b. pure Java implementation (for MySQL, Oracle)

Fig 1. JDBC driver implementation

10.2. Database Connection


There are two stages:
1. Uploading the class of the driver in the memory
2. Executing the connection
Import the libraires java.sql.* and javax.sql.*
Giving the data base location :
jdbc : protocol : data_base_identifier
where
protocol is the type of the driver (oracle, odbc, db2)
data_base_identifier is the host name, port number and data base url
Examples :
for ORACLE :
jdbc:oracle:myhost:1521:testdb
for MySQL :
jdbc:mysql://localhost:3306/testdb
for MySQL Server:
jdbc:odbc:testdb
Java classes used for data base connections are:
java.sql.DriverManager (connecting the application with data base)
java.sql.Connection (connecting with user and password)
java.sql.Statement (for SQL queries)
java.sql.ResultSet (for execution of the query)
java.sql.DataSource

10.3. Downloading the JDBC driver for MySQL


Go to :
http://dev.mysql.com/downloads/connector/j
and download the file for Windows 32.
From the downloaded .zip file we will use only the
mysql-connector-java-5.1.34-bin.jar
10.4. Creating the data base
1. Verify if you have XAMPP installed on your computer. If not, install it from
https://www.apachefriends.org/fr/index.html
Run XAMPP PHPMyAdmin
2. Create your data base starting from the given XML file using PHPMyAdmin
XML file name is libraries.xml from Nedra\LibrairieServices\src

Name your database testDB


Dont forget to connect the two tables of the database
10.5. Create the Java Project
The name of the project is TestJDBC

Add a new class in the project named Driver and check the option for
including the main () function.
Add the driver downloaded earlier creating first a new folder named lib in
your project (right click on the name of the project) and in this folder add the
mysql-connector-java-5.1.34-bin.jar (drag and drop)
Right click on the name of the project testJDBC and Properties/Java Build
Path/Add JARs (from label Libraries) and select .jar from lib, then OK.

Now we can write the Java code for a simple query:


import java.sql.*;

public class Driver {


/**
* @param args
*/
public static void main(String[] args) {
try{
//DB connection
Connection myConn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "pass");
//Query
Statement myStmt = myConn.createStatement();
//Execution
ResultSet myRs = myStmt.executeQuery ("select * from book");
//Processing results
while (myRs.next())
{ System.out.println(myRs.getInt("idBook")
+","+myRs.getString("Name")+","+ myRs.getString("Author"));}
}catch (Exception e){
e.printStackTrace();
}
}
}

Be aware that the password for the root is you that establish!
10.6. Inserting records
You can use another project and insert the following code:
import java.sql.*;
public class Driver {
/**
* @param args
*/
public static void main(String[] args) {
try{

//DB connection
Connection myConn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "pass");
//Query

Statement myStmt = myConn.createStatement();

//Execution
String sql = "insert into book(idBook, Name, Author) values ('3','The Brothers Karamazov',
'Dostoevsky')";
myStmt.executeUpdate(sql);

System.out.println ("Insert complete");


}catch (Exception e){
e.printStackTrace();
}
}
}

TO DO: Write the Java code for updating and deleting records from your
database.

Вам также может понравиться