Ch14 Databases Connection in Java
Ch14 Databases Connection in Java
Database Connection
in Java
DEFINITIONS
Table
Record
COURSE INFORMATION TABLE
STUDENT TABLE
RELATIONAL DATABASE
update tableName
set column1 = newValue1 [, column2 = newValue2, ...] [where condition];
Ex:
update Course
set numOfCredits = 4
where title = 'Database Systems’;
Ex:
delete from Course
where title = 'Database Systems';
Get the names of the students who are in the CS dept and live in the
ZIP code 31411.
To select all the attributes from a table, you don’t have to list all the
attribute names in the select clause. Instead, you can just use an asterisk
(*), which stands for all the attributes.
Ex:
select *
from Student
where deptId = 'CS' and zipCode = '31411';
QUERY STATEMENTS
Ex:
select ssn
from Enrollment
where grade between 'A' and 'C';
QUERY STATEMENTS
Loading drivers
Establishing connections
Processing ResultSet
44
The JDBC Interfaces
1. Loading driver
2. Establishing connection
3. Creating statement
4. Executing statement
5. Processing result set
6. Closing the connection
1- LOADING DRIVER
An appropriate driver must be loaded using the statement shown below before connecting to
a database. Class.forName("JDBCDriverClass");
Class Class<T> has a method named forName() that used to
Load the Class object associated with the class or interface with the given string name
A driver is a concrete class that implements the java.sql.Driver interface.
JDBC Drivers for each different database
JDBC drivers are database specific and are normally provided by the database vendors.
1- LOADING DRIVER(Cont.)
Statement to load a driver:
Class.forName("JDBCDriverClass");
A driver is a class. For example:
Examples:
For Access:
Connection connection =
DriverManager.getConnection("jdbc:odbc:ExampleMDBDataSource");
For MySQL:
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/test“,"scott", "tiger");
For Oracle:
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@liang.armstrong.edu:1521:orcl",
"scott", "tiger");
For MySql Database
Setting up MySql Databases server
HeidiSQL is a powerful and easy client for MySQL and MSSQL. Open
source and completely free to use.
EXERCISE: Review from previous slides
Suppose a data source named javabook has been created in local host MySQL
with username scott and password tiger, how to establish connection?
Answer
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/javabook", "scott", "tiger");
3- CREATING STATEMENTS
A cable linking the program to a database, it played as a card that deliver SQL
statements for execution by the database.
Connection Interface: located in package java.sql and contains a method
createStatement() that create Statement object for sending SQL
statements to the database.
public Statement createStatement() throws SQLException
The code:
Statement statement = connection.createStatement();
4- EXECUTING STATEMENTS
Statement Interface: located in package java.sql and contains a two
methods named executeUpdate() and executeQuery(), which take the sql
command as String that will be executed on database.
public int executeUpdate(String sql) throws SQLException
public ResultSet executeQuery(String sql) throws SQLException
Update statements (Runs the given SQL statement, which can be an INSERT, UPDATE, DELETE)
statement.executeUpdate ("create table Temp (col1 char(5), col2
char(5))");
Execute query
ResultSet resultSet = statement.executeQuery ("select firstName, mi,
lastName from Student where lastName = 'Smith' ");
4- EXECUTING STATEMENTS(Cont.)
Creating statement:
Statement statement = connection.createStatement();
connection.close();
EXAMPLE
import java.sql.*;
public class SimpleJdbc {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost/javabook" , "scott", "tiger");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery ("select firstName, mi, lastName from Student where
lastName = 'Smith'");
while (resultSet.next())
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) + "\t" + resultSet.getString(3));
connection.close();
}
catch(Exception ex){
System.out.println(ex);
}
}
}