Signets Paul Oberson
<%@ page errorPage="error.jsp" import="java.sql.*" %> <%! // --------------- inits for the servlet --------------
// The database connection
Connection con;
// The statement
Statement stmt;
// The queryString
String queryString = null;
String tri ="id";
// ---- configure this for your site
String username = "nobody";
String password = null;
// The URL that will connect to TECFA's MySQL server
// Syntax: jdbc:TYPE:machine:port/DB_NAME
String url = "jdbc:mysql://localhost:3306/oberson";
%> <% // --------------- code for the service method --------------
String action = request.getParameter("submit");
String site = request.getParameter("site");
String URL = request.getParameter("URL");
String comment = request.getParameter("comment");
String theme = request.getParameter("theme");
tri = request.getParameter ("tri");
try {
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, base de donnée connectée.");
try {
stmt = con.createStatement();
out.println("Liste des signets");
if (action!=null) {
String insertString = "INSERT INTO mes_signets VALUES('"+theme+"', '"+URL+"', '"+site+"', '"+comment+"', '', current_date())";
int executeInsert = stmt.executeUpdate (insertString);
}
//print all bookmarks
String viewString = "SELECT * FROM mes_signets ORDER BY "+ tri;
ResultSet resultView= stmt.executeQuery (viewString);
out.println("
");
ResultSetMetaData viewMeta = resultView.getMetaData();
//Get the N of Cols in the ResultSet
int noCols = viewMeta.getColumnCount();
out.println("");
out.println ("Category | \nSite | \nCommentaires | \nNo | \nDate | ");
out.println("
");
while (resultView.next()) {
out.println("");
for (int c=1; c<=noCols; c++) {
String nomurl = resultView.getString("url");
String el = resultView.getString(c);
//HT-link
if (c == 3)
{
out.println(" " + el + " | ");
}
//don't show URL
else if (c==2)
{
out.println("");
} else
{
out.println(" " + el + " | ");
//
// solution alternative
// out.println(" " + resultView.getString("comment") + " | ");
}
}
out.println("
");
}
out.println("
");
}
//print infos ~ errors
catch (SQLException ex ) {
out.println ( "" );
while (ex != null) {
out.println("Message: " + ex.getMessage ());
out.println("SQLState: " + ex.getSQLState ());
out.println("ErrorCode: " + ex.getErrorCode ());
ex = ex.getNextException();
out.println("");
}
out.println ( "
" );
}
}
catch (Exception e) // (ClassNotFoundException and SQLException)
{
throw(new UnavailableException(this, "Sorry! The Database didn't load!"));
}
finally {
if (con != null) {
try {con.close();}
catch (SQLException ex) {ex.printStackTrace();}
}
}
%>