From 1ea84336498c72275a0388c8f13e701fcab63bb6 Mon Sep 17 00:00:00 2001 From: SVEK Date: Sat, 23 Feb 2019 20:07:38 +0300 Subject: [PATCH] add gitignore --- src/App.java | 10 ++++++++++ src/ConnectionUtil.java | 30 ++++++++++++++++++++++++++++++ src/Person.java | 39 +++++++++++++++++++++++++++++++++++++++ src/PersonWebService.java | 34 ++++++++++++++++++++++++++++++++++ src/PostgreSQLDAO.java | 38 ++++++++++++++++++++++++++++++++++++++ web/WEB-INF/web.xml | 6 ++++++ web/index.jsp | 16 ++++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 src/App.java create mode 100644 src/ConnectionUtil.java create mode 100644 src/Person.java create mode 100644 src/PersonWebService.java create mode 100644 src/PostgreSQLDAO.java create mode 100644 web/WEB-INF/web.xml create mode 100644 web/index.jsp diff --git a/src/App.java b/src/App.java new file mode 100644 index 0000000..dedea82 --- /dev/null +++ b/src/App.java @@ -0,0 +1,10 @@ +import javax.xml.ws.Endpoint; + +public class App +{ + public static void main( String[] args ) + { + String url = "http://localhost:8080/PersonService"; + Endpoint.publish(url, new PersonWebService()); + } +} diff --git a/src/ConnectionUtil.java b/src/ConnectionUtil.java new file mode 100644 index 0000000..0956d05 --- /dev/null +++ b/src/ConnectionUtil.java @@ -0,0 +1,30 @@ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; + + +public class ConnectionUtil { + private static final String JDBC_URL = "jdbc:postgresql://192.168.1.119:5432/postgres"; + private static final String JDBC_USER = "postgres"; + private static final String JDBC_PASSWORD = "qwerty"; + + static { + try { + Class.forName("org.postgresql.Driver"); + } catch (ClassNotFoundException ex) { + Logger.getLogger(PostgreSQLDAO.class.getName()).log(Level.SEVERE, null, ex); + } + } + public static Connection getConnection() { + Connection connection = null; + try { + connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, + JDBC_PASSWORD); + } catch (SQLException ex) { + Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex); + } + return connection; + } +} \ No newline at end of file diff --git a/src/Person.java b/src/Person.java new file mode 100644 index 0000000..b810e94 --- /dev/null +++ b/src/Person.java @@ -0,0 +1,39 @@ +import javax.xml.bind.annotation.XmlRootElement; +@XmlRootElement +public class Person { + private String name; + private String surname; + private int age; + public Person() { + } + public Person(String name, String surname, int age) { + this.name = name; + this.surname = surname; + this.age = age; + } + public String getName() { + return name; + } + public String getSurname() { + return surname; + } + public int getAge() { + return age; + } + public void setName(String name) { + + this.name = name; + } + public void setSurname(String surname) { + this.surname = surname; + } + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "Person{" + "name=" + name + ", surname=" + surname + ", age=" + age + '}'; + } + +} diff --git a/src/PersonWebService.java b/src/PersonWebService.java new file mode 100644 index 0000000..13f434a --- /dev/null +++ b/src/PersonWebService.java @@ -0,0 +1,34 @@ +import javax.annotation.Resource; +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + + +@WebService(serviceName = "PersonService") +public class PersonWebService { + @Resource(lookup = "jdbc/ifmo-ws") + private DataSource dataSource; + + @WebMethod(operationName = "getPersons") + public List getPersons() { + PostgreSQLDAO dao = new PostgreSQLDAO(getConnection()); + List persons = dao.getPersons(); + return persons; + } + + private Connection getConnection() { + Connection result = null; + try { + result = dataSource.getConnection(); + } catch (SQLException ex) { + Logger.getLogger(PersonWebService.class.getName()).log(Level.SEVERE, null, ex); + } + return result; + } + +} \ No newline at end of file diff --git a/src/PostgreSQLDAO.java b/src/PostgreSQLDAO.java new file mode 100644 index 0000000..90fae22 --- /dev/null +++ b/src/PostgreSQLDAO.java @@ -0,0 +1,38 @@ +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by artemsolonin on 12.05.2017. + */ +public class PostgreSQLDAO { + public PostgreSQLDAO(Connection connection) { + + } + + public List getPersons(){ + List persons = new ArrayList<>(); + try(Connection connection = ConnectionUtil.getConnection()){ + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM persons"); + + while (rs.next()){ + String name = rs.getString("name"); + String surname = rs.getString("surname"); + int age = rs.getInt("age"); + + Person person = new Person(name,surname,age); + persons.add(person); + } + } + catch (SQLException se) + { + se.printStackTrace(); + } + + return persons; + } +} \ No newline at end of file diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp new file mode 100644 index 0000000..c112873 --- /dev/null +++ b/web/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: svek + Date: 2019-02-23 + Time: 19:25 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + +