<%@page session="false" %> <%@page import="java.sql.*" %> <% String username = request.getParameter("user"); String password = request.getParameter("passwd"); %> Database Test <% if (password == null || username == null) { %> In order to send the predefined query to the database you have to authenticate by sending your login name and password to the database manager.
User name:
Password:
<% } else { Connection conn = null; // load the postgresql db driver try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException cnf) { %>

Could not find the JDBC driver

<% } try{ // connect to the database conn = DriverManager.getConnection( "jdbc:postgresql://nestor.nada.kth.se/varuhuset", username, password); // issue a query and read the results if (conn != null) { Statement st = conn.createStatement(); String query = "SELECT avd, våning FROM avdelning where våning in (2,3)"; ResultSet result = st.executeQuery(query); %> <%-- First, inform the user about the query --%>

The query:

<%=query%>

The database response to the query:

<%-- we open a table, each row in the database will be a row in the table --%> <% // as long as we have results while(result.next()){ // we write a table row in HTML %> <% // end of while() } %> <%-- end of table --%>
AvdelningVåning
<%=result.getString(1)%><%=result.getInt(2)%>

<% // end of db connection result.close(); st.close(); conn.close(); // end of "try {" above } } catch(SQLException e){ out.println("

"+ e.getMessage()+"

"); } } %>