Sql
Sql
Sql
package javanangcao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class bai15_2 extends JFrame{
// Table model
DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
tableModel.addColumn("MSSV");
tableModel.addColumn("HoTen");
tableModel.addColumn("NamSinh");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/qlhv";
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM qlhv");
while(rs.next())
{
String mssv = rs.getString("mssv");
String hoten = rs.getString("hoten");
String namsinh = rs.getString("namsinh");
tableModel.addRow(new Object[]{mssv, hoten, namsinh});
}
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
//System.out.println("Kết nối thành công");
} catch (Exception e) {
System.out.println("Kết nối thất bại: " + e.getMessage());
}
frame.setVisible(true);
}
Sqlsever
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
Vd2:
package javanangcao;
import java.sql.*;
public class bai15{
protected static Connection con;
public static void main(String args[]){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url
="jdbc:sqlserver://localhost:1433;databaseName=qlsv;encrypt=true;trustServerCertificate=true";
String username = "long"; String password= "123456789";
con=DriverManager.getConnection(url, username, password);
Statement stmt =con.createStatement();
ResultSet rs =stmt.executeQuery("SELECT * FROM sinhvien");
while(rs.next()) {
System.out.println(rs.getString("mssv")+"\t");
System.out.println(rs.getString("hoten")+"\t");
System.out.println(rs.getString("tuoi")+"\t");
System.out.println();
}
if(rs !=null) rs.close();
if(stmt !=null)stmt.close();
if(con !=null)con.close();
System.out.println("ket noi thanh cong");
} catch(Exception loi){
System.out.println("loi"+loi.getMessage());
loi.printStackTrace();
}
}}
package chuong4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(panel);
panel.add(lbmssv); panel.add(jmssv);
panel.add(lbhoten); panel.add(jhoten);
panel.add(lbbamsinh); panel.add(jnamsinh);
panel.add(btthem); panel.add(btsua);
panel.add(btxoa);
frame.setVisible(true);
btthem.addActionListener(new ActionListener() {
@Override
String mssv=jmssv.getText();
String hoten=jhoten.getText();
int namsinh=Integer.parseInt(jnamsinh.getText());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=QLSV;encrypt=true;trustServerCertificate=true";
them.setString(1, mssv);
them.setString(2,hoten);
them.setInt(3, namsinh);
them.executeUpdate();
con.close();
System.out.println("Lỗi: "+loi.getMessage());
}
}
});
btsua.addActionListener(new ActionListener() {
@Override
String mssv=jmssv.getText();
String hoten=jhoten.getText();
int namsinh=Integer.parseInt(jnamsinh.getText());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=QLSV;encrypt=true;trustServerCertificate=true";
sua.executeUpdate();
con.close();
System.out.println("Lỗi: "+loi.getMessage());
});
btxoa.addActionListener(new ActionListener() {
@Override
String mssv=jmssv.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=QLSV;encrypt=true;trustServerCertificate=true";
Statement stmt=con.createStatement();
xoa.executeUpdate();
con.close();
System.out.println("Lỗi: "+loi.getMessage());
});
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.*;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=qlbanhang;encrypt=true;trustServerCertificate=true";
String username = "long";
String password = "123456789";
con = DriverManager.getConnection(url, username, password);
panel.add(inputPanel, BorderLayout.NORTH);
}
//su kien them
private class ButtonaddListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
String maSP = txtMaSP.getText();
String tenSP = txtTenSP.getText();
String gia = txtGia.getText();
String maloaiSP = txtMaLoaiSP.getText();
String lenh= "INSERT INTO sanpham (masp, tensp, gia, maloaisp)
VALUES('"+maSP+"','"
+tenSP+"',"+gia+",'"+maloaiSP+"')";
Statement stmt=con.createStatement();
stmt.execute(lenh);
JOptionPane.showMessageDialog(null, "Thêm sản phẩm thành công!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Lỗi khi thêm sản phẩm: " +
ex.getMessage());
}
}
}
//su kien xoa
private class ButtondeleteListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
String maSP = txtMaSP.getText();
Statement stmt=con.createStatement();
stmt.execute(lenh);
JOptionPane.showMessageDialog(null, "sua sản phẩm thành công!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Lỗi khi sua sản phẩm: " + ex.getMessage());
}
}
}
//tim
private class SearchButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
String maSP = txtMaSP.getText();
String lenh = "SELECT * FROM sanpham WHERE masp='" + maSP + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(lenh);
if (rs.next()) {
txtTenSP.setText(rs.getString("tensp"));
txtGia.setText(rs.getString("gia"));
txtMaLoaiSP.setText(rs.getString("maloaisp"));
JOptionPane.showMessageDialog(null, "Tìm thấy sản phẩm!");
} else {
JOptionPane.showMessageDialog(null, "Không tìm thấy sản phẩm với mã SP: " +
maSP);
}
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Lỗi khi tìm sản phẩm: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new bai16().setVisible(true);
}
});
}
package Nguyen_Hoang_Long_221A010124;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public bai1() {
setTitle("Đặt Vé Xe Bus");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=qlbuss;encrypt=true;trustServerCertificate=true";
String username = "long";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
System.out.println("Kết nối thành công");
} catch (Exception loi) {
System.out.println("Lỗi " + loi.getMessage());
loi.printStackTrace();
}
btnDatVe.addActionListener(new DatVeButtonListener());
btnHuyDatVe.addActionListener(new HuyDatVeButtonListener());
setVisible(true);
}
String lenh = "INSERT INTO DatVe (makh, matuyen, ngaydatve) VALUES (?, ?, ?)";
PreparedStatement stmtDatVe = con.prepareStatement(lenh);
stmtDatVe.setInt(1, makh);
stmtDatVe.setInt(2, matuyen);
stmtDatVe.setString(3, java.time.LocalDate.now().toString());
stmtDatVe.executeUpdate();
JOptionPane.showMessageDialog(null, "Đặt vé thành công!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Lỗi khi đặt vé: " + ex.getMessage());
}
}
}
package Nguyen_Hoang_Long_221A010124;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.DefaultTableModel;
tableModel = new DefaultTableModel(new String[]{"Mã tuyến", "Điểm bắt đầu", "Điểm kết
thúc", "Giá"}, 0);
table = new JTable(tableModel);
add(new JScrollPane(table), BorderLayout.CENTER);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url =
"jdbc:sqlserver://localhost:1433;databaseName=qlbuss;encrypt=true;trustServerCertificate=true";
String username = "long";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
System.out.println("Kết nối thành công");
} catch (Exception loi) {
System.out.println("Lỗi " + loi.getMessage());
loi.printStackTrace();
}
btntimkiem.addActionListener(new TimKiemButtonListener());
setVisible(true);
}
private class TimKiemButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
try {
String maTuyen = txttuyenduong.getText();
String sql = "SELECT * FROM TuyenDuong WHERE matuyen = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, maTuyen);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String matuyen = rs.getString("matuyen");
String diembatdau = rs.getString("diemdi");
String diemketthuc = rs.getString("diemden");
int giave = rs.getInt("giave");
tableModel.addRow(new Object[]{matuyen, diembatdau, diemketthuc, giave});
}
}catch (Exception loi) {
System.out.println("Lỗi " + loi.getMessage());
loi.printStackTrace();
}
File:
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class bai10 extends JFrame {
private JTextArea textarea;
private JButton openbutton;
public bai10() {
setTitle("doc file");
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textarea=new JTextArea();
JScrollPane scorllpane =new JScrollPane(textarea);
openbutton =new JButton("mo file");
openbutton.addActionListener(new Openbuttonlisterner());
add(scorllpane, BorderLayout.CENTER);
add(openbutton, BorderLayout.SOUTH);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new bai10().setVisible(true);
}
});
}
}
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class bai11 extends JFrame {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = textarea.getText();
}
});
frame.getContentPane().add(scoellpane, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
public ClickCounter() {
// Tạo frame
JFrame frame = new JFrame("Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
// Tạo panel
JPanel panel = new JPanel();
package javanangcao;
import javax.swing.*;
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class tinhtoancoban extends JFrame{
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
public tinhtoancoban() {
createView();
setTitle("Máy tính đơn giản");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 300);
setLocationRelativeTo(null);
setResizable(false);
}
private void createView() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(new GridLayout(5, 2, 10, 10));
JLabel label1 = new JLabel("Số thứ nhất:");
panel.add(label1);
textField1 = new JTextField();
panel.add(textField1);
JLabel label2 = new JLabel("Số thứ hai:");
panel.add(label2);
textField2 = new JTextField();
panel.add(textField2);
JLabel label3 = new JLabel("Kết quả: ");
panel.add(label3);
textField3 = new JTextField();
panel.add(textField3);
JButton addButton = new JButton("Cộng");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performOperation("+");
}
});
panel.add(addButton);
JButton subtractButton = new JButton("Trừ");
subtractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performOperation("-");
}
});
panel.add(subtractButton);
JButton multiplyButton = new JButton("Nhân");
multiplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performOperation("*");
}
});
panel.add(multiplyButton);
JButton divideButton = new JButton("Chia");
divideButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performOperation("/");
}
});
panel.add(divideButton);
}
private void performOperation(String operation) {
try {
double num1 = Double.parseDouble(textField1.getText());
double num2 = Double.parseDouble(textField2.getText());
double result = 0;
switch (operation) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
JOptionPane.showMessageDialog(this, "Không thể chia cho 0!", "Lỗi",
JOptionPane.ERROR_MESSAGE);
return;
}
result = num1 / num2;
break;
}
textField3.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Vui lòng nhập số hợp lệ!", "Lỗi",
JOptionPane.ERROR_MESSAGE);
}
}
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class bai3 extends JFrame{
private JComboBox<String>corlorcombobox;
private JPanel mainpanel;
public bai3() {
setTitle("doi mau");
setSize(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
corlorcombobox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeBackgroundcolor((String) corlorcombobox.getSelectedItem());
}
});
mainpanel.add(corlorcombobox, BorderLayout.NORTH);
setVisible(true);
}
private void changeBackgroundcolor(String color) {
switch (color) {
case "white:":
mainpanel.setBackground(Color.white);
break;
case "red":
mainpanel.setBackground(Color.red);
break;
case "grenn":
mainpanel.setBackground(Color.green);
break;
case "blue":
mainpanel.setBackground(Color.BLUE);
break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
}
});
}
}
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class bai4 extends JFrame{
public bai4() {
setTitle("Animal Selector");
setSize(600, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create components
catButton = new JRadioButton("Cat");
dogButton = new JRadioButton("Dog");
bigButton = new JRadioButton("big");
fishButton = new JRadioButton("fish");
rabbitButton = new JRadioButton("rabbit");
@Override
public void actionPerformed(ActionEvent e) {
updateImage("Cat");
}
});
dogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateImage("Dog");
}
});
bigButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateImage("big");
}
});
fishButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateImage("fish");
}
});
rabbitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateImage("rabbit");
}
});
// Layout setup
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3, 1));
radioPanel.add(catButton);
radioPanel.add(dogButton);
radioPanel.add(bigButton);
radioPanel.add(fishButton);
radioPanel.add(rabbitButton);
setLayout(new BorderLayout());
add(radioPanel, BorderLayout.WEST);
add(imageLabel, BorderLayout.NORTH);
case "fish":
imagePath = "C:\\Users\\Administrator\\Desktop\\fish.jpg";
break;
case "rabbit":
imagePath = "C:\\Users\\Administrator\\Desktop\\rabbit.jpg";
break;
}
ImageIcon imageIcon = new ImageIcon(imagePath);
imageLabel.setIcon(imageIcon);
}
package javanangcao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class bai5 extends JFrame {
private JCheckBox pizza;
private JCheckBox burger;
private JCheckBox tea;
private JCheckBox coffe;
private JButton orderbutton;
public bai5() {
setTitle("food ordering");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pizza=new JCheckBox("pizza(25$)");
burger=new JCheckBox("burger(30$)");
tea=new JCheckBox("tea(5$)");
coffe=new JCheckBox("coffe(7$)");
orderbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder message =new StringBuilder();
int total =0;
if(pizza.isSelected()) {
message.append("pizaa:$25\n");
total +=25;
}
if(burger.isSelected()) {
message.append("burger:$30\n");
total +=30;
}
if(tea.isSelected()) {
message.append("tea:$5\n");
total +=5;
}
if(coffe.isSelected()) {
message.append("coffe:$7\n");
total +=7;
}
message.append("---------------------\n");
message.append("total: $"+total);
JOptionPane.showMessageDialog(bai5.this,message.toString(),"message",JOptionPane.INFORMATION_
MESSAGE);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new bai5().setVisible(true);
}
});
}
}
package javanangcao;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class bai6 extends JFrame{
private JTable table;
private DefaultTableModel tablemodel;
public bai6() {
setTitle("phonebook");
setSize(400,300);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
addbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(bai6.this,"phone canot
empty" ,"error",JOptionPane.ERROR_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(bai6.this,"name canot
empty" ,"error",JOptionPane.ERROR_MESSAGE );
}
}
});
deletebutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedrow = table.getSelectedRow();
if(selectedrow !=-1) {
tablemodel.removeRow(selectedrow);
}else {
JOptionPane.showMessageDialog(bai6.this,"no contact
selected.","error",JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new bai6().setVisible(true);
}
});
}
}
package javanangcao;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public bai7() {
// Thiết lập tiêu đề cho JFrame
setTitle("Giải Phương Trình Bậc 2");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
btnXoaTrang.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
}
});
btnThoat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
public bai8() {
setTitle("account");
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
btnadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addaccount();
}
});
btnclear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearfields();
}
});
btnexit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
}
@Override
public void run() {
new bai8();
}
});
}