180 likes | 343 Views
Using Java with PSQL and Oracle. Thanks to Drs. Raj and Liu for sharing some of these slides TRUDY: ALSO NEED SAMPLE PROGRAM. In this slide deck:. Tips for using PSQL and Oracle Some “gotchas” for working with the Java database interface. psql.
E N D
Using Java with PSQL and Oracle Thanks to Drs. Raj and Liu for sharing some of these slides TRUDY: ALSO NEED SAMPLE PROGRAM
In this slide deck: • Tips for using PSQL and Oracle • Some “gotchas” for working with the Java database interface
psql • You will need a separate psql account – see your instructor • Server runs on reddwarf • You need to provide your password each time you invoke psql
psql • Full documentation and a tutorial are available online • Default is a command line interface that isn’t very forgiving with typos or to reissue commands • Several UI skins exist; some are open source – check around or write your own
psql passwords • The psql interpreter will bypass the password prompt if it sees a .pgpass file in your home directory • Enter a single line: localhost:*:*:Your_Username:Your_Password (host:port:db:username:password) • IMPORTANT – SEE NEXT SLIDE
psql passwords • Be sure to chmod 600 to set permissions on the file • Our system admin has approved storing this plain-text password IF you do not use the same password as your CS account • DO NOT USE THE SAME PASSWORD AS YOUR CS ACCOUNT!
Getting started with psql • Assuming you use bash: • Edit your .bashrc file to include the following: export path=/usr/lib/postgresql/9.1/bin/:$path You will also need this for the Java JDBC: exportCLASSPATH=/home/student/abc1234/db/ postgresql-9.1-902.jdbc4.jar:$CLASSPATH (all one line. Substitute “/home/student/abc1234” with your own path name
Integrating psql with Java • You also need the .jar file in your working directory. See my website download page for the link. • Review demo program • Note that much of your understanding will be achieved through experimentation and studying the appropriate javadocs!
Setting Java Environment for Oracle • To use sqlplus and JDBC from a Java program • Ensure CLASSPATH is set up correctly • csh users can source the following file • /usr/local/bin/coraenv • Put it in your .cshrc file • bash users can source the following file • /usr/local/bin/oraenv • Put it in your .bashrcfile • Sample Java program • See course webpage under DBMS Docs • You must modularize this program! Understand password privacy issues involved here!
Using Oracle @ the CS Department • Connecting to Oracle directly • sqlplus • Use this primarily for running DDL scripts • Connecting to a DBMS with a Java program • Use the Oracle JDBC driver • Read CRUD and the javadocs • Demonstration
Java Classes/Interfaces: java.sql, javax.sql Some useful classes and methods • DriverManager • getConnection • Connection • createStatement, prepareStatement, commit, setAutoCommit, close • Statement • executeQuery, executeUpdate, close • PreparedStatement • execute, commit, close, setint, setString • ResultSet • next, getString, getInt
JDBC Introduction • Useful to read the following SUN’s JDBC tutorial • java.sun.com/docs/books/tutorial/index.html • Steps for Oracle (similar for any other DBMS) • Load the driver • Connect to the DBMS • Send SQL to the DBMS • Process the results of your queries • Cleanup
Load the Driver and Connect to Oracle • Several steps rolled into one Class.forName( "oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@queeg:1521:csodb10"; Connection con = DriverManager.getConnection( url, dbUser, dbPassword );
Send SQL to the DBMS • Just send the SQL … stmt.executeUpdate("insert into A values(2009)" ); • Expect some goodies back String query = "select name, salary, emp_no from employee"; ResultSetrs = stmt.executeQuery( query );
Process Query Results • Get the result set ResultSet rs = stmt.executeQuery( query ); • Process the result set, row by row while ( rs.next() ) { String emp_name = rs.getString( "name“ ); int emp_no = rs.getInt( "emp_no“ ); double salary = rs.getDouble( "salary“ ); System.out.println( emp_name + ", " + emp_no + “ makes $" + salary + “.“ ); }
Data is representedin two places In the database SQL type DB programmer responsibility In Java program Java type Java programmer responsibility get??? calls Java and SQL: Type Mappings
Cleanup • Close out the statement and connection stmt.close( ); con.close( );