Hi,
i am trying to add my form to the sql but i get a null error i tested with printout and the value are there.
and my update grid also not showing me nothing in the grid.
can you assist me on this ? thank you.
my code is :
Todo.java
package com.packagename.myapp.spring;
import java.time.LocalDate;
import java.util.Date;
public class Todo {
private long userID;
private String taskstatus;
private long ID;
private long customerID;
private long descriptionID;
private String taskStatus;
private String Subject;
private LocalDate Dudate;
public Todo() // Def Ctor
{
}
public Todo (String taskstatus,LocalDate dudate,String subject,long customerid){
this.customerID= customerid;
this.Subject = subject;
this.Dudate = dudate;
this.taskstatus = taskstatus;
}
public Todo(long id, long customerid, long descriptionid, String task) { // Ctor all
this.ID=id;
this.customerID=customerid;
this.descriptionID=descriptionid;
this.taskStatus=task;
}
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public long getDescriptionID() {
return descriptionID;
}
public void setDescriptionID(long descriptionID) {
this.descriptionID = descriptionID;
}
public String getTaskStatus() {
return taskStatus;
}
public void setTaskStatus(String taskStatus) {
this.taskStatus = taskStatus;
}
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
this.Subject = subject;
}
public LocalDate getDudate() {
return Dudate;
}
public void setDudate(LocalDate dudate) {
this.Dudate = dudate;
}
public long getCustomerID() {
return customerID;
}
public void setCustomerID(long customerID) {
this.customerID = customerID;
}
}
TodoView.java
@Route(value = NAV,layout = MainView.class)
public class TodoView extends Composite<Div> {
public static final String NAV = "todo";
public Todo todo = new Todo();
private Grid<Todo> todogrid = new Grid<>(Todo.class);
private Binder<Todo> binder = new Binder<>(Todo.class);
private TodoService service ;
private Button addButton = new Button("Save", e -> {
try {
binder.writeBean(todo);
// System.out.println(todo.getCustomerID());
// System.out.println(todo.getTaskStatus());
// System.out.println(todo.getDudate());
// System.out.println(todo.getSubject());
saveTodo();
// updateGrid();
binder.readBean(new Todo());
} catch (ValidationException ex) {
ex.printStackTrace();
}
});
public TodoView(){
//Layouts//
HorizontalLayout todofields = new HorizontalLayout();
VerticalLayout todoList = new VerticalLayout();
HorizontalLayout subjects = new HorizontalLayout();
todogrid.addThemeNames("no-border","no-row-borders","row-strips");
GridMultiSelectionModel<Todo> selectionModel = (GridMultiSelectionModel<Todo>) todogrid
.setSelectionMode(Grid.SelectionMode.MULTI);
selectionModel.selectAll();
//Layouts//
//Fields//
ComboBox<String> taskstatus = new ComboBox<>("TaskStatus:");
taskstatus.setItems("Completed","OnProgress","Cancelled");
TextField subject = new TextField();
subject.setLabel("נושא:");
TextField customerid = new TextField();
customerid.setLabel("שם הלקוח:");
DatePicker dudate = new DatePicker();
dudate.setClearButtonVisible(true);
dudate.setLabel("תאריך סיום:");
//Binder//
binder.forField(subject).bind(Todo::getSubject,Todo::setSubject);
binder.forField(dudate).bind(Todo::getDudate,Todo::setDudate);
binder.forField(taskstatus).bind(Todo::getTaskStatus,Todo::setTaskStatus);
binder.forField(customerid).withConverter(new StringToLongConverter(""))
.bind(Todo::getCustomerID,Todo::setCustomerID);
//Binder//
addButton.addClickShortcut(Key.ENTER);
addButton.setThemeName("primary");
//Fields//
todofields.add(subject,customerid,dudate,taskstatus,addButton);
todofields.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.BASELINE);
todoList.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.BASELINE);
todofields.getElement().getStyle().set("margin-left","auto");
getContent().add(todofields,subjects,todoList,todogrid);
Direction.set(Direction.RTL);
}
private void saveTodo() {
service.update(todo);
}
private void updategrid() {
List<Todo> todo = service.findAll();
todogrid.setItems(todo);
}
}
mainview.java
package com.packagename.myapp.spring;
import static com.vaadin.flow.component.icon.VaadinIcon.*;
import java.util.HashMap;
import java.util.Map;
@Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
@Theme(Lumo.class)
@Route
@PWA(name = "SimpleIT", shortName = "SimpeIT")
public class MainView extends AppLayout {
public static final String ITM_DASHBOARD = "DashBoard";
private Map<Tab, Component> tab2Workspace = new HashMap<>();
public MainView() {
Direction.set(Direction.RTL);
// setPrimarySection(Section.DRAWER);
Image img = new Image("https://i.ibb.co/KbZbtWm/SimpleIt.png", "Vaadin Logo");
img.setHeight("75px");
addToNavbar(new MenuBar(), img);
Tabs menu = new Tabs(dashBoard()
,customers(),Todo(),Tickets());
menu.setOrientation(Tabs.Orientation.HORIZONTAL);
menu.addSelectedChangeListener(event -> {
final Tab selectedTab = event.getSelectedTab();
final Component component = tab2Workspace.get(selectedTab);
setContent(component);
});
addToNavbar(menu);
this.setPrimarySection(Section.NAVBAR);
setContent(new Span("click in the menu ;-) , you will see me never again.."));
}
///**// Tabs Area//**///
private Tab Tickets() {
final Span label = new Span("קריאות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new TicketsView());
return tab;
}
private Tab customers() {
final Span label = new Span("לקוחות");
final Icon icon = BOOK.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new CustomersView());
return tab;
}
private Tab dashBoard() {
final Span label = new Span("ראשי");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new DashBoardView());
return tab;
}
private Tab Todo() {
final Span label = new Span("משימות");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab,new TodoView());
return tab;
}
//**//Ends Of Tab Area//**//
}
TodoService.java
package com.packagename.myapp.spring.Services;
import com.packagename.myapp.spring.Todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TodoService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Todo> findAll() {
return jdbcTemplate.query(
"SELECT ID, UserID, notesID,TaskStatus FROM todo",
(rs, rowNum) -> new Todo(rs.getLong("ID"),
rs.getLong("UserID"), rs.getLong("descriptionID"),rs.getString("taskstatus")));
}
public void update(Todo todo) {
jdbcTemplate.update("INSERT INTO todo (TaskStatus,dudate,subject,CustomerID) VALUES(?,?,?,?)",todo.getTaskStatus(),todo.getDudate(),todo.getSubject(),todo.getCustomerID());
}
}