%@ page import="java.io.*, java.util.*, javax.servlet.*, java.sql.*" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
Ajouter un livre
<%
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();
}
}
%>