package dbj.jdbc; import java.sql.*; import java.io.*; public class QueryBooks { Connection con; public QueryBooks (Connection c) { con = c; } public void queryByAuthor (String name) throws SQLException { PreparedStatement stmt = con.prepareStatement ( "SELECT b.title, b.price, b.isbn " + "FROM author a, book_author ba, book b " + "WHERE a.lastname = ? AND a.a_id = ba.a_id AND ba.isbn = b.isbn"); stmt.setString (1, name); ResultSet rs = stmt.executeQuery (); while (rs.next ()) { System.out.println (rs.getString (1) + ", " + rs.getDouble (2) + ", " + rs.getString (3)); } rs.close (); stmt.close (); } public void queryByTitle (String title) throws SQLException { PreparedStatement stmt = con.prepareStatement ( "SELECT title, price, isbn FROM book WHERE title LIKE ?"); stmt.setString (1, title); ResultSet rs = stmt.executeQuery (); while (rs.next ()) { System.out.println (rs.getString (1) + ", " + rs.getDouble (2) + ", " + rs.getString (3)); } rs.close (); stmt.close (); } public static void main (String[] args) throws IOException { if (args.length != 2) { System.out.println ("usage: dbj.jdbc.QueryBooks title|author value"); System.exit (1); } try { Connection con = Utils.getConnection (); QueryBooks app = new QueryBooks (con); if (args[0].equals ("title")) app.queryByTitle (args[1]); else if (args[0].equals ("author")) app.queryByAuthor (args[1]); } catch (SQLException exc) { exc.printStackTrace (); } } }