mirror of
https://github.com/svek95/WST_Labs.git
synced 2026-05-17 18:44:02 +03:00
Add Lab5
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
package Lab5;
|
||||||
|
|
||||||
|
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
|
||||||
|
import com.sun.jersey.api.core.ClassNamesResourceConfig;
|
||||||
|
import com.sun.jersey.api.core.ResourceConfig;
|
||||||
|
import org.glassfish.grizzly.http.server.HttpServer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
private static final URI BASE_URI = URI.create("http://localhost:8081/rest/");
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
HttpServer server = null;
|
||||||
|
try {
|
||||||
|
ResourceConfig resourceConfig = new ClassNamesResourceConfig(CarCrudResource.class);
|
||||||
|
server = GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
|
||||||
|
server.start();
|
||||||
|
System.in.read();
|
||||||
|
stopServer(server);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
stopServer(server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void stopServer(HttpServer server) {
|
||||||
|
if (server != null)
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package Lab5;
|
||||||
|
|
||||||
|
//import Lab2.CheckStatus;
|
||||||
|
import Lab4.common.Car;
|
||||||
|
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Path("/cars")
|
||||||
|
@Produces({MediaType.APPLICATION_JSON})
|
||||||
|
public class CarCrudResource {
|
||||||
|
|
||||||
|
private final static CarsDAO carDao = new CarsDAO();
|
||||||
|
|
||||||
|
/// Find
|
||||||
|
@GET
|
||||||
|
public List<Car> findCars(@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return carDao.getDataByFields(name, model, country, dateOfSales, power);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// seve
|
||||||
|
@POST
|
||||||
|
@Path("/saves")
|
||||||
|
public String insertCars(@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return carDao.insertDB(name, model, country, dateOfSales, power).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update id
|
||||||
|
@PUT
|
||||||
|
@Path("/update/ID")
|
||||||
|
public String updateCars(@QueryParam("id") Integer id,
|
||||||
|
@QueryParam("Name") String name,
|
||||||
|
@QueryParam("model") String model,
|
||||||
|
@QueryParam("country") String country,
|
||||||
|
@QueryParam("dateOfSales") Date dateOfSales,
|
||||||
|
@QueryParam("power") Integer power) {
|
||||||
|
return String.valueOf(carDao.updateDB(id, name, model, country, dateOfSales, power));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete
|
||||||
|
|
||||||
|
@Path("/delete/ID")
|
||||||
|
@DELETE
|
||||||
|
public String deleteCars(@QueryParam("id") Integer id) {
|
||||||
|
|
||||||
|
return carDao.deleteDB(id).getStringValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package Lab5;
|
||||||
|
|
||||||
|
import Lab1.CarDAO;
|
||||||
|
import Lab1.ConnectionUtil;
|
||||||
|
import Lab2.CheckStatus;
|
||||||
|
import Lab4.common.StandaloneCarDAO;
|
||||||
|
|
||||||
|
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 extends StandaloneCarDAO {
|
||||||
|
|
||||||
|
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 addUpdated(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 ");
|
||||||
|
addUpdated(name, "name", query);
|
||||||
|
addUpdated(model, "model", query);
|
||||||
|
addUpdated(country, "country", query);
|
||||||
|
addUpdated(dateOfSales, "dateOfSales", query);
|
||||||
|
addUpdated(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean findID(Integer id) {
|
||||||
|
boolean isIdPresent = false;
|
||||||
|
String query = "select count(*) from cars where id = ?";
|
||||||
|
try (Connection connection = ConnectionUtil.getConnection()) {
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(query)) {
|
||||||
|
ps.setInt(1, id);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
isIdPresent = rs.getInt(1) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
Logger.getLogger(CarsDAO.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
} finally {
|
||||||
|
return isIdPresent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user