我有以下任务:
这是一个简单的MVC,JavaFX单场景应用程序。
这是一种用于注册互联网套餐销售的表格,其中包含ListView
个先前购买的商品。
互联网程序包由几个参数组成:速度,流量,合同期限,ID,用户名和姓氏以及用户地址。
功能包括:查看所有销售,添加新销售和删除现有销售。
代码编译没有错误,但是当我运行它时,我收到一个错误
package mainMethod;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
URL fxmlUrl = getClass().getClassLoader().getResource("view/SalesView.fxml");
BorderPane root = FXMLLoader.<BorderPane>load(fxmlUrl);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Registration of internet package sales");
primaryStage.centerOnScreen();
primaryStage.setResizable(false);
primaryStage.show();
}
}
package controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import model.ProdajaModel;
public class SalesController {
SalesModel sales;
public SalesController() {
}
@FXML
private ChoiceBox cbSpeed;
@FXML
private ChoiceBox cbFlow;
@FXML
private ChoiceBox cbContract;
@FXML
private TextField tfNameSurname;
@FXML
private TextField tfAdress;
@FXML
private ListView listView;
ObservableList<SalesModel> salesLista = FXCollections.observableArrayList();
private ObservableList<SalesModel> display() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
ObservableList<SalesModel> salesLista1 = FXCollections.observableArrayList();
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_packages", "root", "");) {
Statement st = conn.createStatement();
st.executeQuery("select * from sales");
ResultSet rs = st.getResultSet();
while (rs.next()) {
SalesModel sales = new SalesModel(
Integer.parseInt(rs.getString("speed")),
rs.getString("flow"),
Integer.parseInt(rs.getString("contract")),
Integer.parseInt(rs.getString("salesId")),
rs.getString("nameSurname"),
rs.getString("adress"));
salesLista1.add(sales);
salesLista = salesLista1;
}
} catch (SQLException e) {
System.out.println("ERROR: " + e.getMessage());
}
return salesLista1;
}
@FXML
private void initialize() throws ClassNotFoundException {
sales = new SalesModel();
cbSpeed.getItems().addAll(2, 5, 10, 20, 50, 100);
cbSpeed.valueProperty().bindBidirectional(sales.speedProperty());
cbFlow.getItems().addAll(1, 5, 10, 100, "Flat");
cbFlow.valueProperty().bindBidirectional(sales.flowProperty());
cbContract.getItems().addAll(1, 2);
cbContract.valueProperty().bindBidirectional(sales.contractProperty());
tfNameSurname.textProperty().bindBidirectional(sales.nameSurnameProperty());
tfAdress.textProperty().bindBidirectional(sales.adressProperty());
display();
listView.setItems(salesLista);
}
@FXML
private void clearSell() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
int selIdx = listView.getSelectionModel().getSelectedIndex();
String modId = null;
for (SalesModel mod : salesLista) {
if (selIdx == salesLista.indexOf(mod)) {
modId = Integer.toString(mod.getId());
}
}
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_packages", "root", "");) {
PreparedStatement st = conn.prepareStatement("delete from sales where salesId=?");
st.setString(1, modId);
st.execute();
listView.setItems(display());
} catch (SQLException e) {
System.out.println("ERROR! " + e.getMessage());
}
}
@FXML
private void prodaj() throws ClassNotFoundException {
if (sales.isValid()) {
sales.setId(salesLista.size() + 1);
sales.save();
listView.setItems(display());
} else {
StringBuilder errMsg = new StringBuilder();
ArrayList<String> errList = sales.errorsProperty().get();
for (String errList1 : errList) {
errMsg.append(errList1);
}
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Sale cannot be executed!");
alert.setHeaderText(null);
alert.setContentText(errMsg.toString());
alert.showAndWait();
errList.clear();
}
}
}
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class SalesModel {
List<SalesModel> listaSales;
private final IntegerProperty speed = new SimpleIntegerProperty(this,"speed");
private final ObjectProperty flow = new SimpleObjectProperty(this,"flow");
private final IntegerProperty contract = new SimpleIntegerProperty(this,"contract");
private final IntegerProperty id = new SimpleIntegerProperty(this,"id");
private final StringProperty imePrezime = new SimpleStringProperty(this,"nameSurname");
private final StringProperty adress = new SimpleStringProperty(this,"adress");
private final ObjectProperty<ArrayList<String>> errorList = new SimpleObjectProperty<>(this, "errorList", new ArrayList<>());
public SalesModel() {}
public SalesModel(int speed,Object flow,int contract,int id,String nameSurname,String adress) {
this.speed.set(speed);
this.flow.set(flow);
this.contract.set(contract);
this.id.set(id);
this.nameSurname.set(nameSurname);
this.adress.set(adress);
}
public int getSpeed() {return speed.get();}
public void setSpeed(int speed) {this.speed.set(speed);}
public IntegerProperty speedProperty() {return speed;}
public Object getFlow() {return flow.get();}
public void setFlow(Object flow) {this.flow.set(flow);}
public ObjectProperty flowProperty() {return flow;}
public int getContract() {return contract.get();}
public void setContract(int contract) {this.contract.set(contract);}
public IntegerProperty contractProperty() {return contract;}
public int getId() {return id.get();}
public void setId(int id) {this.id.set(id);}
public IntegerProperty idProperty() {return id;}
public String getNameSurname() {return nameSurname.get();}
public void setNameSurname(String nameSurname) {this.nameSurname.set(nameSurname);}
public StringProperty nameSurnameProperty() {return nameSurname;}
public String getAdress() {return adress.get();}
public void setAdress(String adress) {this.adress.set(adress);}
public StringProperty adressProperty() {return adress;}
public ObjectProperty<ArrayList<String>> errorsProperty() {return errorList;}
public boolean isValid() {
boolean isValid = true;
if (nameSurname.get() == null) {
errorList.getValue().add(" The name and surname must be entered!");
isValid = false;
}
if (adress.get() == null) {
errorList.getValue().add(" The address must be entered!");
isValid = false;
}
if (flow.get() == null) {
errorList.getValue().add(" The flow must be selected!");
isValid = false;
}
if (contract.get() == 0) {
errorList.getValue().add(" Trajanje ugovora mora biti odredjeno!");
isValid = false;
}
if (speed.get() == 0) {
errorList.getValue().add(" The flow velocity must be selected!");
isValid = false;
}
return isValid;
}
public String save() throws ClassNotFoundException {
String message = "The entry is done successfully";
if (isValid()) {
Class.forName("com.mysql.jdbc.Driver");
try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_packages","root","");) {
PreparedStatement st = conn.prepareStatement("insert into sales (salesId, nameSurname,adress,contract,speed,flow) values (?,?,?,?,?,?)");
st.setString(1, Integer.toString(id.get()));
st.setString(2, nameSurname.get());
st.setString(3, adress.get());
st.setString(4, Integer.toString(contract.get()));
st.setString(5, Integer.toString(speed.get()));
st.setString(6, flow.get().toString());
st.execute();
} catch (SQLException e) {
message = "ERROR! " + e.getMessage();
}
}
return message;
}
@Override
public String toString() {
return
"ID: " + id.get() + "\n" +
"Name and surname: " + nameSurname.get() + "\n" +
"Adress: " + adress.get() + "\n" +
"Contract: " + contract.get() + " god." + "\n" +
"Speed: " + speed.get() + " Mbit" +"\n" +
"Flow: " + flow.get() + " GB";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<BorderPane id="bp1" prefHeight="500.0" prefWidth="500.0" styleClass="mainFxmlClass"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.SalesController">
<stylesheets>
<URL value="@SalesStil.css"/>
</stylesheets>
<top>
<HBox fx:id="hbTit">
<Label fx:id="lbl1" text="Registration of sales:" />
</HBox>
</top>
<left>
<VBox fx:id="vbox1" spacing="20">
<BorderPane.margin>
<Insets top="25.0" />
</BorderPane.margin>
<children>
<HBox spacing="10" alignment="BASELINE_LEFT">
<children>
<Label fx:id="lbl2" text="Flow rate: " />
<ChoiceBox fx:id="cbSpeed" value="2" />
<Label text=" Mbit"/>
</children>
</HBox>
<HBox spacing="10" alignment="BASELINE_LEFT">
<children>
<Label fx:id="lbl3" text="Flow: " />
<ChoiceBox fx:id="cbFlow" />
<Label text=" GB" />
</children>
</HBox>
<HBox spacing="10" alignment="BASELINE_LEFT">
<children>
<Label fx:id="lbl4" text="Contract duration: " />
<ChoiceBox fx:id="cbContract"/>
<Label text=" god." />
</children>
</HBox>
<HBox spacing="10" alignment="BASELINE_LEFT">
<children>
<Label fx:id="lbl5" text="User name and last name: " />
<TextField fx:id="tfNameSurname"/>
</children>
</HBox>
<HBox spacing="10" alignment="BASELINE_LEFT">
<children>
<Label fx:id="lbl6" text="Address of the user: " />
<TextField fx:id="tfAdress"/>
</children>
</HBox>
<Button fx:id="prodajaBtn" text="Prodaj"
AnchorPane.leftAnchor="0.0" onAction="#prodaj"/>
<Label /> <Label /> <Label />
<AnchorPane fx:id="apDel">
<HBox fx:id="vbDel" alignment="BASELINE_CENTER" spacing="10"
AnchorPane.topAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0">
<Button fx:id="delBtn" text="Delete Selected Sales" onAction="#outlinesSell"/>
</HBox>
</AnchorPane>
</children>
</VBox>
</left>
<center>
<AnchorPane>
<BorderPane.margin>
<Insets bottom="0.0" left="25.0" top="25.0" right="0.0"/>
</BorderPane.margin>
<ListView fx:id="listView" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />`
</AnchorPane>
</center>
</BorderPane>
这是我得到的错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/Mike/Desktop/Online%20Shop/InternetServiceRegistration/build/classes/view/SalesView.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at jfxas1.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,8]
Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 17 more
Exception running application mainMethod.Main
Java Result: 1