T4_L7_Host_Program_Java-2
T4_L7_Host_Program_Java-2
T4_L7_Host_Program_Java-2
https://dev.mysql.com/doc/connector-j/8.0/en/
https://dev.mysql.com/doc/connector-
python/en/connector-python-reference.html
https://pymysql.readthedocs.io/en/latest/
Embedding SQL
SQL commands can be called from within a host language (e.g., C++ or Java) program.
SQL statements can refer to host variables (including special variables used to return
status).
Two main integration approaches:
• Embed SQL in the host language (Embedded SQL, SQLJ). A Preprocessor
converts SQL code to host language calls. The output from the preprocessor is then
compiled by the host compiler
• Create special API to call SQL commands
JDBC Java Database Connectivity API (for JAVA)
http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/
ODBC Standard database connectivity API Pep 249 – Python Database Application
specification https://www.python.org/dev/peps/pep-0249/
Embedded SQL
JDBC
Mysqli or PDO ADO.NET
MySQL
Database (API)s
Add a library with database calls (API)
Special standardized interface: procedures/objects
Pass SQL strings from host language, presents result sets in a host
language-friendly way
GO TO:
https://www.mysql.com/products/connector/
MySQL Drivers
• Connector/ODBC provides driver support for connecting to MySQL using the Open Database
Connectivity (ODBC) API.
• Connector/Net enables developers to create .NET applications that connect to MySQL.
Connector/Net implements a fully functional ADO.NET interface and provides support for use
with ADO.NET
• Connector/J provides driver support for connecting to MySQL from Java applications using the
standard Java Database Connectivity (JDBC) API.
• Connector/Python provides driver support for connecting to MySQL from Python applications
using an API that is compliant with the Python DB API version 2.0.
http://dev.mysql.com/doc/connector-python/en/
• Connector/C++ enables C++ applications to connect to MySQL.
• Connector/C is a standalone replacement for the MySQL Client Library (libmysqlclient), to be
used for C applications.
JDBC Processing (Java)
Steps to submit a database query:
//Get a connection to the database for a user named root with a xxxx password.
Connection con = DriverManager.getConnection( url,"root", “xxxx");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
Connection class interface
public int getTransactionIsolation() and
void setTransactionIsolation(int level)
Sets isolation level for the current connection.
public boolean getReadOnly() and void setReadOnly(boolean b)
Specifies whether transactions in this connection are readonly
public boolean getAutoCommit()
and void setAutoCommit(boolean b)
If autocommit is set, then each SQL statement is considered its own
transaction. Otherwise, a transaction is committed using commit(), or
aborted using rollback().
public boolean isClosed()
Checks whether connection is still open.
Executing SQL statements
Three different methods to execute SQL statements:
Statement (both static and dynamic SQL statements)
PreparedStatement (semi-static SQL statements)
CallableStatment (stored procedures)
ResultSet rs=pstmt.executeQuery(sql);
// rs is now a cursor
While (rs.next()) {
// process the data }
ResultSet: Cursor with seek functionality
A ResultSet is a very powerful cursor:
previous(): moves one row back
absolute(int num): moves to the row with the specified number
relative (int num): moves forward or backward
first() and last()
DatabaseMetaData md = con.getMetaData();
// print information about the driver:
System.out.println(
“Name:” + md.getDriverName() +
“version: ” + md.getDriverVersion());
Metadata- print out tables and fields
DatabaseMetaData md=con.getMetaData();
ResultSet trs=md.getTables(null,null,null,null);
String tableName;
While(trs.next()) {
tableName = trs.getString(“TABLE_NAME”);
System.out.println(“Table: “ + tableName);
//print all attributes
ResultSet crs = md.getColumns(null,null,tableName, null);
while (crs.next()) {
System.out.println(crs.getString(“COLUMN_NAME” + “, “);
}
}
http://docs.oracle.com/javase/10/docs/api/java/sql/DatabaseMetaData.html
Connect, Process, check for errors
Connection con = // connect
DriverManager.getConnection(url, ”login", ”pass");
Statement stmt = con.createStatement(); // set up stmt Connect
String query = "SELECT name, rating FROM Sailors";
ResultSet rs = stmt.executeQuery(query);
try { // handle exceptions
// loop through result tuples
Get multiset
while (rs.next()) {
String s = rs.getString(“name");
Int n = rs.getFloat(“rating");
System.out.println(s + " " + n);
Process with cursor
}
} catch(SQLException ex) {
System.out.println(ex.getMessage () +
ex.getSQLState () + ex.getErrorCode ());
Catch Errors
}
Java documentation
For documentation refer to:
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-
examples.html
Java Summary
APIs such as JDBC introduce a layer of abstraction between
application and DBMS
Embedded SQL allows execution of parameterized static
queries within a host language
Dynamic SQL allows execution of completely ad hoc queries
within a host language
Cursor mechanism allows retrieval of one record at a time
and bridges impedance mismatch between host language
and SQL