Finish exo9 code and fix unicode support where possible.
[TD_webapps.git] / ROOT / exo9 / viewbooks.jsp
diff --git a/ROOT/exo9/viewbooks.jsp b/ROOT/exo9/viewbooks.jsp
new file mode 100644 (file)
index 0000000..02b2591
--- /dev/null
@@ -0,0 +1,42 @@
+<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
+
+<meta charset="UTF-8">
+<html>
+<body>
+
+<h1>Liste des livres</h1>
+
+<%
+
+Connection connection = null;
+Statement stmt = null;
+try {
+    String connectionURL = "jdbc:mysql://localhost/MyBd?useUnicode=yes&characterEncoding=UTF-8";
+    Class.forName("com.mysql.jdbc.Driver").newInstance();
+    connection = DriverManager.getConnection(connectionURL, "MyBd", "MyBd");
+    if(!connection.isClosed()) {
+        String query = "SELECT nom_livre, auteur FROM details_livres";
+        stmt = connection.createStatement();
+        ResultSet rs = stmt.executeQuery(query);
+        while (rs.next()) {
+            String title = rs.getString("nom_livre");
+            String author = rs.getString("auteur");
+            //TODO: make a html table
+            out.println(title + " par " + author + "<br>");
+        }
+    }
+    } catch (Exception e){
+        out.println("Unable to connect to database or run query: " + e);
+    }
+    finally {
+        if (connection != null)
+            connection.close();
+        if (stmt != null)
+            stmt.close();
+    }
+
+%> 
+
+</body>
+</html>
\ No newline at end of file