0% found this document useful (0 votes)
2 views13 pages

Java 2

The document contains code snippets for a programming exam, focusing on Java GUI components and database operations. It includes examples of inserting data into tables, handling user inputs through text fields and combo boxes, and updating records in a database. The code demonstrates the use of PreparedStatement for executing SQL queries and managing data in a structured format.

Uploaded by

thắng
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)
2 views13 pages

Java 2

The document contains code snippets for a programming exam, focusing on Java GUI components and database operations. It includes examples of inserting data into tables, handling user inputs through text fields and combo boxes, and updating records in a database. The code demonstrates the use of PreparedStatement for executing SQL queries and managing data in a structured format.

Uploaded by

thắng
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/ 13

ĐỀ THI 2022-2023

Câu a:

jTextField txtTentour= new jTextField();

jTextField txtNgaykh= new jTextFiled();

jTextField txtsongay= new jTextField();

jTextFiled txtsodem= new jTextField();

jTextField txtGia= new jTextField();

jButton btnThem = new jButton (“Thêm”);

btnThem.addActionlistener( (e)->{

string sql= “insert into TOUR(tentour,ngaykh,songay,sodem,gia) VALUES (?, ?, ?, ?, ?)";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, txtTentour.getText());

stmt.setString(2, txtNgaykh.getText());

stmt.setInt(3, Integer.parseInt(txtsongay.getText()));

stmt.setInt(4, Integer.parseInt(txtsodem.getText()));

stmt.setDouble(5, Double.parseDouble(txtGia.getText()));

stmt.executeUpdate();

});

Câu b:

jCombobox cbbtinhtp = new jCombobox (new String[]{“TinhTP”});

jTextFiled txttendiem= new jTextField();

jTextField txtdactrung= new jTextField();

jButton btnThem = new jButton (“Thêm”);

ArrayList<TINHTP> ListTINHTP= new ArrayList<TINHTP>();

String sql= “ select MATTP,TENTTP from TINHTP “;

statement stmt= con.createStatement();

ResultSet rs= (ResultSet);

stmt.executeQuery(sql);

while (rs.next()) {
TINHTP tinh= new TINHTP();

tinh.MATTP=rs.getString(“MATTP”);

tinh.TenTTP=rs.getString(“TenTTP”);

ListTINHTP.add(tinh);

for( var x : ListTINHTP) cbbtinhtp.addItem(x.TenTTP);

btnThem.addActionlistener( (e)->{

String selectedMaT = ListTINHTP.get(cbbtinhtp.getSelectedIndex()).MATTP;

string sql= “insert into DIEMDL(TenDDL, MaTTP, DacTrung) VALUES (?, ?, ?)";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, txttendiem.getText());

stmt.setString(2, . selectedMaT);

stmt.setInt(3, txtdactrung.getText());

stmt.excuteUpdate();

});

c)

jTextField txtMatour=new jTextField();

jTextField txtTentour= new jTextField();

jTextField txtNgaykh= new jTextFiled();

jTextField txtsongay= new jTextField();

jTextFiled txtsodem= new jTextField();

jTextField txtGia= new jTextField();

jCombobox cbbtinhtp = new jCombobox (new String[]{“TinhTP”});

jButton btnThem = new jButton (“Thêm”);

--

DefaultTableModel tbModel1= new DefaultTableModel();

String tieude1[]={“Tên điểm du lịch”,”Đặc trưng”};


tbModel1.setColumnIdentifiers(tieude1);

Jtable table1 = new Jtable(tbModel1);

JScrollPane scrollPane1= new JscrollPane();

scrollPane1.setViewportView(table1);

DefaultTableModel tbModel2= new DefaultTableModel();

String tieude2[]={“Tên điểm du lịch”};

tbModel2.setColumnIdentifiers(tieude2);

Jtable table2= new Jtable(tbModel2);

JScrollPane scrollPane2= new JscrollPane();

scrollPane2.setViewportView(table2);

txtTentour.setEditable(false);

txtNgaykh.setEditable(false);

txtsongay.setEditable(false);

txtsodem.setEditable(false);

txtGia.setEditable(false);

--them du lieu vao combobox

ArrayList<TINHTP> ListTINHTP= new ArrayList<TINHTP>();

String sql= “ select MATTP,TENTTP from TINHTP “;

statement stmt= con.createStatement();

ResultSet rs= (ResultSet);

stmt.executeQuery(sql);

while (rs.next()) {

TINHTP tinh= new TINHTP();

tinh.MATTP=rs.getString(“MATTP”);

tinh.TenTTP=rs.getString(“TenTTP”);
ListTINHTP.add(tinh);

for( var x : ListTINHTP) cbbtinhtp.addItem(x.TenTTP);

ĐỀ THI 2021-2022
a)

String sqludyck = “UPDATE KHAMBENH SET KETLUAN = ketluanfield.getText()


WHERE MABS = mabs.get(bscombobox.getSelectedIndex() AND NGAYKHAM =
ngaykhamfield.getText() AND MABN =
mabn.get(bncombobox.getSelectedIndex()))”;
PreparenStatement prud = c.prepareStatement(sqlupyck);
Prud.excute();
b)

jTextField txtimei=new jTextField();

jTextField txttendt= new jTextField();

jTextField txthoten= new jTextFiled();

jTextField txtngaylap= new jTextField();

jTextFArea txtnoidung= new jTextArea();

jButton btnThem = new jButton (“Thêm”);

txttendt.setEditable(false);

txthoten.setEditable(false);

// sự kiện nhấn enter

txtimei.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_ENTER) {

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT TENDT, HOTENKH FROM DIENTHOAI WHERE IMEI = ?";

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, txtimei.getText());

