Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.howest;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class CountryServlet
- */
- public class CountryServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public Connection getConnection() {
- Connection aConnection = null;
- String databaseURL = "jdbc:mysql://localhost:3306/codelib";
- String user = "root";
- String password = "";
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- aConnection = DriverManager.getConnection(databaseURL,user,password);
- } catch (Exception e) {
- System.out.println("Could not get a connection.");
- e.printStackTrace();
- }
- return aConnection;
- }
- ArrayList<Country> getList( Connection theConnection) {
- ArrayList<Country> aList = new ArrayList<Country>();
- try {
- PreparedStatement anSQL = theConnection.prepareStatement(
- "select code, name from countries order by name");
- ResultSet aRset = anSQL.executeQuery();
- aRset.beforeFirst();
- while (aRset.next()) {
- Country country = new Country((String) aRset.getObject(1), (String) aRset.getObject(2));
- aList.add(country);
- }
- System.out.println("Fetched: " + aList.size() + " rows.");
- aRset.close();
- } catch (Exception a) {
- System.out.println("Error getting the countries");
- }
- return aList;
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Connection aCon = this.getConnection();
- RequestDispatcher aDispatcher = request.getRequestDispatcher("CountryList.jsp");;
- String action = request.getParameter("action");
- if (action == null) {
- request.setAttribute("countries", getList( aCon ));
- aDispatcher = request.getRequestDispatcher("CountryList.jsp");
- } else if (action.equalsIgnoreCase("edit")) {
- request.setAttribute("country", getCountry( aCon, request.getParameter("code") ));
- aDispatcher = request.getRequestDispatcher("CountryForm.jsp");
- } else if (action.equalsIgnoreCase("save")) {
- String code = request.getParameter("code");
- String name = request.getParameter("name");
- saveCountry(aCon, code, name);
- request.setAttribute("countries", getList( aCon ));
- }
- aDispatcher.forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- void saveCountry( Connection theConnection, String code, String name) {
- try {
- PreparedStatement anSQL = theConnection.prepareStatement(
- "update countries set name = ? where code = ?");
- anSQL.setString(1, name);
- anSQL.setString(2, code);
- anSQL.executeUpdate();
- System.out.println("saved");
- } catch (Exception a) {
- System.out.println("Error saving the country " + code + "/" + name);
- }
- }
- Country getCountry( Connection theConnection, String code ) {
- Country country = new Country("", "");
- try {
- PreparedStatement anSQL = theConnection.prepareStatement(
- "select code, name from countries where code = ?");
- anSQL.setString(1, code);
- ResultSet aRset = anSQL.executeQuery();
- aRset.beforeFirst();
- if (aRset.next()) {
- country = new Country((String) aRset.getObject(1),
- (String) aRset.getObject(2));
- } else {
- System.out.println("No country " + code + " found.");
- }
- aRset.close();
- } catch (Exception a) {
- System.out.println("Error getting the country " + code);
- }
- return country;
- }
- }
Add Comment
Please, Sign In to add comment