0% found this document useful (0 votes)
11 views5 pages

Sinhviendao - Java Form - Java

The document contains code for a Java application that manages student data using Hibernate and Swing. It includes DAO, Service, and GUI classes to retrieve, add, update and delete student objects from a database. The DAO class interacts with Hibernate to save, update and delete students. The Service class uses the DAO for data access. The GUI displays students in a JTable and allows CRUD operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Sinhviendao - Java Form - Java

The document contains code for a Java application that manages student data using Hibernate and Swing. It includes DAO, Service, and GUI classes to retrieve, add, update and delete student objects from a database. The DAO class interacts with Hibernate to save, update and delete students. The Service class uses the DAO for data access. The GUI displays students in a JTable and allows CRUD operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SinhVienDao.java Form.

java
/* package gui;
* To change this license header,
choose License Headers in Project import Domain.SinhVien;
Properties. import java.util.List;
* To change this template file, choose import
Tools | Templates javax.swing.table.DefaultTableModel;
* and open the template in the editor. import service.SinhVienService;
*/ import util.NewHibernateUtil;
package dao;
public class form extends
import Domain.SinhVien; javax.swing.JFrame {
import java.util.List; private SinhVienService svs = new
import org.hibernate.Session; SinhVienService(NewHibernateUtil.openS
ession());
/** private DefaultTableModel model;
*
* @author thuyt public form() {
*/ initComponents();
public class SinhVienDao { model = (DefaultTableModel)
private Session session; jTable1.getModel();
loadData();
public SinhVienDao(Session session) }
{ void loadData(){
this.session = session; hienthi(svs.getAll());
} }
void hienthi(List<SinhVien> list){
public List<SinhVien> getAll(){ model.setRowCount(0);
return session.createQuery("from for(SinhVien sv:list ){
SinhVien").list(); Object[] row =
} {sv.getMssv(),sv.getHoten(),sv.getDtb()};
model.addRow(row);
public void saveOrUpdate(SinhVien }
sv){ }
session.saveOrUpdate(sv); private void clear(){
} txtMa.setText("");
public void delete(SinhVien sv){ txtTen.setText("");
session.delete(sv); txtDTB.setText("");
} }
public SinhVien findById(int mssv){ private void
return (SinhVien) jTable1MouseClicked(java.awt.event.Mou
session.get(SinhVien.class,mssv); seEvent evt) {
} // TODO add your handling code
here:
}
int row = jTable1.getSelectedRow();
SinhVienService.java if (row == -1) return;
package service; int mssv = (int)
jTable1.getValueAt(row, 0);
import Domain.SinhVien; SinhVien sv = svs.findById(mssv);
import dao.SinhVienDao;
import java.util.List; txtMa.setText(jTable1.getValueAt(row,
import org.hibernate.Session; 0)+"");
import org.hibernate.Transaction;
txtTen.setText(jTable1.getValueAt(row,
/** 1)+"");
*
txtDTB.setText(jTable1.getValueAt(row,
* @author thuyt
2)+"");
*/
}
public class SinhVienService {
private void
private Session session;
btnThemActionPerformed(java.awt.event.
private SinhVienDao svd;
ActionEvent evt) {
// TODO add your handling code
public SinhVienService(Session here:
session) { int mssv =
this.session = session; Integer.parseInt(txtMa.getText());
svd = new SinhVienDao(session); double dtb =
} Double.parseDouble(txtDTB.getText());
SinhVien sv = new
public List<SinhVien> getAll(){ SinhVien(mssv,txtTen.getText(),dtb);
Transaction tx = svs.saveOrUpdate(sv);
session.beginTransaction(); loadData();
}
List<SinhVien> dssv = svd.getAll();
tx.commit();
private void
return dssv;
btnXoaActionPerformed(java.awt.event.A
}
ctionEvent evt) {
public void saveOrUpdate(SinhVien // TODO add your handling code
sv){ here:
Transaction tx = int row = jTable1.getSelectedRow();
session.beginTransaction(); if(row==-1) return;
svd.saveOrUpdate(sv); int mssv = (int)
tx.commit(); jTable1.getValueAt(row, 0);
} svs.delete(mssv);
public SinhVien findById(int mssv){ loadData();
Transaction tx = }
session.beginTransaction();
SinhVien sv = svd.findById(mssv); private void
tx.commit(); btnSuaActionPerformed(java.awt.event.A
return sv; ctionEvent evt) {
} // TODO add your handling code
public void delete(int mssv){ here:
Transaction tx = int row = jTable1.getSelectedRow();
session.beginTransaction(); if(row == -1)return;
SinhVien sv = svd.findById(mssv); int mssv = (int)
if(sv!=null) jTable1.getValueAt(row, 0);
svd.delete(sv); SinhVien sv = svs.findById(mssv);
tx.commit(); sv.setHoten(txtTen.getText());
}
sv.setDtb(Double.parseDouble(txtDTB.get
} Text()));
NewHibernateUtil svs.saveOrUpdate(sv);
package util;
loadData();
clear();
import org.hibernate.Session;
}
import
org.hibernate.cfg.AnnotationConfigura
tion;
import org.hibernate.SessionFactory;

/**
* Hibernate Utility class with a
convenient method to get Session
Factory
* object.
*
* @author thuyt
*/
public class NewHibernateUtil {

private static final SessionFactory


sessionFactory;

static {
try {
// Create the SessionFactory
from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new
AnnotationConfiguration().configure().
buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial
SessionFactory creation failed." + ex);
throw new
ExceptionInInitializerError(ex);
}
}

public static SessionFactory


getSessionFactory() {
return sessionFactory;
}
public static Session openSession() {
return
sessionFactory.openSession();
}
public static void close() {
sessionFactory.close();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy