Finish exo9 code and fix unicode support where possible.
[TD_webapps.git] / ROOT / exo9 / addbook.jsp
diff --git a/ROOT/exo9/addbook.jsp b/ROOT/exo9/addbook.jsp
new file mode 100644 (file)
index 0000000..a0fb035
--- /dev/null
@@ -0,0 +1,52 @@
+<%@ 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>Ajouter un livre</h1>
+<form method="POST" action="addbook.jsp?new=true">
+  Titre:<br>
+  <input type="text" name="title"><br>
+  Auteur:<br>
+  <input type="text" name="author"><br><br>
+  <input type="submit" value="Submit">
+</form>
+
+<%
+request.setCharacterEncoding("UTF-8");
+String isNew = request.getParameter("new");
+
+if (isNew != null && isNew.equals("true")) {
+    String title = request.getParameter("title");
+    String author = request.getParameter("author");
+    Connection connection = null;
+    PreparedStatement 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()) {
+            stmt = connection.prepareStatement("INSERT INTO details_livres (nom_livre, auteur) VALUES (?, ?)");
+            stmt.setString(1, title);
+            stmt.setString(2, author);
+            stmt.executeUpdate();
+            out.println("Book " + title + " by " + author + " successfully added");
+        }
+        } 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