mirror of
https://github.com/svek95/WST_PersonTest.git
synced 2026-05-18 02:51:47 +03:00
add gitignore
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Person> getPersons() {
|
||||
PostgreSQLDAO dao = new PostgreSQLDAO(getConnection());
|
||||
List<Person> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Person> getPersons(){
|
||||
List<Person> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user