| |
- Nu ga je via de Tomcat server een jsp-pagina runnen die een connectie met de database opzet.
Maak in Notepad de volgende jsp-pagina aan en save die onder de naam testAccess.jsp:
<%@ page language="java" contentType="text/html" import="java.sql.*"%>
<%
Connection conn;
ResultSet resultSet;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e) {
System.out.println("Driver not found.");
System.out.println(e.toString());
}
try {
// Connect to the Database
conn = DriverManager.getConnection("jdbc:odbc:boeken_A", "", "");
Statement statement = conn.createStatement();
resultSet = statement.executeQuery("SELECT id, titel, id_auteur"
+ " FROM boek");
}
catch(SQLException e) {
System.out.println("An error occurs.");
System.out.println(e.toString());
}
%>
<html>
<head>
<title>Access to Access Database on Tomacat</title>
</head>
<body>
<h1>Select * from Boek (Datasource boek_A)</h1>
<hr>
<pre>
Connection settings:
getCatalog = <%=conn.getCatalog()%>
getAutoCommit = <%=conn.getAutoCommit()%>
</pre>
<hr>
<p>Return result:</p>
<table border=1 cellpadding=2 cellspacing=0 width=500>
<% int countrows = 0; %>
<% while (resultSet.next()) { %>
<tr height="302">
<td height="16"><%= resultSet.getInt("id") %></td>
<td height="16"><%= resultSet.getString("titel") %></td>
<td height="16"><%= resultSet.getInt("id_auteur") %></td>
<% countrows++; %>
</tr>
<% } %>
</table>
<p><%=countrows%> row(s) found.</p>
<hr>
</body>
</html>
- Plaats de file testAccess in dezelfde directory waar ook de file leuke.html staat.
- start de jsp-paina door: http://localhost:8080/tst3Q/testAccess.jsp
Het resultaat toont de inhoud van de opgevraagde database-tabel.
| |