Mavzu: Dasturiy ta’minotni loyihalash va dizayn jarayonini hamda uning ma’lumotlar bazasi qismini dasturiy ta’minot standartlariga ko’ra tayyorlash
Download 1.46 Mb.
|
651-17-7-Otajonov-Abduqahhor
public class Guruh {
private IntegerProperty t_nomer; private StringProperty nom; private StringProperty teacher; private StringProperty fan; private IntegerProperty id; Guruh modelining maydonlari yaratamiz public Guruh() { } Bo‘sh konastruktor yaratamiz public Guruh(int t_nomer, String nom, String teacher, String fan, int id) { this.t_nomer = new SimpleIntegerProperty(t_nomer); this.nom= new SimpleStringProperty(nom); this.teacher = new SimpleStringProperty(teacher); this.fan = new SimpleStringProperty(fan); this.id = new SimpleIntegerProperty(id); } Qiymatli konstruktorlar yaratamiz. public int getT_nomer() { return t_nomer.get(); } public IntegerProperty t_nomerProperty() { return t_nomer; } public void setT_nomer(int t_nomer) { this.t_nomer.set(t_nomer); } public String getNom() { return nom.get(); } public StringProperty nomProperty() { return nom; } public void setNom(String nom) { this.nom.set(nom); } public String getTeacher() { return teacher.get(); } public StringProperty teacherProperty() { return teacher; } public void setTeacher(String teacher) { this.teacher.set(teacher); } public String getFan() { return fan.get(); } public StringProperty fanProperty() { return fan; } public void setFan(String fan) { this.fan.set(fan); } public int getId() { return id.get(); } public IntegerProperty idProperty() { return id; } public void setId(int id) { this.id.set(id); } } Model maydonlarining getter, setterlarini yuqoridagi tartibda yaratamiz yaratamiz. Talaba nomli modelimiz quydagicha hosil qilinadi. public class Talaba { private IntegerProperty t_nomer; private StringProperty ism; private StringProperty fam; private StringProperty teacher; private StringProperty gur; private StringProperty nomer; private StringProperty fan; private IntegerProperty id; Talaba maydonlari beriladi. public Telefon() { } Shu klassga bo‘sh konstruktor yaratamiz. public Telefon(int t_nomer, String ism, String fam, String nomer, String fan,String gur, String teacher, int id) { this.t_nomer = new SimpleIntegerProperty(t_nomer); this.ism = new SimpleStringProperty(ism); this.fam = new SimpleStringProperty(fam); this.nomer = new SimpleStringProperty(nomer); this.fan = new SimpleStringProperty(fan); this.gur = new SimpleStringProperty(gur); this.teacher = new SimpleStringProperty(teacher); this.id = new SimpleIntegerProperty(id); } Keyin qiymatli konstruktor yaratamiz public String getFan() { return fan.get(); } public StringProperty fanProperty() { return fan; } public void setFan(String fan) { this.fan.set(fan); public int getT_nomer() { return t_nomer.get(); } public IntegerProperty t_nomerProperty() { return t_nomer; } public void setT_nomer(int t_nomer) { this.t_nomer.set(t_nomer); } public String getIsm() { return ism.get(); } public StringProperty ismProperty() { return ism; } public void setIsm(String ism) { this.ism.set(ism); } public String getFam() { return fam.get(); } public StringProperty famProperty() { return fam; } public void setFam(String fam) { this.fam.set(fam); } public String getTeacher() { return teacher.get(); } public StringProperty teacherProperty() { return teacher; } public void setTeacher(String teacher) { this.teacher.set(teacher); } public String getGur() { return gur.get(); } public StringProperty gurProperty() { return gur; } public void setGur(String gur) { this.gur.set(gur); } public String getNomer() { return nomer.get(); } public StringProperty nomerProperty() { return nomer; } public void setNomer(String nomer) { this.nomer.set(nomer); } public int getId() { return id.get(); } public IntegerProperty idProperty() { return id; } public void setId(int id) { this.id.set(id); } } Yana shu Modelning getter, setterlarini yuqoridagi tartibda yaratamiz. 2.2. O‘quv markaz faoliyatini avtomatlashtirish dasturiy ta’minotining ma’lumotlar bazasi bilan bog‘lash modellar asosida CRUD klasslarini yaratish5-7-bet Dasturimizni ma’lumotlar bazasi bilan bog‘lash uchun biz quydagi tartibda ishlar olib boramiz. public class DBConnection { Avvalo DBConnection nomli klass ochib olamiz Connection connection = null; String server; String baza; String port; String user; String parol; Bu klassda yuqoridagidagi maydonlar yaratamiz public DBConnection() { } Va bosh konstruktor yaratamiz. public Connection getConnection() { Ushbu nomdagi metod yaratamiz. File file = new File("baza.properties"); Bazamiz ma’lumotlarini saqlab qolish uchun bitta fayl ochamiz. Va shu faylga malumotlarni saqlab qolish uchun kiritib qoyamiz. if (file.exists()) { try { Properties properties = new Properties(); properties.load(new FileInputStream("baza.properties")); server = properties.getProperty("server"); port = properties.getProperty("port"); baza = properties.getProperty("baza"); user = properties.getProperty("user"); parol = properties.getProperty("password"); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + baza, user, parol); Ushbu qism biz uchun MB bilan bog‘laydi ya’ni server, PCning porti, MB nomi, MBning Foydalanuvchi useri va agar parol mavjud bo‘lsa parol kiritamiz. return connection; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (SQLException e) { e.printStackTrace(); return null; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } } else { return null; } }
try { if (connection!=null){ connection.close(); } if (statement!=null){ statement.close(); } if (resultSet!=null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } } Biz ushbu holatdan so‘ng Talabaning CRUD qismini ko‘rib chiqamiz. Avvalo dasturimizni jadvallar bilan bog‘lash uchun unga sorovlarni o‘qib oluvchi umumiy klass ochib olamiz 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 ObservableList 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); }
} Biz ushbu listbilan MB bilan Select qilib ma’lumotlar ba’zasi bilan bog‘lab olamiz. Bu holat TalabaController 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 ism,fam,nomer,fan,gur,teacher,id from tutorial.talaba"; list=dao.getMahsulotlar(sql); tableview.setItems(list); } Bu klassda sql degan string bilan bazani kerakli sorov beramiz. Va shu metodimizni initialize qismiga berib qo‘yamiz. Talaba 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(); } } Download 1.46 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling