我试图显示所有具有用户用来制作特定CNP的约会。

要求用户重新输入创建约会时使用的相同CNP,以查看使用该特定CNP进行的约会。

我想在查询中做什么,而不是“?”添加存储在字符串cnp中的CNP的值

附注:我在代码中添加了注释,以帮助大家理解。

代码:

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);

}
}


谢谢 !

将来链接到删除问题

最佳答案

您可以简单地串联字符串。

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...()方法。

关于java - 用数据库中的特定行填充tableview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50922139/

10-10 06:30