class Box{
public double length;
public double width;
public double height;
}
Konstruktor – bu klassning maxsus usuli bo‘lib, bu sinfning har bir yangi ob’ekti yaratilayotganda bajariladi. Konstruktor – bu usul bo‘lib, uning nomi klass nomi bilan ustma-ust tushadi. Konstruktor hech qanaqa qiymat qaytarmaydi. Standart konstruktorlar – bu parametrsiz konstruktorlardir. Klass yaratishda bir nechta konstruktorlardan foydalaniladi.
Klasslarda konstruktorlarni qayta yuklash mumkin. Bunda konstruktorlarning parametlarida farq bo‘ladi. Masalan,
2.1. Optik linza ishlab chiqaruvchi korxonani dasturiy ta’minotini ma’lumotlar bazasi bilan bog‘lash modellar asosida CRUD klasslarini yaratish
Dasturimizni yaratish jarayonida javvallarni yaratdik, modellarni hosil qildik endi mana shu jadvallarga ma’lumotlarni yozish, o‘qish, o‘zgartirish va o‘chirish uchun CRUD yasaymiz.
public class DataAccsesObject {
DBConnection database=new DBConnection();
private Connection connection=null;
private PreparedStatement statement=null;
private ResultSet resultSet=null;
public DataAccsesObject() {
}
//user aniqlash uchun
public int tekUser(String sql){
int id=0;
try {
connection=database.getConnection();
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
id=resultSet.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
database.close(connection,statement,resultSet);
}
return id;
}
public ObservableList getBemor(String sql){
ObservableList list= FXCollections.observableArrayList();
int index=0;
try {
connection=database.getConnection();
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
index++;
list.add(new Telefon(index,resultSet.getString(1),resultSet.getString(2),
resultSet.getString(3),resultSet.getString(4),resultSet.getString(5),
resultSet.getString(6),resultSet.getInt(7)));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
database.close(connection,statement,resultSet);
}
return list;
}
Biz ushbu listbilan MB bilan Select qilib ma’lumotlar ba’zasi bilan bog‘lab olamiz. Bu holat BemorController qismida quydagicha amalga oshiriladi
private void initTable(){
column_nomer.setCellValueFactory(cell->cell.getValue().t_nomerProperty().asObject());
column_ism.setCellValueFactory(cell->cell.getValue().ismProperty());
column_fam.setCellValueFactory(cell->cell.getValue().famProperty());
column_yili.setCellValueFactory(cell->cell.getValue().gurProperty());
column_vil.setCellValueFactory(cell->cell.getValue().teacherProperty());
column_tel.setCellValueFactory(cell->cell.getValue().nomerProperty());
column_fan.setCellValueFactory(cell->cell.getValue().fanProperty());
column_id.setCellValueFactory(cell->cell.getValue().idProperty().asObject());
}
Ya’ni jadvaldagi ustunlarni tuzgan modelimiz bilan bog‘laymiz. Bundan so‘ng
private void yangilashTable(){
initTable();
sql="select * from bemor";
list=dao.getbemor(sql);
tableview.setItems(list);
}
Bu klassda sql degan string bilan bazani kerakli sorov beramiz. Va shu metodimizni initialize qismiga berib qo'yamiz.
Bemor ma’lumotini o‘chirish uchun quydagi kodlardan foydalanamiz.
void handle_ochirish(ActionEvent event) {
if (tableview.getSelectionModel().getSelectedIndex()!=-1){
Telefon telefon=tableview.getSelectionModel().getSelectedItem();
sql="delete from tutorial.talaba where id='"+telefon.getId()+"'";
dao.UmumiySorov(sql);
yangilashTable();
}
}
Do'stlaringiz bilan baham: |