Guest User

Untitled

a guest
Jun 7th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package be.howest;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.util.ArrayList;
  9.  
  10. import javax.servlet.RequestDispatcher;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. /**
  17.  * Servlet implementation class CountryServlet
  18.  */
  19. public class CountryServlet extends HttpServlet {
  20.     private static final long serialVersionUID = 1L;
  21.                
  22.         public Connection getConnection() {
  23.             Connection aConnection = null;
  24.             String databaseURL =  "jdbc:mysql://localhost:3306/codelib";
  25.             String user = "root";
  26.             String password = "";
  27.             try {
  28.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  29.                 aConnection = DriverManager.getConnection(databaseURL,user,password);
  30.    
  31.             } catch (Exception e) {
  32.                 System.out.println("Could not get a connection.");
  33.                  e.printStackTrace();
  34.             }
  35.             return aConnection;
  36.         }
  37.        
  38.         ArrayList<Country> getList( Connection theConnection) {
  39.             ArrayList<Country> aList = new ArrayList<Country>();
  40.                
  41.             try {
  42.                 PreparedStatement anSQL = theConnection.prepareStatement(
  43.                                    "select code, name from countries order by name");
  44.                 ResultSet aRset = anSQL.executeQuery();
  45.                 aRset.beforeFirst();
  46.                 while (aRset.next()) {
  47.                   Country country = new Country((String) aRset.getObject(1), (String) aRset.getObject(2));
  48.                   aList.add(country);
  49.                 }
  50.                 System.out.println("Fetched: " + aList.size() + " rows.");
  51.                 aRset.close();
  52.                
  53.             } catch (Exception a) {
  54.                 System.out.println("Error getting the countries");
  55.  
  56.             }
  57.             return aList;
  58.         }
  59.        
  60.         protected void doGet(HttpServletRequest request, HttpServletResponse response)
  61.                 throws ServletException, IOException {
  62.  
  63.             Connection aCon = this.getConnection();
  64.            
  65.             RequestDispatcher aDispatcher = request.getRequestDispatcher("CountryList.jsp");;
  66.            
  67.             String action = request.getParameter("action");
  68.             if (action == null) {
  69.                 request.setAttribute("countries", getList( aCon ));
  70.                 aDispatcher = request.getRequestDispatcher("CountryList.jsp");
  71.                
  72.             } else if (action.equalsIgnoreCase("edit")) {
  73.                 request.setAttribute("country", getCountry( aCon, request.getParameter("code") ));
  74.                 aDispatcher = request.getRequestDispatcher("CountryForm.jsp");
  75.                
  76.             } else if (action.equalsIgnoreCase("save")) {
  77.                 String code = request.getParameter("code");
  78.                 String name = request.getParameter("name");
  79.                 saveCountry(aCon, code, name);
  80.                 request.setAttribute("countries", getList( aCon ));
  81.             }
  82.            
  83.             aDispatcher.forward(request, response);
  84.             }
  85.  
  86.        
  87.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  88.         this.doGet(request,  response);
  89.     }
  90.    
  91.     void saveCountry( Connection theConnection, String code, String name) {
  92.         try {
  93.             PreparedStatement anSQL = theConnection.prepareStatement(
  94.             "update countries set name = ? where code = ?");
  95.             anSQL.setString(1, name);
  96.             anSQL.setString(2, code);
  97.             anSQL.executeUpdate();
  98.             System.out.println("saved");
  99.         } catch (Exception a) {
  100.             System.out.println("Error saving the country " + code + "/" + name);
  101.         }
  102.     }
  103.    
  104.     Country getCountry( Connection theConnection, String code ) {
  105.         Country country = new Country("", "");
  106.         try {
  107.         PreparedStatement anSQL = theConnection.prepareStatement(
  108.         "select code, name from countries where code = ?");
  109.         anSQL.setString(1, code);
  110.         ResultSet aRset = anSQL.executeQuery();
  111.         aRset.beforeFirst();
  112.         if (aRset.next()) {
  113.         country = new Country((String) aRset.getObject(1),
  114.          (String) aRset.getObject(2));
  115.         } else {
  116.         System.out.println("No country " + code + " found.");
  117.         }
  118.         aRset.close();
  119.         } catch (Exception a) {
  120.         System.out.println("Error getting the country " + code);
  121.         }
  122.         return country;
  123.         }
  124.  
  125. }
Add Comment
Please, Sign In to add comment