ResultSet rs = statement.executeQuery();

while(rs.next()) {

txttendt.setText(rs.getString(1));

txthoten.setText(rs.getString(2));

databaseConnection.closeDatabaseConnection(conn);

});
// sự kiện nhấn thêm

btnThem.addActionlistener( (e)->{

string sql= “insert into SUACHUA(NGAYLAP,NOIDUNG,IMEI) VALUES (?, ?, ?)";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, txtngaylap.getText());

stmt.setString(2, txtnoidung. getText());

stmt.setString(3, txtimei.getText());

stmt.excuteUpdate();

});

C)

jTextField txtimei=new jTextField();

jTextField txttendt= new jTextField();

jTextField txthoten= new jTextFiled();

jTextField txtngaylap= new jTextField();

jTextFArea txtnoidung= new jTextArea();

jButton btnThem = new jButton (“Thêm sửa chữa”);

txttendt.setEditable(false);

txthoten.setEditable(false);

DefaultTableModel tbModel1= new DefaultTableModel();

String tieude1[]={“Mã công việc”,”công việc”};

tbModel1.setColumnIdentifiers(tieude1);

Jtable table1 = new Jtable(tbModel1);

JScrollPane scrollPane1= new JscrollPane();

scrollPane1.setViewportView(table1);

DefaultTableModel tbModel2= new DefaultTableModel();

String tieude2[]={“công việc”,”đơn giá” };


tbModel2.setColumnIdentifiers(tieude2);

Jtable table2= new Jtable(tbModel2);

JScrollPane scrollPane2= new JscrollPane();

scrollPane2.setViewportView(table2);

// sự kiện nhấn enter imei

txtimei.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_ENTER) {

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT TENDT, HOTENKH FROM DIENTHOAI WHERE IMEI = ?";

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, txtimei.getText());

ResultSet rs = statement.executeQuery();

while(rs.next()) {

txttendt.setText(rs.getString(1));

txthoten.setText(rs.getString(2));

databaseConnection.closeDatabaseConnection(conn);

});

// nhấn enter ngày

txtngaylap.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_ENTER) {

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT NOIDUNG FROM SUACHUA WHERE IMEI = ? AND NGAYLAP=?";

PreparedStatement statement = conn.prepareStatement(sql);


statement.setString(1, txtimei.getText());

statement.setString(2, txtngaylap.getText());

ResultSet rs = statement.executeQuery();

while(rs.next()) {

txtnoidung.setText(rs.getString(1));

databaseConnection.closeDatabaseConnection(conn);

});

// load data lên table 1

ArrayList<CONGVIEC> listcv = new ArrayList<CONGVIEC>();

Connection con = OracleJDBCconnection.getJDBCconnection();

String sql = "SELECT * FROM CONGVIEC";

PreparedStatement pst = con.prepareStatement(sql);

ResultSet rs = pst.executeQuery();

while (rs.next()) {

String MACV = rs.getString("MACV");

String TENCV= rs.getString("TENCV");

double DONGIA = rs.getDouble("DONGIA");

CONGVIEC cv = new CONGVIEC(MACV,TENCV,DONGIA);

listcv.add(cv);

tbModel1.setRowCount(0);

for (CONGVIEC i : listcv ) {

tbModel1.addRow(new Object[]{i.getMACV(), i.getTENCV()}

);
}

// load dữ liệu table2 khi bấm vào table 1

table1.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent event) {

int selectedRow = table1.getSelectedRow();

String macv= table1.getValueAt(selectedRow, 0);

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT TENCV,DONGIA FROM CONGVIEC WHERE MACV = ?";


PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, macv);

ResultSet rs = statement.executeQuery();

while(rs.next()) {

Object rowData [] = new Object[2];

rowData [0] = rs.getString(1);

rowData[1] = rs.getString(2);

tbModel2.addRow(rowData);

);

// Thêm

themBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

double tongtien=0;

String maCVList[] = new String[tbModel2.getRowCount()];

int cnt = 0;

for(int i = 0; i < tbModel2.getRowCount(); i++) {

String tencv = (String) tbModel2.getValueAt(i, 0);


Double dongia= (Double)tbModel2.getValueAt(i,1);

Tongtien=tongtien+dongia;

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT MACV FROM CONGVIEC WHERE TENCV = ? AND DONGIA=?";

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, tencv);

statement.setString(1, dongia);

ResultSet rs = statement.executeQuery();

while(rs.next()) {

maCVList[cnt] = rs.getString(1);

cnt++; }

databaseConnection.closeDatabaseConnection(conn);

String imei = txtimei.getText();

String mapsc;

Connection conn = databaseConnection.getDatabaseConnection();

String sql = "SELECT MAPSC FROM SUACHUA WHERE IMEI=?”;

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, imei);

ResultSet rs = statement.executeQuery();

while(rs.next()) {

mapsc = rs.getString("MAPSC");

databaseConnection.closeDatabaseConnection(conn);

for(int i = 0; i < maCVList.length; i++) {


Connection conn = databaseConnection.getDatabaseConnection();

String sql = "INSERT INTO CT_SC (MAPSC,MACV) VALUES (?, ?)";

PreparedStatement statement = conn.prepareStatement(sql);

statement.setString(1, mapsc);

statement.setString(2, maDDLList[i]);

statement.executeUpdate();

System.out.println("ok");

databaseConnection.closeDatabaseConnection(conn);

Connection conn = databaseConnection.getDatabaseConnection();

String sql = “UPDATE SUACHUA SET TONGTIEN = tongtien WHERE MAPSC


=”;

PreparedStatement statement = con.PreparedStatement (sql);


statement.executeUpdate();

} });

You might also like