mirror of
https://github.com/svek95/WST_Labs.git
synced 2026-06-09 23:18:27 +03:00
Lab2
Change IP do DB
This commit is contained in:
@@ -7,7 +7,8 @@ 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_URL = "jdbc:postgresql://192.168.1.119:5432/";
|
||||
private static final String JDBC_URL = "jdbc:postgresql://5.19.136.134:5432/";
|
||||
private static final String JDBC_USER = "postgres";
|
||||
private static final String JDBC_PASSWORD = "qwerty";
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package Lab2;
|
||||
|
||||
//import Lab1.CarWebService;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
|
||||
@@ -1,4 +1,47 @@
|
||||
package Lab2;
|
||||
|
||||
public class CarWebCrudService {
|
||||
|
||||
import Lab1.CarWebService;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebParam;
|
||||
import javax.jws.WebService;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import java.sql.Connection;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@WebService(name = "CarService")
|
||||
public class CarWebCrudService extends CarWebService{
|
||||
|
||||
private final static CarsDao carsDao = new CarsDao();
|
||||
|
||||
@WebMethod
|
||||
public Integer insertCars(
|
||||
@XmlElement(required = true)
|
||||
@WebParam(name = "Name") String name,
|
||||
@WebParam(name = "model") String model,
|
||||
@WebParam(name = "country") String country,
|
||||
@WebParam(name = "dateOfSales") Date dateOfSales,
|
||||
@WebParam(name = "power") int power) {
|
||||
return CarsDao.insertDB(name, model, country, dateOfSales, power);
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public String updateCars(@XmlElement(required = true)
|
||||
@WebParam(name = "CarsId") Integer id,
|
||||
@WebParam(name = "Name") String name,
|
||||
@WebParam(name = "model") String model,
|
||||
@WebParam(name = "country") String country,
|
||||
@WebParam(name = "dateOfSales") Date dateOfSales,
|
||||
@WebParam(name = "power") int power) {
|
||||
return CarsDao.updateDB(id, name, model, country, dateOfSales, power).getStringValue();
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public String deleteCars(@XmlElement(required = true)
|
||||
@WebParam(name = "CarsId") Integer id) {
|
||||
return CarsDao.deleteDB(id).getStringValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package Lab2;
|
||||
|
||||
import Lab1.CarDAO;
|
||||
import Lab1.ConnectionUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CarsDao {
|
||||
|
||||
public static Integer insertDB(String name, String model, String country, Date dateOfSales, int power) {
|
||||
String query = " insert into cars (name, model, country, dateOfSales, power)"
|
||||
+ " values (?, ?, ?, ?, ?)";
|
||||
int index = 1;
|
||||
Integer id = null;
|
||||
try (Connection connection = ConnectionUtil.getConnection();) {
|
||||
|
||||
try (PreparedStatement preparedStmt = connection.prepareStatement(query)) {
|
||||
preparedStmt.setString(index++, name);
|
||||
preparedStmt.setString(index++, model);
|
||||
preparedStmt.setDate(index++, dateOfSales != null
|
||||
? new java.sql.Date(dateOfSales.getTime())
|
||||
: null);
|
||||
preparedStmt.setString(index++, country);
|
||||
preparedStmt.setObject(index, power);
|
||||
preparedStmt.executeUpdate();
|
||||
try (ResultSet result = connection.createStatement().executeQuery("select max(id) as maxid from cars")) {
|
||||
result.next();
|
||||
id = result.getInt("maxid");
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
private static void addUpdatedValues(Object value, String fieldName, StringBuilder query) {
|
||||
if (value != null) {
|
||||
if (value instanceof String) {
|
||||
query.append(fieldName).append(" = '").append(value).append("'").append(", ");
|
||||
} else {
|
||||
query.append(fieldName).append(" = ").append(value).append(',');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static CheckStatus updateDB(Integer id, String name, String model, String country, Date dateOfSales, int power) {
|
||||
StringBuilder query = new StringBuilder(" update cars set ");
|
||||
addUpdatedValues(name, "name", query);
|
||||
addUpdatedValues(model, "model", query);
|
||||
addUpdatedValues(country, "country", query);
|
||||
addUpdatedValues(dateOfSales, "dateOfSales", query);
|
||||
addUpdatedValues(power, "power", query);
|
||||
query.replace(query.lastIndexOf("."), query.length(), " ");
|
||||
query.append(" where id = " + id);
|
||||
try (Connection connection = ConnectionUtil.getConnection();) {
|
||||
try (PreparedStatement preparedStmt = connection.prepareStatement(query.toString())) {
|
||||
preparedStmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return CheckStatus.FAIL;
|
||||
}
|
||||
return CheckStatus.SUCCESS_UPDATE;
|
||||
}
|
||||
|
||||
public static CheckStatus deleteDB(Integer id) {
|
||||
String query = "DELETE FROM cars WHERE id = " + id;
|
||||
try (Connection connection = ConnectionUtil.getConnection()) {
|
||||
try (PreparedStatement preparedStmt = connection.prepareStatement(query)) {
|
||||
preparedStmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CarDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return CheckStatus.FAIL;
|
||||
}
|
||||
return CheckStatus.SUCCESS_DELETE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package Lab2;
|
||||
|
||||
public enum CheckStatus {
|
||||
|
||||
SUCCESS_UPDATE("Row was successfully updated"),
|
||||
SUCCESS_DELETE("Row eas successfully deleted"),
|
||||
FAIL("Something went wrong");
|
||||
|
||||
String value;
|
||||
|
||||
CheckStatus(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getStringValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user