0% found this document useful (0 votes)
62 views

Bai Tap Lap Trinh Java

The document describes how to create a database and table in SQL Server, insert sample data, and update records. It then provides code to design a Java GUI application using Swing components to display, add, search and update records in the SinhVien (Student) table. The application connects to the SQL Server database, loads data into a JTable, and implements button click handlers to perform CRUD operations on the student records.

Uploaded by

Love Holic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Bai Tap Lap Trinh Java

The document describes how to create a database and table in SQL Server, insert sample data, and update records. It then provides code to design a Java GUI application using Swing components to display, add, search and update records in the SinhVien (Student) table. The application connects to the SQL Server database, loads data into a JTable, and implements button click handlers to perform CRUD operations on the student records.

Uploaded by

Love Holic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dng SQLServer to Database nh sau:

create database QLSV


use QLSV
drop table sinhvien
go
create table SinhVien (
maSo varchar (10) primary key,
hoTen nvarchar(40),
tuoi int)
insert into SinhVien values('12345',N'Hong Lan',23)
insert into SinhVien values('54321',N'H Hng',24)
select * from SinhVien
update SinhVien
set hoTen=N'Hng Lan'
where maSo='22222'
select * from SinhVien where maSo='54321'

Thit k v hin thc form bn di.

package jdbcExercise;
import
import
import
import
import
import
import
import

java.awt.Font;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.sql.Connection;
java.sql.DriverManager;
java.sql.PreparedStatement;
java.sql.ResultSet;
java.sql.Statement;

import
import
import
import
import
import
import

javax.swing.Box;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JOptionPane;
javax.swing.JScrollPane;
javax.swing.JTable;

import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class
private
private
private
private
private
private
private
private
private
private
private

GiaoDien extends JFrame implements ActionListener{


JTextField tfMaso;
JTextField tfTuoi;
JTextField tfHoTen;
JButton bttThem,bttXoa;
JButton bttSua;
JButton bttXoaRong;
JButton bttThoat;
Connection con=null;
JButton bttTim;
DefaultTableModel dfModel;
JTable table;

public GiaoDien() {
setTitle("JDBC demo");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
taoGUI();
taoKetNoiCSDL();
}
private void taoKetNoiCSDL() {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=QLSV";
con=DriverManager.getConnection(url, "sa", "sapassword");
napDuLieuVaoBang();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Loi CSDL");
return;
}
}
private void napDuLieuVaoBang() {

while(table.getRowCount()>0)
dfModel.removeRow(0);
try{
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from sinhvien");
int i=0;
while(rst.next()){
i++;
String []dataRow={""+i,rst.getString("maso"),
rst.getString("hoten"),""+rst.getInt("tuoi")};
dfModel.addRow(dataRow);
}
}catch(Exception ex){
}
}
private void taoGUI() {
Box b, b01,b1,b2,b3,b31,b4;
add(b=Box.createVerticalBox());
b.add(b01=Box.createHorizontalBox());
b.add(b1=Box.createHorizontalBox());
b.add(b2=Box.createHorizontalBox());
b.add(b3=Box.createHorizontalBox());
b.add(b31=Box.createHorizontalBox());
b.add(b4=Box.createHorizontalBox());
JLabel lblMaSo, lblHoTen,lblTuoi,lblThongTin;
b01.add(lblThongTin=new JLabel("THNG TIN SINH VIN"));
lblThongTin.setFont(new Font("Arial", Font.BOLD, 25));
b1.add(lblMaSo=new JLabel("Ma so: "));b1.add(tfMaso=new JTextField());
b2.add(lblHoTen=new JLabel("Ho ten: "));b2.add(tfHoTen=new JTextField());
b3.add(lblTuoi=new JLabel("Tuoi: "));b3.add(tfTuoi=new JTextField());
lblMaSo.setPreferredSize(lblHoTen.getPreferredSize());
lblTuoi.setPreferredSize(lblHoTen.getPreferredSize());
String[] headers={"STT","M s", "h tn","Tui"};

b31.add(new JScrollPane(table=new JTable(dfModel=new DefaultTableModel(headers,0))));


b4.add(bttThem=new JButton("Them"));
b4.add(bttXoa=new JButton("Xoa"));
b4.add(bttSua=new JButton("Sua"));
b4.add(bttTim=new JButton("Tim"));
b4.add(bttXoaRong=new JButton("Xoa rong"));
b4.add(bttThoat=new JButton("Thoat"));
bttThem.addActionListener(this);
bttXoa.addActionListener(this);
bttSua.addActionListener(this);
bttXoaRong.addActionListener(this);
bttThoat.addActionListener(this);
bttTim.addActionListener(this);
}
public static void main(String[] args) {
new GiaoDien().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(bttXoaRong))
xoaRong();
if(e.getSource().equals(bttThem))
themDuLieu();
if(e.getSource().equals(bttTim))
timDuLieu();
}
private void timDuLieu() {
try{
String inputMaso=JOptionPane.showInputDialog("Nhap ma so sinh vien: ");
PreparedStatement stmt=con.prepareStatement("select * from Sinhvien where maso =
? ");
stmt.setString(1, inputMaso);
ResultSet rst=stmt.executeQuery();
if(rst.next())
{

tfMaso.setText(rst.getString(1));
tfHoTen.setText(rst.getString(2));
tfTuoi.setText(""+rst.getInt(3));
}
}catch(Exception ex){
}
}
private void themDuLieu() {
try{
PreparedStatement pstmt=
con.prepareStatement("Insert into SinhVien values (? , ?, ?)");
pstmt.setString(1, tfMaso.getText());
pstmt.setString(2, tfHoTen.getText());
pstmt.setInt(3, Integer.parseInt(tfTuoi.getText()));
int n=pstmt.executeUpdate();
if(n>0)
{
JOptionPane.showMessageDialog(null, "Them thanh cong.");
napDuLieuVaoBang();
}
}catch(Exception ex){
}
}
private void xoaRong() {
tfMaso.setText("");
tfHoTen.setText("");
tfTuoi.setText("");
tfMaso.requestFocus();
}
}

You might also like