java—用db中的特定行填充tableview

0mkxixxg  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(323)

我正在尝试显示所有具有特定cnp的约会,用户使用该cnp进行预约。
用户被要求重新输入他在创建预约时使用的相同cnp,以便查看使用该特定cnp进行的预约。
我想做的是,在查询时,而不是用“?”来添加cnp的值,该值存储在字符串cnp中
p、 我在代码中添加了注解来帮助你们理解。
代码:

package LicentaApp;

public class ProgramariDBController implements Initializable {

@FXML
private TextField cpn; // The user is asked to reenter the same CNP that he did use when he created the appointment
@FXML
private TableView<DetaliiProgramari> ProgrDB;
@FXML
private TableColumn<DetaliiProgramari, String> Nume;
@FXML
private TableColumn<DetaliiProgramari, String> Prenume;
@FXML
private TableColumn<DetaliiProgramari, String> Data;
@FXML
private TableColumn<DetaliiProgramari, String> Ora;
@FXML
private TableColumn<DetaliiProgramari, String> Departament;
@FXML
private TableColumn<DetaliiProgramari, String> Doctor;
@FXML
private TableColumn<DetaliiProgramari, String> Nr_telefon;

public LogareController Numeutilzator = new LogareController();
private ObservableList<DetaliiProgramari> Info;

@Override
public void initialize(URL url, ResourceBundle ResurcesFORDAYS) {
    // TODO

}

@FXML
private void AfiseazaProgramari(ActionEvent event) {
    try {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();

        Info = FXCollections.observableArrayList();
        String CNP=cpn.getText(); // I though making it a string will help, guess it didn't
        ResultSet IncDate = conectare.createStatement().executeQuery("SELECT *  FROM programari where CNP=?"); // What I want to do, instead of the "?" to add the value of the CNP, that is stored in String cnp
        while (IncDate.next()) {

            Info.add(new DetaliiProgramari(IncDate.getString(2), IncDate.getString(3), IncDate.getString(4), IncDate.getString(5), IncDate.getString(6), IncDate.getString(7), IncDate.getString(8)));
        }

    } catch (SQLException ex) {
        System.err.println("Error"+ex);
    }

    Nume.setCellValueFactory(new PropertyValueFactory<>("Nume"));
    Prenume.setCellValueFactory(new PropertyValueFactory<>("Prenume"));
    Data.setCellValueFactory(new PropertyValueFactory<>("Data"));
    Ora.setCellValueFactory(new PropertyValueFactory<>("Ora"));
    Departament.setCellValueFactory(new PropertyValueFactory<>("Departament"));
    Doctor.setCellValueFactory(new PropertyValueFactory<>("Doctor"));
    Nr_telefon.setCellValueFactory(new PropertyValueFactory<>("Nr_telefon"));

    ProgrDB.setItems(null);
    ProgrDB.setItems(Info);

}
}

谢谢您!
删除问题的未来链接

webghufk

webghufk1#

您可以简单地连接字符串。

ResultSet IncDate = conectare.createStatement().executeQuery(
    "SELECT *  FROM programari where CNP=" + cnp);

或者,使用准备好的语句。

String sql = "SELECT *  FROM programari where CNP=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, cnp);
ResultSet result = ps.executeQuery();

编辑

在使用这两种方法时,需要注意列的数据类型。如果 CNP 是字符串(varchar)列,应该包含引号。

"SELECT *  FROM programari where CNP='" + cnp + "'"

此外,您可以使用不同的 set...() 方法(如果使用第二种方法)。

相关问题