mirror of
https://github.com/svek95/WST_Labs.git
synced 2026-06-09 23:18:27 +03:00
Initial commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
|
||||
import Lab1.CarWebService;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
String url = "http://localhost:8081/CarService";
|
||||
Endpoint.publish(url, new CarWebService());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package Lab1;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Car {
|
||||
private String name;
|
||||
private Date dateOfStart;
|
||||
private String country;
|
||||
private double duration;
|
||||
private String director;
|
||||
|
||||
public Car(String name, Date date, String country, double duration, String director) {
|
||||
this.name = name;
|
||||
this.dateOfStart = date;
|
||||
this.country = country;
|
||||
this.duration = duration;
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Date getDateOfStart() {
|
||||
return dateOfStart;
|
||||
}
|
||||
|
||||
public void setDateOfStart(Date dateOfStart) {
|
||||
this.dateOfStart = dateOfStart;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public double getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(double duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package Lab1;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public abstract class CarDAO {
|
||||
|
||||
private Boolean addChecking(Object value, String fieldName, Boolean isNotFirst, StringBuilder query) {
|
||||
if (value != null) {
|
||||
if (isNotFirst) {
|
||||
query.append(" and ");
|
||||
}
|
||||
if (value instanceof String) {
|
||||
query.append(fieldName).append(" = '").append(value).append("'");
|
||||
} else {
|
||||
query.append(fieldName).append(" = ").append(value);
|
||||
}
|
||||
if (!isNotFirst) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return isNotFirst;
|
||||
}
|
||||
|
||||
public abstract Connection getConnection();
|
||||
|
||||
public List<Car> getDataByFields(String name, String director, String country, Date dateOfStart, Double duration) {
|
||||
List<Car> cars = Collections.emptyList();
|
||||
try (Connection connection = getConnection()) {
|
||||
StringBuilder query = new StringBuilder("select * from cars");
|
||||
boolean notFirstField = false;
|
||||
if (name != null || director != null || country != null && dateOfStart != null || duration != null) {
|
||||
query.append(" where ");
|
||||
}
|
||||
notFirstField = addChecking(name, "name", notFirstField, query);
|
||||
notFirstField = addChecking(director, "director", notFirstField, query);
|
||||
notFirstField = addChecking(country, "country", notFirstField, query);
|
||||
notFirstField = addChecking(dateOfStart, "dateOfStart", notFirstField, query);
|
||||
addChecking(duration, "duration", notFirstField, query);
|
||||
|
||||
PreparedStatement stmt = connection.prepareStatement(query.toString());
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
cars = extractFilmsFromResultSet(rs);
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return cars;
|
||||
}
|
||||
|
||||
private List<Car> extractFilmsFromResultSet(ResultSet rs) throws SQLException{
|
||||
List<Car> cars = new ArrayList<Car>();
|
||||
while (rs.next()) {
|
||||
String name = rs.getString("name");
|
||||
Date date = rs.getDate("dateOfStart");
|
||||
String director = rs.getString("director");
|
||||
String country = rs.getString("country");
|
||||
double duration = rs.getDouble("duration");
|
||||
|
||||
Car car = new Car(name, date, country, duration, director);
|
||||
cars.add(car);
|
||||
} return cars;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package Lab1;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebParam;
|
||||
import javax.jws.WebService;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@WebService(name = "FilmService")
|
||||
public class CarWebService {
|
||||
|
||||
private final static CarDAO filmDao = new StandaloneCarDAO();
|
||||
|
||||
@WebMethod
|
||||
public List<Car> getFilmsByFields(@WebParam(name = "filmName") String name,
|
||||
@WebParam(name = "director") String director,
|
||||
@WebParam(name = "country") String country,
|
||||
@WebParam(name = "dateOfStart") Date date,
|
||||
@WebParam(name = "duration") Double duration) {
|
||||
return filmDao.getDataByFields(name, director, country, date, duration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package Lab1;
|
||||
|
||||
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/";
|
||||
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(CarDAO.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,11 @@
|
||||
package Lab1;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class StandaloneCarDAO extends CarDAO {
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return ConnectionUtil.getConnection();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user