Java Reference
In-Depth Information
Display 19.13
Retrieving Rows with the SELECT Statement (part 2 of 3)
19 System.out.println("ID = " + id + ", Author = "
20 + author + ", URL = " + url);
21 }
22 public static void main(String[] args)
23 {
24 try
25 {
26 Class.forName(driver).newInstance();
27 System.out.println("Loaded the embedded driver.");
28 }
29 catch (Exception err)
30 {
31 System.err.println("Unable to load the embedded driver.");
32 err.printStackTrace(System.err);
33 System.exit(0);
34 }
35 String dbName = "BookDatabase";
36 Connection conn = null ;
37 try
38 {
39 System.out.println("Connecting to the database...");
40 conn = DriverManager.getConnection(protocol + dbName);
The text create = true;
has been left of the connection
string to connect to an existing
database.
41 System.out.println("Connected.");
42 Statement s = conn.createStatement();
43 ResultSet rs = null ;
44 System.out.println("All rows:");
45 rs = s.executeQuery("SELECT author, author_id, url FROM names");
46 while ( rs.next() )
47 {
48 displayNameRow(rs);
49 }
50 rs.close();
51 System.out.println();
52 System.out.println("All rows with an ID > 1:");
53 rs = s.executeQuery("SELECT author, author_id, url " +
54 "FROM names WHERE author_id > 1");
55 while ( rs.next() )
56 {
57 displayNameRow(rs);
58 }
Search WWH ::




Custom Search