exo10: add a FIXME comment.
[TD_webapps.git] / ROOT / exo9 / addbook.jsp
... / ...
CommitLineData
1<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
2<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
3
4<meta charset="UTF-8">
5<html>
6<body>
7
8<h1>Ajouter un livre</h1>
9<form method="POST" action="addbook.jsp?new=true">
10 Titre:<br>
11 <input type="text" name="title"><br>
12 Auteur:<br>
13 <input type="text" name="author"><br><br>
14 <input type="submit" value="Submit">
15</form>
16
17<%
18request.setCharacterEncoding("UTF-8");
19String isNew = request.getParameter("new");
20
21if (isNew != null && isNew.equals("true")) {
22 String title = request.getParameter("title");
23 String author = request.getParameter("author");
24 Connection connection = null;
25 PreparedStatement stmt = null;
26 try {
27 String connectionURL = "jdbc:mysql://localhost/MyBd?useUnicode=yes&characterEncoding=UTF-8";
28 Class.forName("com.mysql.jdbc.Driver").newInstance();
29 connection = DriverManager.getConnection(connectionURL, "MyBd", "MyBd");
30 if(!connection.isClosed()) {
31 stmt = connection.prepareStatement("INSERT INTO details_livres (nom_livre, auteur) VALUES (?, ?)");
32 stmt.setString(1, title);
33 stmt.setString(2, author);
34 stmt.executeUpdate();
35 out.println("Book " + title + " by " + author + " successfully added");
36 }
37 } catch (Exception e){
38 out.println("Unable to connect to database or run query: " + e);
39 }
40 finally {
41 if (connection != null)
42 connection.close();
43 if (stmt != null)
44 stmt.close();
45 }
46
47}
48
49%>
50
51</body>
52</html>