Я уже разработал get()
и getAll()
методы. но есть проблема с методами save()
, update()
, delete()
. Тем не менее, я хотел знать, как мне создать эти методы внутри моего StudentDAOImpl.java
файла?
StudentDAOImpl.java
package com.vertx.student.crud.DAO.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import com.vertx.student.crud.Config.DBConnection;
import com.vertx.student.crud.DAO.StudentDAO;
import com.vertx.student.crud.Model.Student;
import org.jooq.DSLContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariDataSource;
public class StudentDAOImpl implements StudentDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(StudentDAOImpl.class);
@Override
public Student get(Long studentID) throws SQLException {
LOGGER.info("Fetching record from the database for the id : {}", studentID);
HikariDataSource dataSource = DBConnection.getConnection();
Connection conn = null;
Student student = null;
try {
conn = dataSource.getConnection();
DSLContext dslContext = DBConnection.getDSLContext(conn);
String sql = "SELECT * FROM Student where id = " + studentID;
student = dslContext.fetchOne(sql).into(Student.class);
LOGGER.info("Student record fetched successfully from db: {}", student);
} catch (SQLException ex) {
LOGGER.info(ex.getMessage() + ex);
}
return student;
}
@Override
public List<Student> getAll() {
LOGGER.info("Fetching all student from the database");
List<Student> students = null;
HikariDataSource dataSource = DBConnection.getConnection();
try (Connection conn = dataSource.getConnection()) {
DSLContext dslContext = DBConnection.getDSLContext(conn);
String sql = "SELECT * FROM Student ";
students = dslContext.fetch(sql).into(Student.class);
LOGGER.info("All students fetched successfully from db: {}", students);
} catch (SQLException ex) {
LOGGER.info(ex.getMessage() + ex);
}
return students;
}
@Override
public Student save(Student student) {
LOGGER.info("Save student record to the database according to relevant id :");
HikariDataSource dataSource = DBConnection.getConnection();
Connection conn = null;
try {
conn = dataSource.getConnection();
DSLContext dslContext = DBConnection.getDSLContext(conn);
String sql = "INSERT INTO Student(id,name,classname,address,phoneNumber,creationDate) VALUES(?,?,?,?,?,?)";
student = (Student) dslContext.fetch(sql).into(Student.class);
LOGGER.info("Student saved successfully to the db: {} ", student);
} catch (SQLException ex) {
LOGGER.info(ex.getMessage() + ex);
}
return new Student();
}
@Override
public Student update(Student student) {
LOGGER.info("Update student record to the database to relevant id :");
HikariDataSource dataSource = DBConnection.getConnection();
try
(Connection conn = dataSource.getConnection()) {
DSLContext dslContext = DBConnection.getDSLContext(conn);
String sql = "UPDATE Student SET name = ?, classname = ? ,address = ? , phoneNumber = ? WHERE id = ?";
student = (Student) dslContext.fetch(sql).into(Student.class);
LOGGER.info("Student Update successfully to the db: {} ", student);
} catch (SQLException ex) {
LOGGER.info(ex.getMessage() + ex);
}
return new Student();
}
@Override
public Student delete(Long studentID) {
LOGGER.info("Deleting record from the database for the id : {}", studentID);
HikariDataSource dataSource = DBConnection.getConnection();
Connection conn = null;
Student student = null;
try {
conn = dataSource.getConnection();
DSLContext dslContext = DBConnection.getDSLContext(conn);
String sql = "DELETE FROM Student where id = " + studentID;
student = dslContext.fetchOne(sql).into(Student.class);
LOGGER.info("Student record deleted successfully from db: {}", student);
} catch (SQLException ex) {
LOGGER.info(ex.getMessage() + ex);
}
return new Student();
}
}
Я уже разработал методы update()
, save()
и delete()
. Но они не дают ожидаемого значения в соответствии с REST API. Методы get()
и getAll()
работают здесь правильно.
Я ожидал вывода методов update()
, save()
и delete()
в соответствии с их REST API, но они не работают должным образом.
Это вывод после запуска файла jar:
> ERROR StatusLogger No logging configuration WARNING: An illegal
> reflective access operation has occurred WARNING: Illegal reflective
> access by io.netty.util.internal.ReflectionUtil
> (file:/home/kapila/Desktop/vertex%20folder/StudentCrudSystem%20Using%20vertx/CRUD%20ooperation%20with%20API/target/my-first-app-1.0-SNAPSHOT.jar)
> to constructor java.nio.DirectByteBuffer(long,int) WARNING: Please
> consider reporting this to the maintainers of
> io.netty.util.internal.ReflectionUtil WARNING: Use
> --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be
> denied in a future release Nov 06, 2019 6:14:37 AM
> io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer INFO:
> Succeeded in deploying verticle 06:18:49.883 [vert.x-worker-thread-4]
> ERROR com.vertx.student.crud.Resources.StudentResource - Create
> student request body: {"id":8,"name":"Chandana","className":"Class
> F","address":"Ampara","phoneNumber":12364986,"creationDate":null}
> 06:18:49.924 [vert.x-worker-thread-4] ERROR
> com.vertx.student.crud.Resources.StudentResource - Error occurred
> while saving students: org.jooq.exception.DataAccessException: SQL
> [INSERT INTO
> Student(id,name,classname,address,phoneNumber,creationDate)
> VALUES(?,?,?,?,?,?)]; No value specified for parameter 1
> at org.jooq_3.10.3.MYSQL.debug(Unknown Source) ~[?:?]
> at org.jooq.impl.Tools.translate(Tools.java:2197) ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:704)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:361) ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:317)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at org.jooq.impl.DefaultDSLContext.fetch(DefaultDSLContext.java:767)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at com.vertx.student.crud.DAO.impl.StudentDAOImpl.save(StudentDAOImpl.java:94)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at com.vertx.student.crud.Resources.StudentResource.save(StudentResource.java:68)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:223)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:200)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:533)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at io.vertx.core.impl.TaskQueue.run(TaskQueue.java:76) ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
> ~[?:?]
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> ~[?:?]
> at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
> ~[my-first-app-1.0-SNAPSHOT.jar:?]
> at java.lang.Thread.run(Thread.java:834) ~[?:?]