Beruflich Dokumente
Kultur Dokumente
Content
Introduction of Java Introduction of JDBC JDBC API
Introduction of Java
Java evolved from C++. They have similar syntax and both support object-oriented programming. The output of Java compiler is not executable code but bytecode. It is run on Java virtual machine (JVM).
Introduction of Java
Java programs are more portable and secure. Java is the language of Internet There are some significant differences between Java and C++. For example Java does not support pointers.
Requirements in HW2
How to use Java to create a Graphical User Interface (GUI) for a spatial DBMS It can display spatial data and query results It can handle the events from the mouse or keyboard The related classes include JFrame, JComponent, JPanel, Jlabel, JCheckBox, JTextField, JRadioButton, etc. They are in javax.swing and java.awt packages.
What is JDBC
Java DataBase Connectivity It enables the integration of SQL with Java The application programmer access database capabilities through an application programming interface (API)
What is JDBC
In contrast to embedded SQL, it allows a single executable to access different DBMSs without recompilation An application can access several different DMBSs simultaneously
JDBC architecture
JDBC achieves the portability by using DBMS-specific drivers A driver is a software program that translates the JDBC calls into DBMSspecific calls
JDBC architecture
Available drivers are registered with a driver manager Drivers are loaded dynamically at run-time
JDBC API
The classes and interfaces are part of the java.sql package. So:
import java.sql.*;
JDBC API
Connection to the DBMS:
url = jdbc:oracle:thin:@shams.usc.edu:1521:CSCI585; userId = vivksin; Password = v578657 Connection con = DriverManager.getConnection(url, userId, password);
Close Connection:
con.close();
JDBC API
Set connnection properties, for example:
Connection.SetAutoCommit(true); //each SQLstatement is a transaction
JDBC API
Execute SQL statements:
String sql = SELECT title From Books WHERE year = 2005; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();
Or
String sql = SELECT title From Books WHERE year = 2005; Statement stmt = con.createStatment(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(sql);
JDBC API
ResultSets:
Statement stmt = con.CreateStatement(); ResultSet rs = stmt.executeQuery(sql); //rs is a cursor While(rs.next()){ title = rs.getString(TITLE); }
JDBC API
SQL Type BIT CHAR VARCHAR DOUBLE INTEGER FLOAT Java class Boolean String String Double Integer Double Get method getBoolean() getString() getString() getDouble() getInt() getDouble()
JDBC API
Most methods in java.sql can throw an exception of the type SQLException if an error occurs. It provides getMessage(), getSQLState() and getErrorCode() to retrieve the error information.
JDBC API
Connection, statement and ResultSet objects all have a getWarnings() method to retrive SQL warnings if they exist.