Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java for Interfaces and Networks Java for Interfaces and Networks (DT3010, HT11) Java Database Connectivity (JDBC) Federico Pecora School of Science and Technology Örebro University [email protected] Federico Pecora – Java for Interfaces and Networks – Lecture 6 1 / 16 Java for Interfaces and Networks Outline 1 Java and DBMSs: background 2 Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: updating 3 Applets and JDBC Security issues Federico Pecora – Java for Interfaces and Networks – Lecture 6 2 / 16 Java for Interfaces and Networks Java and DBMSs: background Outline 1 Java and DBMSs: background 2 Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: updating 3 Applets and JDBC Security issues Federico Pecora – Java for Interfaces and Networks – Lecture 6 3 / 16 Java for Interfaces and Networks Java and DBMSs: background Java and DBMS The Java Database Connectivity (JDBC) API allows database-independent connectivity between the Java programming language and a wide range of DBMSs Federico Pecora – Java for Interfaces and Networks – Lecture 6 4 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Database: collection of data managed by a Database Management System (DBMS) Relational database: one or more tables, each with with zero or more rows and one or more columns Each table has a name Each column has a name The contents of a table a RDB represent a relation A query in a RDB is a relation also queries are expressed via SQL (Structured Query Language) Federico Pecora – Java for Interfaces and Networks – Lecture 6 5 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 A simple query select number, name from person where number < 4 Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Number 1 2 3 Phone 260088 282677 260088 174590 Name Olle Peter Holga Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 Creating a table create table Person (number integer, name varchar(6), phone varchar(6), primary key (number)); Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 Adding rows insert into person values (7, ’Klas’, ’260088’); Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 Updating rows update person set phone = ’20270’ where number = 7; Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 Another query... select * from person where phone in (select phone from (select distinct phone, count(*) as n from person group by phone) as p where n > 1); Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java and DBMSs: background Relational databases Number 1 2 3 4 Name Olle Peter Holga Lotta Phone 260088 282677 260088 174590 Number 1 3 Name Olle Holga Phone 260088 260088 Federico Pecora – Java for Interfaces and Networks – Lecture 6 6 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) Outline 1 Java and DBMSs: background 2 Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: updating 3 Applets and JDBC Security issues Federico Pecora – Java for Interfaces and Networks – Lecture 6 7 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC JDBC: standard API for accessing relational databases send SQL statements in a vendor-independent way standardised way to establish a connection to a database standardised way of sending queries standardised way of committing transactions allows for vendor specific SQL syntax (try to avoid that!) JDBC is part of JDK since version 1.4 DBMS vendors distribute their own JDBC driver Federico Pecora – Java for Interfaces and Networks – Lecture 6 8 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC drivers Federico Pecora – Java for Interfaces and Networks – Lecture 6 9 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) Useful links on JDBC Using JDBC with MySQL, Getting started http://www.developer.com/java/data/print.php/3417381 Get Started with Mimer JDBC http://developer.mimer.com/howto/howto_17.htm Getting started with the JDBC API http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/ getstart/GettingStartedTOC.fm.html The Java Tutorial. Trail: JDBC Database Access http://java.sun.com/docs/books/tutorial/jdbc JDBC 2.0 Fundamentals http: //java.sun.com/developer/onlineTraining/Database Federico Pecora – Java for Interfaces and Networks – Lecture 6 10 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: connecting to a DBMS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 PersonTest.java import java.sql.*; public class PersonTest { public static void main(String args[]) { try { Class.forName("com.mimer.jdbc.Driver"); String url = "jdbc:mimer://<user>:<pass>@<hostName>/<dbName>"; Connection con = DriverManager.getConnection(url); Statement stmt = con.createStatement(); String sql = "select number, name from person where number < 4"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int number = rs.getInt(1); String name = rs.getString(2); System.out.println("Person " + number + "’s name is " + name + "."); } //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 11 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: connecting to a DBMS PersonTest.java (cont.) 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 rs.close(); stmt.close(); con.close(); } catch (SQLException e) { while (e != null) { System.out.println("SQLException:"); System.out.println(" SQLState: " + e.getSQLState()); System.out.println(" Message: " + e.getMessage()); System.out.println(" ErrorCode: " + e.getErrorCode()); e = e.getNextException(); } } catch (Exception e) { System.out.println("Another exception:"); e.printStackTrace(); } } //main } //class PersonTest Federico Pecora – Java for Interfaces and Networks – Lecture 6 11 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: connecting to a DBMS Normal execution. . . linux> linux> Person Person Person linux> [make sure com.mimer.jdbc.Driver is in your classpath] java PersonTest 1’s name is Olle 2’s name is Peter 3’s name is Holga Federico Pecora – Java for Interfaces and Networks – Lecture 6 11 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: connecting to a DBMS Wrong password. . . linux> java PersonTest SQLException: SQLState: 08004 Message: Login failure ErrorCode: -14006 linux> Federico Pecora – Java for Interfaces and Networks – Lecture 6 11 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: connecting to a DBMS Wrong SQL query. . . linux> java PersonTest SQLException: SQLState: 42000 Message: NUMMER is not a column of an inserted table, updated table or any table identified in a FROM clause ErrorCode: -12202 linux> Federico Pecora – Java for Interfaces and Networks – Lecture 6 11 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 NewPersonTest.java public class NewPersonTest { public static String readUserInput() throws MyReadException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String numberString; int number; String name; String phone; try { System.out.println("Enter a new person."); System.out.print(" number: "); numberString = reader.readLine(); number = Integer.parseInt(numberString); System.out.print(" name: "); name = reader.readLine(); System.out.print(" phone: "); phone = reader.readLine(); } //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface 18 19 20 21 22 23 24 25 26 NewPersonTest.java (cont.) catch (IOException e) { throw new MyReadException("Could not read form input!"); } catch (NumberFormatException e) { throw new MyReadException("Not a number!"); } return new String("insert into person values (" + number + ", ’" + name + "’, ’" + phone + "’)"); } //readUserInput //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 NewPersonTest.java (cont.) public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://<hostname>/<dbName>"; Connection con = DriverManager.getConnection(url, <user>, <pass>); Statement stmt = con.createStatement(); String sql = readUserInput(); System.out.println("SQL command: " + sql); int rowCount = stmt.executeUpdate(sql); System.out.println(rowCount + " row(s) updated."); stmt.close(); con.close(); } //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 NewPersonTest.java (cont.) catch (SQLException e) { while (e != null) { System.out.println("SQLException:"); System.out.println(" SQLState: " + e.getSQLState()); System.out.println(" Message: " + e.getMessage()); System.out.println(" ErrorCode: " + e.getErrorCode()); e = e.getNextException(); } } catch (ClassNotFoundException e) { System.out.println("SQLException:"); e.printStackTrace(); } catch (MyReadException e) { System.out.println("A MyReadException has occured!"); e.printStackTrace(); } System.exit(0); } //main } //class NewPersonTest Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface Normal execution. . . linux> [make sure com.mysql.jdbc.Driver is in your classpath] linux> java NewPersonTest Enter a new person. number: 7 name: Klas phone: 260088 SQL command: insert into person values (7, ’Klas’, ’260088’) 1 row(s) updated. linux> Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface Duplicate keys. . . linux> java NewPersonTest Enter a new person. number: 1 name: Hanna phone: 767610 SQL command: insert into person values (1, ’Hanna’, ’767610’) SQLException: SQLState: 23000 Message: Duplicate entry ’1’ for key 1 ErrorCode: 1062 linux> Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Java Database Connectivity (JDBC) JDBC: updating JDBC: interactive DBMS interface Use of MyReadException. . . linux> java NewPersonTest Enter a new person. number: SeventySeven A MyReadException has occured! MyReadException: Not a number! at NewPersonTest.readUserInput(NewPersonTest.java:21) at NewPersonTest.main(NewPersonTest.java:35) linux> Federico Pecora – Java for Interfaces and Networks – Lecture 6 12 / 16 Java for Interfaces and Networks Applets and JDBC Outline 1 Java and DBMSs: background 2 Java Database Connectivity (JDBC) JDBC: connecting and querying JDBC: updating 3 Applets and JDBC Security issues Federico Pecora – Java for Interfaces and Networks – Lecture 6 13 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 MySQLApplet.java public class MySQLApplet extends JApplet implements ActionListener { public void actionPerformed (ActionEvent event) { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://<host>/<dbName>"; Connection con = DriverManager.getConnection(url, <user>, <pass>); Statement stmt = con.createStatement(); String sql = "select number, name from person where number < 4"; ResultSet rs = stmt.executeQuery(sql); text.setText("Persons:\n"); //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface 16 17 18 19 20 21 22 23 24 25 26 MySQLApplet.java (cont.) while (rs.next()) { int number = rs.getInt(1); String name = rs.getString(2); text.append("Person " + number + "’s name is " + name + ".\n"); } rs.close(); stmt.close(); con.close(); } //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface 27 28 29 30 31 32 33 34 35 36 37 38 39 40 MySQLApplet.java (cont.) catch (SQLException se) { while (se != null) { text.setText("SQLException:\n"); text.append(" SQLState: " + se.getSQLState() + "\n"); text.append(" Message: " + se.getMessage() + "\n"); text.append(" ErrorCode: " + se.getErrorCode() + "\n"); se = se.getNextException(); } } catch (Exception exc) { text.setText("Other Exception:\n"); text.append(exc.toString() + "\n"); } } //actionPerformed //continues... Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface 41 42 43 44 45 46 47 48 49 50 51 52 MySQLApplet.java (cont.) private JTextArea text; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JLabel("An applet that gets persons")); text = new JTextArea("Output field", 6, 30); cp.add(new JScrollPane(text)); JButton runButton = new JButton("Get persons"); runButton.addActionListener(this); cp.add(runButton); } //init } //class MySQLApplet Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC JDBC and Applets: simple Swing interface Federico Pecora – Java for Interfaces and Networks – Lecture 6 14 / 16 Java for Interfaces and Networks Applets and JDBC Security issues JDBC and Applets: security Applets are restricted in what they can access SQLException: SQLState: 08001 Message: Could not establish connection to server XXXX on host YYYY using port 1360, java.security.AccessControlException: access denied (java.net.SocketPermission YYYY resolve) ErrorCode: -22017 Federico Pecora – Java for Interfaces and Networks – Lecture 6 15 / 16 Java for Interfaces and Networks Applets and JDBC Security issues JDBC and Applets: security JVM runs applets under a different security regime than applications applications are trusted by default (it is assumed that the user starts them) applets are untrusted because they are started by browsers (user cannot be expected to know what an applet does) Cannot access any internet site other than the one they were obtained from Cannot access Java Native Interface (JNI) Cannot access filesystem (except “inside” their .jar) These limitations can be overcome with digital certificates http://java.sun.com/javase/6/docs/technotes/ guides/security/index.html Federico Pecora – Java for Interfaces and Networks – Lecture 6 15 / 16 Java for Interfaces and Networks Applets and JDBC Security issues Java Database Connectivity (JDBC) Thank you! Federico Pecora – Java for Interfaces and Networks – Lecture 6 16 / 16