Advertisement
ventress

Praktyki - Dzien 03

Jul 15th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.48 KB | None | 0 0
  1. package service;
  2.  
  3. import dao.PatientDAO;
  4. import java.util.List;
  5. import javax.jws.WebService;
  6. import javax.jws.WebMethod;
  7. import javax.jws.WebParam;
  8. import model.Patient;
  9.  
  10. @WebService(serviceName = "patient")
  11. public class app
  12. {
  13.     @WebMethod(operationName = "add")
  14.     public String add(@WebParam(name = "patient") Patient patient)
  15.     {
  16.         PatientDAO patientDAO = new PatientDAO();
  17.         patientDAO.savePatient(patient);
  18.         return "Patient added";
  19.     }
  20.     @WebMethod(operationName = "showAll")
  21.     public List<Patient> showAll()
  22.     {
  23.         PatientDAO patientDAO = new PatientDAO();
  24.         List<Patient> patientList = patientDAO.getPatientList();
  25.         return patientList;
  26.     }
  27.     @WebMethod(operationName = "showOne")
  28.     public Patient showOne(@WebParam(name = "id") long id)
  29.     {
  30.         Patient patient = new Patient();
  31.         PatientDAO patientDAO = new PatientDAO();
  32.         patient = patientDAO.getPatientById(id);
  33.         return patient;
  34.     }
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. package dao;
  43.  
  44. import java.sql.Connection;
  45. import java.sql.Date;
  46. import java.sql.DriverManager;
  47. import java.sql.PreparedStatement;
  48. import java.sql.ResultSet;
  49. import java.sql.SQLException;
  50. import java.util.ArrayList;
  51. import java.util.Calendar;
  52. import java.util.List;
  53. import model.Patient;
  54. import tool.DBTool;
  55.  
  56. public class PatientDAO implements IPatientDAO
  57. {
  58.     private Patient parse(ResultSet resultSet) throws SQLException
  59.     {
  60.         Patient patient = new Patient();
  61.         patient.setFirstName(resultSet.getString("FNAME"));
  62.         patient.setMiddleName(resultSet.getString("MNAME"));
  63.         patient.setLastName(resultSet.getString("LNAME"));
  64.  
  65.         Calendar calendar = Calendar.getInstance();
  66.         calendar.setTime(resultSet.getDate("DOB"));                
  67.         patient.setDateOfBirth(calendar);                
  68.  
  69.         patient.setCity(resultSet.getString("CITY"));
  70.         patient.setZipCode(resultSet.getString("ZIP_CODE"));
  71.         patient.setPhone(resultSet.getString("PHONE"));
  72.         patient.setAddress(resultSet.getString("ADDRESS"));
  73.         patient.setBirthCity(resultSet.getString("BIRTH_CITY"));
  74.         patient.setMrn(resultSet.getString("MRN"));
  75.         patient.setId(resultSet.getInt("ID"));
  76.         patient.setSex(resultSet.getString("SEX"));
  77.         patient.setNationality(resultSet.getString("NATIONALITY"));
  78.         patient.setIsDisabled(resultSet.getString("IS_DISABLED"));
  79.         patient.setRace(resultSet.getString("RACE"));
  80.         return patient;
  81.     }    
  82.     @Override
  83.     public Patient getPatientById(long id)
  84.     {
  85.         Connection connection = null;
  86.         ResultSet resultSet = null;
  87.         PreparedStatement statement = null;
  88.        
  89.         Patient patient = new Patient();
  90.         String query = "SELECT * FROM P2_PATIENT p WHERE p.ID = ?";
  91.        
  92.         try
  93.         {
  94.             DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  95.             connection = DBTool.startConnection();
  96.             statement = connection.prepareStatement(query);
  97.             statement.setLong(1, id);
  98.             resultSet = statement.executeQuery();
  99.             while(resultSet.next())
  100.             {
  101.                 patient = parse(resultSet);
  102.             }
  103.         }
  104.         catch(SQLException exception)
  105.         {
  106.             exception.printStackTrace();
  107.         }
  108.         finally
  109.         {
  110.             try
  111.             {
  112.                 statement.close();
  113.                 resultSet.close();
  114.                 connection.close();
  115.             }
  116.             catch(SQLException exception)
  117.             {
  118.                 exception.printStackTrace();
  119.             }
  120.         }
  121.         return patient;
  122.     }
  123.     @Override
  124.     public List<Patient> getPatientList()
  125.     {
  126.         List<Patient> patientList = new ArrayList();
  127.        
  128.        Connection connection = null;
  129.         ResultSet resultSet = null;
  130.         PreparedStatement statement = null;
  131.        
  132.         Patient patient = new Patient();
  133.         String query = "SELECT * FROM P2_PATIENT";
  134.        
  135.         try
  136.         {
  137.             connection = DBTool.startConnection();
  138.             statement = connection.prepareStatement(query);
  139.             resultSet = statement.executeQuery();
  140.             while(resultSet.next())
  141.             {
  142.                 patient = new Patient();
  143.                 patient = parse(resultSet);
  144.                 patientList.add(patient);
  145.             }
  146.         }
  147.         catch(SQLException exception)
  148.         {
  149.             exception.printStackTrace();
  150.         }
  151.         finally
  152.         {
  153.             try
  154.             {
  155.                 statement.close();
  156.                 resultSet.close();
  157.                 connection.close();
  158.             }
  159.             catch(SQLException exception)
  160.             {
  161.                 exception.printStackTrace();
  162.             }
  163.         }
  164.         return patientList;
  165.     }
  166.     @Override
  167.     public boolean savePatient(Patient patient)
  168.     {
  169.         boolean  checker = true;
  170.         Connection connection = null;
  171.         PreparedStatement statement = null;
  172.        
  173.         String query = "INSERT INTO P2_PATIENT VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  174.         try
  175.         {
  176.             connection = DBTool.startConnection();
  177.             statement = connection.prepareStatement(query);
  178.             statement.setString(1, patient.getFirstName());
  179.             statement.setString(2, patient.getMiddleName());
  180.             statement.setString(3, patient.getLastName());
  181.            
  182.             statement.setDate(4, new Date(patient.getDateOfBirth().getTimeInMillis()));
  183.                      
  184.             statement.setString(5, patient.getCity());
  185.             statement.setString(6, patient.getZipCode());
  186.             statement.setString(7, patient.getPhone());
  187.             statement.setString(8, patient.getAddress());
  188.             statement.setString(9, patient.getBirthCity());
  189.             statement.setString(10, patient.getMrn());
  190.             statement.setLong(11, patient.getId());
  191.             statement.setString(12, patient.getSex());
  192.             statement.setString(13, patient.getNationality());
  193.             statement.setString(14, patient.getIsDisabled());
  194.             statement.setString(15, patient.getRace());
  195.             statement.executeQuery();            
  196.             connection.commit();
  197.         }
  198.         catch(SQLException exception)
  199.         {
  200.             exception.printStackTrace();
  201.             checker = false;
  202.         }
  203.         finally
  204.         {
  205.             try
  206.             {
  207.                 statement.close();
  208.                  connection.close();
  209.             }
  210.             catch(SQLException exception)
  211.             {
  212.                 exception.printStackTrace();
  213.             }
  214.         }
  215.         return checker;
  216.     }
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. package dao;
  224.  
  225. import java.util.List;
  226. import model.Patient;
  227.  
  228. public interface IPatientDAO
  229. {
  230.     public Patient getPatientById(long id);
  231.  
  232.     public List<Patient> getPatientList() ;
  233.  
  234.     public boolean savePatient(Patient patient);
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. package tool;
  243.  
  244. import java.sql.Connection;
  245. import java.sql.DriverManager;
  246. import java.sql.SQLException;
  247.  
  248. public class DBTool
  249. {
  250.     public static Connection startConnection() throws SQLException
  251.     {
  252.         Connection connection = null;
  253.        
  254.         String port = "1521";
  255.         String address = "iws2";
  256.         String login = "PRAKTYKI";
  257.         String password = "PRAKTYKI";
  258.         String schema = "oraiws.softsystem.pl";
  259.        
  260.         DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  261.         connection = DriverManager.getConnection("jdbc:oracle:thin:"+login+"/"+password+"@"+address+":"+port+"/"+schema);
  262.        
  263.         return connection;
  264.     }
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. package model;
  274.  
  275. import java.util.Calendar;
  276.  
  277. public class Patient
  278. {
  279.     private String firstName;
  280.     private String middleName;
  281.     private String lastName;
  282.     private Calendar dateOfBirth;
  283.     private String city;
  284.     private String zipCode;
  285.     private String phone;
  286.     private String address;
  287.     private String birthCity;
  288.     private String mrn;
  289.     private long id;
  290.     private String sex;
  291.     private String nationality;
  292.     private String isDisabled;
  293.     private String race;
  294.  
  295.     public String getFirstName() {
  296.         return firstName;
  297.     }
  298.  
  299.     public void setFirstName(String firstName) {
  300.         this.firstName = firstName;
  301.     }
  302.  
  303.     public String getMiddleName() {
  304.         return middleName;
  305.     }
  306.  
  307.     public void setMiddleName(String middleName) {
  308.         this.middleName = middleName;
  309.     }
  310.  
  311.     public String getLastName() {
  312.         return lastName;
  313.     }
  314.  
  315.     public void setLastName(String lastName) {
  316.         this.lastName = lastName;
  317.     }
  318.  
  319.     public Calendar getDateOfBirth() {
  320.         return dateOfBirth;
  321.     }
  322.  
  323.     public void setDateOfBirth(Calendar dateOfBirth) {
  324.         this.dateOfBirth = dateOfBirth;
  325.     }
  326.  
  327.     public String getCity() {
  328.         return city;
  329.     }
  330.  
  331.     public void setCity(String city) {
  332.         this.city = city;
  333.     }
  334.  
  335.     public String getZipCode() {
  336.         return zipCode;
  337.     }
  338.  
  339.     public void setZipCode(String zipCode) {
  340.         this.zipCode = zipCode;
  341.     }
  342.  
  343.     public String getPhone() {
  344.         return phone;
  345.     }
  346.  
  347.     public void setPhone(String phone) {
  348.         this.phone = phone;
  349.     }
  350.  
  351.     public String getAddress() {
  352.         return address;
  353.     }
  354.  
  355.     public void setAddress(String address) {
  356.         this.address = address;
  357.     }
  358.  
  359.     public String getBirthCity() {
  360.         return birthCity;
  361.     }
  362.  
  363.     public void setBirthCity(String birthCity) {
  364.         this.birthCity = birthCity;
  365.     }
  366.  
  367.     public String getMrn() {
  368.         return mrn;
  369.     }
  370.  
  371.     public void setMrn(String mrn) {
  372.         this.mrn = mrn;
  373.     }
  374.  
  375.     public long getId() {
  376.         return id;
  377.     }
  378.  
  379.     public void setId(long id) {
  380.         this.id = id;
  381.     }
  382.  
  383.     public String getSex() {
  384.         return sex;
  385.     }
  386.  
  387.     public void setSex(String sex) {
  388.         this.sex = sex;
  389.     }
  390.  
  391.     public String getNationality() {
  392.         return nationality;
  393.     }
  394.  
  395.     public void setNationality(String nationality) {
  396.         this.nationality = nationality;
  397.     }
  398.  
  399.     public String getIsDisabled() {
  400.         return isDisabled;
  401.     }
  402.  
  403.     public void setIsDisabled(String isDisabled) {
  404.         this.isDisabled = isDisabled;
  405.     }
  406.  
  407.     public String getRace() {
  408.         return race;
  409.     }
  410.  
  411.     public void setRace(String race) {
  412.         this.race = race;
  413.     }
  414.  
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement