Advanced Java Slides
Advanced Java Slides
Generics
Sequential Collections
Associative Collections
Exception Handling
String and String Builder
Generics
Data Source
JDBC
JDBC Driver
API
Typical JDBC Programming Procedure
• Class.forName (“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
• C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib
• right click on your Java Project -> Properties -> Buildpath -> Libraries -> Add
External JAR and select the odbc6.jar file.
Java Database Connectivity with
MySQL
• Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
• Connection URL: The connection URL for the mysql
database is jdbc:mysql://localhost/DbName 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 sonoo is the database name.
We may use any database, in such case, we need to replace
the sonoo with our database name.
• Username: The default username for the mysql database
is root.
• Password: It is the password given by the user at the time
of installing the mysql database.
Configure MySql to JDBC programs
• To connect java application with the mysql
database, mysqlconnector.jar file is required
to be loaded.
• Download this file and add it as external jar to
Java Application
• https://dev.mysql.com/downloads/file/?id=48
0091
• Example of Insert, Select using Oracle DB
Day 3
• JDBC using MySql
• Threads
• Serialization
• Regular Expression
• Additional Java Features
Threads
• Threads Overview
• Creating threads in Java
• Synchronization
• wait() and notify()
• sleep
• Thread Pools
• Exercise
Threads Overview
– Threads allow the program to run tasks in parallel
– In many cases threads need to be synchronized,
that is, be kept not to handle the same data in memory concurrently
– There are cases in which a thread needs to wait for another thread before
proceeding
68
Threads in Java
Option 1 – extending class Thread (cont’):
@Override
public void run() {
}
}
69
Threads in Java
Option 2 – implementing Runnable:
@Override
public void run() {
}
}
70
Synchronization
Synchronization of threads is needed for in order to control threads
coordination, mainly in order to prevent simultaneous operations on data
71
wait() and notify() allows a thread to
wait for an event
• What is concurrency?
• Concurrency is the ability to run several programs
or several parts of a program in parallel. If a time
consuming task can be performed
asynchronously or in parallel, this improve the
throughput and the interactivity of the program.
• A modern computer has several CPU’s or several
cores within one CPU. The ability to leverage
these multi-cores can be the key for a successful
high-volume application.
• The Stream API enables developers to create
the parallel streams that can take advantage
of multi-core architectures and enhance the
performance of Java code. In a parallel stream,
the operations are executed in parallel and
there are two ways to create a parallel stream.
• Using the parallelStream() method on a
collection
• Using the parallel() method on a stream
• Parallel Streams must be used only
with stateless, non-interfering, and associative
operations i.e.
• A stateless operation is an operation in which the
state of one element does not affect another
element
• A non-interfering operation is an operation in
which data source is not affected
• An associative operation is an operation in which
the result is not affected by the order of operands
When to use Parallel Streams