Java Reference
In-Depth Information
Display 19.25
JSP Program to Update Database Entries Submitted by a Browser Viewing
Display 19.18 (part 2 of 3)
15 String driver = "org.apache.derby.jdbc.EmbeddedDriver";
16 String protocol = "jdbc:derby:";
17 try
18 {
19 Class.forName(driver).newInstance();
20 out.println("Loaded the embedded driver.<br>");
21 }
22 catch (Exception err)
23 {
24 out.println("Unable to load the embedded driver.</body></html>");
25 return ;
26 }
Refer to Displays 19.13 and 19.14 for
an explanation of the database code in
this scriptlet.
27 String dbName = "BookDatabase";
28 Connection conn = null ;
29 try
30 {
31 out.println("Connecting to and creating the database...<br />");
32 conn = DriverManager.getConnection(protocol + dbName +
";create=true");
33 out.println("Connected.<br />");
The URL in the database is changed to the
submitted value if the Author IDs match.
34 Statement s = conn.createStatement();
35 s.execute("UPDATE names " +
36 "SET URL = '" + newURL + "' WHERE author_id = " + author_id);
37 out.println("<br/><b>URL changed to " + newURL +
38 "for Author ID = " + author_id + "</b><br />");
39 out.println("<br />Displaying all rows : <br />");
40 ResultSet rs = null ;
41 rs = s.executeQuery("SELECT author, author_id, url FROM names");
42 out.println("<ol>");
43 while ( rs.next() )
44 {
45 int id = rs.getInt("author_id");
46 String author = rs.getString("author");
47 String url = rs.getString("url");
48 out.println("<li>ID = " + id + ", Author = "
49 + author + ", URL = " + url + "</li>");
50 }
51 out.println("</ol>");
52 rs.close();
This loop outputs all rows in the database
inside a numbered list.
53 conn.close();
54 }
55 catch (Exception err)
56 {
57 out.println("SQL error.<br />");
58 }
Search WWH ::




Custom Search