-<%@ page contentType="text/xml, charset=ISO-8859-1" %>
+<%@ page contentType="text/xml; charset=ISO-8859-1" %>
<html>
<body>
<%
+request.setCharacterEncoding("UTF-8");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String address = request.getParameter("address");
<%@ page import="java.io.*, java.util.*, javax.servlet.*" %>
<%
+request.setCharacterEncoding("UTF-8");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String username = firstname + lastname;
-<%@ page contentType="text/html" pageEncoding="UTF-8" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="user" class="user.UserData" scope="session" />
<meta charset="UTF-8">
<html>
-<%@ page contentType="text/html" pageEncoding="UTF-8" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="user" class="user.UserData" scope="session" />
<jsp:setProperty name="user" property="*"/>
<meta charset="UTF-8">
--- /dev/null
+<%@ 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
<%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
<%
+Connection connection = null;
try {
- String connectionURL = "jdbc:mysql://localhost/MyBd";
- Connection connection = null;
+ 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())
out.println("Successfully connected to " + connectionURL + " MySQL server using TCP/IP");
- connection.close();
} catch (Exception e){
- out.println("Unable to connect to database: " + e);
+ out.println("Unable to connect to database: " + e);
+ }
+ finally {
+ if (connection != null)
+ connection.close();
}
%>
--- /dev/null
+<%@ 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