Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<%@page contentType="text/html" pageEncoding="UTF-8"
import="java.sql.*,javax.naming.*,javax.sql.*"%>
<%
request.setCharacterEncoding("UTF-8");
Connection db = null;
PreparedStatement ps = null;
try{
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/BOOKS");
db = ds.getConnection();
ps = db.prepareStatement("INSERT INTO books(name,year,title,publisher) VALUES(?,?,?,?)");
ps.setString(1,request.getParameter("name"));
ps.setString(2,request.getParameter("year"));
ps.setString(3,request.getParameter("title"));
ps.setString(4,request.getParameter("publisher"));
ps.executeUpdate();
}catch(Exception err){
throw new ServletException(err);
}finally{
try{
if(ps != null) {ps.close();}
if(db != null) {db.close();}
}catch(Exception err){}
}
response.sendRedirect("index.jsp");
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment