使用db中的特定行填充tableview

时间:2018-06-19 06:44:53

标签: java mysql sql javafx

我正在尝试显示用户已经习惯使用特定CNP的所有约会。

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

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

P.S我在代码中添加了评论,以帮助你们理解。

代码:

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

}
}

谢谢!

删除问题的未来链接

1 个答案:

答案 0 :(得分:1)

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

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