1 / 19

CS422 Principles of Database Systems Embedded SQL and JDBC

CS422 Principles of Database Systems Embedded SQL and JDBC. Chengyu Sun California State University, Los Angeles. Applications and Databases. Users. Applications. Databases. C/C++, Java, PHP, Perl. SQL. ?. Call-level Interface (CLI). A library of functions that allow applications to

bynum
Download Presentation

CS422 Principles of Database Systems Embedded SQL and JDBC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS422 Principles of Database SystemsEmbedded SQL and JDBC Chengyu Sun California State University, Los Angeles

  2. Applications and Databases Users Applications Databases C/C++, Java, PHP, Perl... SQL ?

  3. Call-level Interface (CLI) • A library of functions that allow applications to • connect to the database • send SQL statements to the database • get the results back from the database String url = "jdbc:mysql:///csun?user=csun&password=abcd"; Connection c = DriverManager.getConnection( url ); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery( "select * from Products"); while( rs.next() ) { // some processing here }

  4. Embedded SQL • Embed SQL statements directly in host language (specifically, C) • Check SQL syntax at compilation time • Variables shared by SQL and the host language • Part of the SQL standard

  5. ECPG Example • artists.pgc • EXEC SQL • Compilation • Preprocessing: .pgc  .c • Compile: .c  .o • Link: .o  executable • -lecpg

  6. condition SQLERROR SQLWARNING NOT FOUND action CONTINUE STOP SQLPRINT GOTO label CALL name ( args ) Error Handling EXEC SQL WHENEVER condition action;

  7. Connections • target • dbname@hostname • user • username USING password EXEC SQL CONNECT TO target USER user; EXEC SQL DISCONNECT;

  8. Shared Variables • Shared (host) variables • Referenced as :variable_namein SQL statements • Referenced as variable_name in host language EXEC SQL BEGIN DECLARE SECTION; EXEC SQL END DECLARE SECTION;

  9. SELECT INTO • Needs indicator to handle null values • 0: result not null • negative: null • positive: result truncated • Only for single result EXEC SQL SELECT col(s) INTO variable(s) WHERE cond(s);

  10. Cursor and FETCH INTO EXEC SQL DECLARE name CURSOR FOR query; EXEC SQL OPEN name; do { ... EXEC SQL FETCH NEXT FROM name INTO variables; ... } while ( strcmp(sqlca.sqlstate,”02000”) != 0 ); • SQLSTATE and sqlca • http://www.postgresql.org/docs/7.4/static/ecpg-errors.html#AEN23727 • http://www.postgresql.org/docs/7.4/static/errcodes-appendix.html

  11. Updates • update could be any update statement • create table, insert, delete, update ... • Examples: EXEC SQL update; EXEC SQL COMMIT; EXEC SQL CREATE TABLE foo (number integer, ascii char(16)); EXEC SQL CREATE UNIQUE INDEX num1 ON foo(number); EXEC SQL COMMIT; ... ... EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad'); EXEC SQL COMMIT;

  12. Dynamic SQL • When SQL statements themselves are generated by the host language EXEC SQL BEGIN DECLARE SECTION; char *stmt; EXEC SQL END DECLARE SECTION; EXEC SQL PREPARE name FROM :stmt; ... EXEC SQL EXECUTE name [INTO variables] [USING values];

  13. JDBC • An interface between Java programs and SQL databases Java program JDBC Driver MySQL JDBC API com.mysql.jdbc.Driver

  14. JDBC Basics … • import java.sql.*; • Load driver • Class.forName(“org.postgresql.Driver") • Create connection • Connection c = DriverManager.getConnection( url, username, password ); • URL • jdbc:postgresql://hostname/dbname

  15. … JDBC Basics • Create statement • Statement stmt = c.createStatement(); • stmt.executeQuery() • stmt.executeUpdate() • Get result back • ResultSet rs http://java.sun.com/j2se/1.3/docs/guide/jdbc/

  16. In a program, we want to Access each row Access column in a row Access column names DB Query Results select * from UserInfo;

  17. JDBC ResultSet – Row Access • next() – move cursor down one row • true if the current row is valid • false if no more rows • Cursor starts from before the 1st row

  18. JDBC ResultSet – Column Access • Access the columns of current row • getXxx( String columnName ) • E.g. getString( “user” ); • getXxx( int columnIndex ) • columnIndex starts from 1 • E.g. getString( 1 );

  19. JDBC ResultSet – Access Column Names • ResultSetMetaData • getColumnName( columnIndex ) • Column name • getColumnLabel( columnIndex ) • Column title for display or printout ResultSetMetaData meta = rs.getMetaData();

More Related