<h1>Coffee Break JSP Example</h1>
<%@ page errorPage="error.jsp" import="java.sql.*" %>
<%! // --------------- inits for the servlet --------------
// ---- configure this for your site
// The URL that will connect to TECFA's MySQL server
// Syntax: jdbc:TYPE:machine:port/DB_NAME
// String url = "jdbc:mysql://localhost:3306/COFFEEBREAK";
String url = "jdbc:mysql://tecfa2.unige.ch:3306/COFFEEBREAK";
<% // --------------- code for the service method --------------
// Let's see if we got a request
queryString = request.getParameter ("QUERYSTRING");
if ((queryString != "") && (queryString != null)) {
Class.forName("org.gjt.mm.mysql.Driver");
// Establish Connection to the database at URL with usename and password
con = DriverManager.getConnection(url, username, password);
out.println ("Ok, connection to the DB is working.");
} catch (Exception e) // (ClassNotFoundException and SQLException)
throw(new UnavailableException(this, "Sorry! The Database didn't load!"));
out.println ("<h2>You asked: </h2>");
out.println ( "Query: " + queryString + "<BR>" );
out.println("<h3>Query Result</h3>");
out.println("<table border>");
ResultSet rs = stmt.executeQuery(queryString);
ResultSetMetaData rsMeta = rs.getMetaData();
// Get the N of Cols in the ResultSet
int noCols = rsMeta.getColumnCount();
for (int c=1; c<=noCols; c++) {
String el = rsMeta.getColumnLabel(c);
out.println("<th> " + el + " </th>");
for (int c=1; c<=noCols; c++) {
out.println("<td> " + el + " </td>");
out.println("Message: " + ex. getMessage ());
out.println("SQLState: " + ex. getSQLState ());
out.println("ErrorCode: " + ex. getErrorCode ());
<hr>You can now try to retrieve something.
< FORM METHOD=POST ACTION=" CoffeeBreakLine.jsp ">
Query: <INPUT TYPE=TEXT SIZE=50 NAME=" QUERYSTRING ">
<INPUT TYPE=SUBMIT VALUE="GO!">
SELECT * FROM COFFEES WHERE PRICE > 9
SELECT PRICE, COF_NAME FROM COFFEES
<hr><a href="CoffeeBreakLine.jsp">Encore une fois ?</a> |
Source: <A HREF="CoffeeBreakLine.jsp.text">CoffeeBreakLine.jsp.text</A>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
[ ........]
public class
CoffeeBreakServlet
extends
HttpServlet
{
// The database connection
Connection con;
// The statement
Statement stmt;
// The queryString
String queryString = null;
public void
init
(ServletConfig conf) throws ServletException {
super.init(conf);
// ---- configure this for your site
String username = "nobody";
String password = null;
// The URL that will connect to TECFA's MySQL server
String url = "jdbc:mysql://tecfa2.unige.ch:3306/COFFEEBREAK";
// ---- configure END
try {
Class.forName("org.gjt.mm.mysql.Driver");
// Connection to the database at URL with username and password
con = DriverManager.getConnection(url, username,password);
System.out.println ("Ok, connection to the DB is working.");
} catch (Exception e)
// (ClassNotFoundException and SQLException)
{
throw(new UnavailableException(this, "Sorry! The Database didn't load!"));
}
public void
service
(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType ( "text/html" );
PrintWriter out = res.getWriter ( );
try {
String title = "Coffee Break JDBC Demo Java Servlet";
out.println ( "<html><head><title>" + title
+ "</title></head>" );
out.println ( "<body><H1>" + title + "</H1>" );
String queryString = req.getParameter ("QUERYSTRING");
if ((queryString != "") && (queryString != null)) {
out.println ("<h2>You asked: </h2>");
out.println ( "Query: " + queryString + "<BR>" );
out.println("<h3>Query Result</h3>");
out.println("<table border>");
Statement
stmt = con.
createStatement
();
ResultSet
rs = stmt.
executeQuery
(queryString);
ResultSetMetaData
rsMeta = rs.
getMetaData
();
// Get the N of Cols in the ResultSet
int noCols = rsMeta.
getColumnCount
();
out.println("<tr>");
for (int c=1; c<=noCols; c++) {
String el = rsMeta.
getColumnLabel
(c);
out.println("<th> " + el + " </th>");
}
out.println("</tr>");
while (rs.next()) {
out.println("<tr>");
for (int c=1; c<=noCols; c++) {
String el = rs.
getString
(c);
out.println("<td> " + el + " </td>");
}
out.println("</tr>");
}
out.println("</table>");
}
} catch { [ .. gérer les erreurs de la requête......] }
out.println ("<hr>You can now try to retrieve something.");
out.println("<
FORM
METHOD=POST
ACTION='/servlet/CoffeeBreakServlet'
>");
out.println("Query: <INPUT TYPE=TEXT SIZE=50 NAME='
QUERYSTRING
'> ");
out.println("<INPUT TYPE=SUBMIT VALUE='GO!'>");
out.println("</
FORM
>");
out.println("<hr><pre>e.g.:");
out.println("SELECT * FROM COFFEES");
out.println("SELECT * FROM COFFEES WHERE PRICE > 9");
out.println("SELECT PRICE, COF_NAME FROM COFFEES");
out.println("<pre>");
out.println ("<hr><a href='/servlet/CoffeeBreakServlet'>Enocre une fois ?</a> | Source: <A HREF='/develop/servlets-ex/coffee-break/CoffeeBreakServlet.java'>CoffeeBreakServlet.java</A>");
out.println ( "</body></html>" );
return ;
}