Tutorial: Advanced Java Programming and Database Connection
Tutorial: Advanced Java Programming and Database Connection
• try block:
try {
System.out.println("Entering try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
}
for (int i = 0; i < size; i++)
out.println("Value at: " + i + " = " + victor.elementAt(i));
5
The catch Block
6
Catching Multiple Exceptions
Exception
MyException
MySpecificException2 MySpecificException1
} catch (Exception e) {
System.err.println("Exception caught: " + e.getMessage());
}
8
The finally Block
9
Putting it All Together
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < size; i++)
out.println("Value at: " + i + " = " + victor.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException: " +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
10
throw Statement
• All Java methods use the throw statement to
throw an exception
• The method must declare that it might throw
something, by using the throws statement
public Object pop() throws EmptyStackException {
Object obj;
if (size == 0)
throw new EmptyStackException(“exception text”);
11
Exceptions and JavaDoc
/**
* regular javadoc text…
* @throwsExceptionIf the Driver was not found.
* @throwsSQLExceptionIf the the <code>DriverManager.getConnection
* </code> method returned an error.
*/
public void createConnection()throws SQLException, Exception{
12
Agenda
13
JDBC
• Types
14
Type 1
• The type1 driver translates all jdbc calls into odbc calls
and sends them to the odbc driver.
15
Advantage/Disadvantage
16
Type 2
17
Advantage/Disadvantage
18
Type 3
19
Advantage/Disadvantage
20
Type 4
21
Advantage/Disadvantage
22
Accessing Database
23
Example – Database Management
mysql> create database mytest; Creating the DB
Query OK, 1 row affected (0.05 sec)
mysql> grant all on *.* to eran@localhost identified by '1234';
Query OK, 0 rows affected (0.14 sec) Creating user account
mysql>create table phones (name varchar(255) not null unique key, phone
varchar(25) not null);
Creating the ‘phones’
mysql>describe phones;
table
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+ Is everything alright?
| name | varchar(255) | | PRI | | | Let’s see…
| phone | varchar(25) | | | | |
+-------+--------------+------+-----+---------+-------+
mysql> insert into phones values ('Eran Toch', '+972-4-9831894');
Query OK, 1 row affected (0.11 sec)
Inserting some data
24
Example – Connection
import java.sql.*;
Importing java.sql.* that
public class SQLConnect { contains all the classes we
Connection conn = null; need
Statement stmt = null;
ResultSet rs = null; Connection, Statement and
public SQLConnect(){}
ResultSet are defined as
public void createConnection(){ class variables
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E){ Dynamically loading the specific
System.out.println(E); JDBC driver. The runtime environment
} must know where the library is located!
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test” , user
,password);
}
catch (SQLException E){
Connecting to the database using
System.out.println(E); the url
}
}
25
Example – Access and Query
Creating a statement
public String getPhones(){
String output = ""; Creating a ResultSet, based
try {
on a SQL statement
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM phones");
if (rs != null){
while (rs.next()){ Going through the
output += rs.getString("phone") + "\n"; ResultSet by using
} rs.next(). Remember – you
} need to call the next method
} before you start reading from the
catch (Exception E){ ResultSet
System.out.println(E.getMessage());
}
26