Return-table
package Testv1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class Retrun_table extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Retrun_table frame = new Retrun_table();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Retrun_table() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 720, 367);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 684, 290);
contentPane.add(panel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 46, 300, 150);
panel.add(scrollPane);
String []Name = {"����֤��", "����", "���", "����","����ʱ��","����ʱ��"};
Object [][] rowData = new Object [100][6];
JTable table = new JTable(rowData, Name);
table.setBounds(297, 179, -279, -124);
table.setRowHeight(30); //�����и�
table.getColumnModel().getColumn(0).setPreferredWidth(50); //��һ���п�
table.setPreferredScrollableViewportSize(new Dimension(650 ,300)); //���ù�������ӿڴ�С�������ô�С����������Ҫ�϶���������
scrollPane.setViewportView(table);
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select * from ���� order by ����ʱ�� asc";
ResultSet rs = state.executeQuery(sql0);
int i = 0;
while(rs.next() && i<rowData.length) {
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2] = rs.getString(3);
rowData[i][3] = rs.getString(4);
rowData[i][4] = rs.getString(5);
rowData[i][5] = rs.getString(6);
i++;
}
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JButton btnNewButton = new JButton("\u8FD4\u56DE");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
btnNewButton.setBounds(431, 305, 93, 23);
contentPane.add(btnNewButton);
}
}
//这段代码是一个用于展示数据库中数据的界面。以下是代码的基本结构和功能说明:
//
//Retrun_table 类继承自 JFrame,是一个窗口界面。
//main 方法启动了应用程序,并创建了 Retrun_table 的实例并设置为可见。
//构造函数 Retrun_table() 初始化了窗口的各个组件,并设置了布局、大小和位置等属性。
//创建了一个面板(JPanel)和一个滚动面板(JScrollPane),用于放置表格。
//rowData 是一个二维数组,用于存储从数据库中查询到的数据。
//创建了一个 JTable 表格,并使用查询结果填充表格的数据。
//设置表格的行高、列宽和可见视口的大小等属性。
//使用 JScrollPane 包装表格,以便在需要滚动时进行滚动操作。
//使用 JDBC 连接数据库,并执行查询语句,将查询结果填充到 rowData 中。
//关闭数据库连接。
//创建了一个返回按钮(btnNewButton),点击该按钮会关闭当前窗口。
//总体来说,这段代码是一个简单的展示数据库数据的界面,
// 通过使用 JDBC 连接数据库,并使用 JTable 展示数据。用户可以点击返回按钮来关闭窗口。
ReturnBook
package Testv1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class ReturnBook extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ReturnBook frame = new ReturnBook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ReturnBook() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 543, 428);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u4E66\u53F7");
lblNewLabel.setBounds(10, 306, 54, 15);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(54, 303, 107, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u64CD\u4F5C\u6210\u529F");
lblNewLabel_1.setBounds(107, 360, 54, 15);
contentPane.add(lblNewLabel_1);
lblNewLabel_1.setVisible(false);
JButton btnNewButton_1 = new JButton("\u8FD4\u56DE");
btnNewButton_1.addActionListener(new ActionListener() {//����
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
btnNewButton_1.setBounds(408, 356, 93, 23);
contentPane.add(btnNewButton_1);
JLabel lblNewLabel_2 = new JLabel("\u64CD\u4F5C\u5931\u8D25");
lblNewLabel_2.setForeground(Color.RED);
lblNewLabel_2.setBounds(34, 331, 54, 15);
contentPane.add(lblNewLabel_2);
lblNewLabel_2.setVisible(false);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 507, 286);
contentPane.add(panel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 46, 300, 150);
panel.add(scrollPane);
String []Name = {"���", "����", "���ʱ��"};
Object [][] rowData = new Object [100][3];
JTable table = new JTable(rowData, Name);
table.setBounds(297, 179, -279, -124);
table.setRowHeight(30); //�����и�
table.getColumnModel().getColumn(0).setPreferredWidth(110); //��һ���п�
table.setPreferredScrollableViewportSize(new Dimension(480 ,300)); //���ù�������ӿڴ�С�������ô�С����������Ҫ�϶���������
scrollPane.setViewportView(table);
String user = null;
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select top 1* from �����˺� order by ʱ�� desc";
ResultSet rs = state.executeQuery(sql0); //���һ��
String s1 = new String();
while(rs.next()) {
s1 = rs.getString(1);
}
user = s1;//�˺�
Statement state_1 = dbConn.createStatement();
String sql1 = "select * from ���� where ����֤�� = '"+user+"' order by ����ʱ�� asc";
ResultSet rs_1 = state_1.executeQuery(sql1);
int i = 0;
while(rs_1.next() && i<rowData.length) {
rowData[i][0] = rs_1.getString("���");
rowData[i][1] = rs_1.getString("����");
rowData[i][2] = rs_1.getString("����ʱ��");
i++;
}
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel lblNewLabel_3 = new JLabel("\u5DF2\u8D85\u65F6\u8FD8\u4E66\uFF0C\u8BF7\u5230\u524D\u53F0\u7F34\u6EDE\u7EB3\u91D1");
lblNewLabel_3.setForeground(Color.RED);
lblNewLabel_3.setBounds(182, 331, 184, 15);
contentPane.add(lblNewLabel_3);
lblNewLabel_3.setVisible(false);
JButton btnNewButton = new JButton("\u8FD8\u4E66");
btnNewButton.addActionListener(new ActionListener() {//ȷ�ϻ���
public void actionPerformed(ActionEvent e) {
String Bnum = new String(textField.getText());
Calendar c = Calendar.getInstance();
String tm = null;
long result = 0;
try {//��ȡʱ�䣬�ж��Ƿ�ʱ
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
Statement state_1 = dbConn.createStatement();
String sql1 = "select ����ʱ�� from ���� where ��� = '"+Bnum+"'";
ResultSet rs_1 = state_1.executeQuery(sql1);
while(rs_1.next()) {
tm = rs_1.getString(1); //ʱ���
}
Calendar ct = Calendar.getInstance();
Timestamp ts = Timestamp.valueOf(tm);
ct.setTime(ts);
result = (c.getTimeInMillis() - ct.getTimeInMillis())/(1000*60*60*24);
//ʱ���
}catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_2.setVisible(true);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_2.setVisible(true);
}
if(result>30) {//����30��
lblNewLabel_3.setVisible(true);
lblNewLabel_2.setVisible(true);
lblNewLabel_1.setVisible(false);
}
else {
lblNewLabel_3.setVisible(false);
lblNewLabel_2.setVisible(false);
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
PreparedStatement pst1 = null;
pst1 = dbConn.prepareStatement("update ͼ�� set ״̬=? where ���=?");//��
pst1.setString(1, "�����ڲ�");
pst1.setString(2, Bnum);
pst1.addBatch();
pst1.executeBatch();
String Unum = null;
Statement state_1 = dbConn.createStatement();
String sql1 = "select * from ���� where ���='"+Bnum+"'";
ResultSet rs1 = state_1.executeQuery(sql1);
String s1 = new String();
while(rs1.next()) {
s1 = rs1.getString("����֤��");
}
Unum = s1;
String name = null;
Statement state_2 = dbConn.createStatement();
String sql2 = "select * from ���� where ���='"+Bnum+"'";
ResultSet rs2 = state_2.executeQuery(sql2);
String s2 = new String();
while(rs2.next()) {
s2 = rs2.getString("����");
}
name = s2;
String Bname = null;
Statement state_3 = dbConn.createStatement();
String sql3 = "select * from ���� where ���='"+Bnum+"'";
ResultSet rs3 = state_3.executeQuery(sql3);
while(rs3.next()) {
Bname = rs3.getString("����");
}
PreparedStatement pst2 = null; //���뻹���
pst2 = dbConn.prepareStatement("insert into ���� values(?,?,?,?,?,?)");//����֤�ţ���������ţ�����������ʱ�䣬����ʱ��
pst2.setString(1, Unum);
pst2.setString(2, name);
pst2.setString(3, Bnum);
pst2.setString(4, Bname);
pst2.setString(5, tm);
pst2.setTimestamp(6, new Timestamp(c.getTimeInMillis()));
pst2.addBatch();
pst2.executeBatch();
String sql0 = "delete from ���� where ���= ?";
PreparedStatement pst = dbConn.prepareStatement(sql0);//ɾ��
pst.setString(1, Bnum);
pst.addBatch();
pst.executeBatch();
Statement state_1_1 = dbConn.createStatement();
String sql1_1 = "select * from ���� where ����֤�� = '"+Unum+"' order by ����ʱ�� asc";
ResultSet rs_1 = state_1_1.executeQuery(sql1_1);
int i = 0;
while(rs_1.next() && i<rowData.length) {
rowData[i][0] = rs_1.getString("���");
rowData[i][1] = rs_1.getString("����");
rowData[i][2] = rs_1.getString("����ʱ��");
i++;
}
while(i<rowData.length) {
rowData[i][0] = null;
rowData[i][1] = null;
rowData[i][2] = null;
i++;
}
//ˢ��
TableModel tml = new DefaultTableModel(rowData,Name);
table.setModel(tml);
dbConn.close();
lblNewLabel_1.setVisible(true);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_2.setVisible(true);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_2.setVisible(true);
}
if(lblNewLabel_2.isVisible() || lblNewLabel_3.isVisible()) {
lblNewLabel_1.setVisible(false);
}
if(lblNewLabel_1.isVisible()) {
lblNewLabel_2.setVisible(false);
lblNewLabel_3.setVisible(false);
}
}
}
});
btnNewButton.setBounds(10, 356, 93, 23);
contentPane.add(btnNewButton);
}
}
//这是一个名为ReturnBook的Java类,它是一个图书归还界面的窗体。
// 该类继承自JFrame类,并实现了图形用户界面的构建和相关操作。
//
//在该类中,有以下主要成员变量和方法:
//
//contentPane:面板,用于放置其他组件。
//textField:文本框,用于输入图书编号。
//lblNewLabel、lblNewLabel_1、lblNewLabel_2、lblNewLabel_3:标签,用于显示提示信息。
//btnNewButton_1、btnNewButton:按钮,用于触发相应的操作。
//panel:面板,用于放置表格组件。
//scrollPane:滚动面板,用于包含表格。
//table:表格,用于显示图书信息。
//main方法:程序入口,创建并显示ReturnBook窗体对象。
//在构造方法ReturnBook()中,进行了窗体的初始化工作。
// 设置了窗体的大小、位置、布局等。在面板中添加了各个组件,并设置了它们的位置和大小。
//
//同时,类中还包括了与数据库的交互操作。
// 通过JDBC连接数据库,获取图书的借阅信息,并在表格中显示出来。
// 当用户点击"还书"按钮时,会根据输入的图书编号进行相应的处理:更新图书状态、
// 插入归还记录、删除借阅记录等。
//
//需要注意的是,该代码中存在一些未注释的异常处理和数据库操作,
// 具体执行的逻辑需要根据实际需求进行调整和完善。
/*String user_1=null;
Statement state = dbConn.createStatement();
String sql2 = "select top 1* from �����˺� order by ʱ�� desc";
ResultSet rs = state.executeQuery(sql2); //���һ��
String s1 = new String();
while(rs.next()) {
s1 = rs.getString(1);
}
user_1 = s1;//�˺�
Statement state_1 = dbConn.createStatement();
String sql1 = "select * from ���� where ����֤�� = '"+user_1+"'";
ResultSet rs_1 = state_1.executeQuery(sql1);
int i = 0;
while(rs_1.next() && i<rowData.length) {
rowData[i][0] = rs_1.getString("���");
rowData[i][1] = rs_1.getString("����");
rowData[i][2] = rs_1.getString("����ʱ��");
i++;
}
*/
U_mianPage
package Testv1;
/*
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class U_mianPage extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
/*
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
U_mianPage frame = new U_mianPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
*/
/**
* Create the frame.
*/
/*public U_mianPage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
public class U_mianPage extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
U_mianPage frame = new U_mianPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public U_mianPage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 741, 436);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("ͼ���ѯ");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookFind bf = new BookFind();
bf.setVisible(true);
}
});
btnNewButton.setBounds(115, 53, 134, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("ͼ�����");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Lend lendb = new Lend();
lendb.setVisible(true);
}
});
btnNewButton_1.setBounds(115, 170, 134, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("ͼ��黹");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ReturnBook rb = new ReturnBook();
rb.setVisible(true);
}
});
btnNewButton_2.setBounds(115, 278, 134, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("�ĸ�����Ϣ");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
U_upPerson up = new U_upPerson();
up.setVisible(true);
}
});
btnNewButton_3.setBounds(496, 53, 141, 23);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("������");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpdatePassword upw = new UpdatePassword();
upw.setVisible(true);
}
});
btnNewButton_4.setBounds(496, 170, 141, 23);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("����");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
btnNewButton_5.setBounds(496, 278, 141, 23);
contentPane.add(btnNewButton_5);
JLabel lblNewLabel = new JLabel("\u6B22\u8FCE\u4F7F\u7528");
lblNewLabel.setFont(new Font("����", Font.PLAIN, 30));
lblNewLabel.setBounds(295, 108, 188, 36);
contentPane.add(lblNewLabel);
}
}
//这是一个名为U_mianPage的Java类,
// 它是一个用户主页面的窗体。该类继承自JFrame类,并实现了图形用户界面的构建和相关操作。
//
//在该类中,有以下主要成员变量和方法:
//
//contentPane:面板,用于放置其他组件。
//btnNewButton、btnNewButton_1、btnNewButton_2、btnNewButton_3、
// btnNewButton_4、btnNewButton_5:按钮,用于触发相应的操作。
//lblNewLabel:标签,用于显示欢迎信息。
//main方法:程序入口,创建并显示U_mianPage窗体对象。
//在构造方法U_mianPage()中,进行了窗体的初始化工作。
// 设置了窗体的大小、位置、布局等。在面板中添加了各个按钮和标签,并设置了它们的位置和大小。
//
//每个按钮都添加了相应的事件监听器,当用户点击对应按钮时,
// 会创建并显示其他窗体对象(如图书查询、图书借阅、图书归还等)。
//
//需要注意的是,该代码中存在一些中文注释,Unicode编码的字符串和其他UI元素,
// 需要根据实际需求进行调整和完善。
U_upPerson
package Testv1;
import java.sql.*;
/*
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class U_upPerson extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
/* public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
U_upPerson frame = new U_upPerson();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
*/
/**
* Create the frame.
*/
/*public U_upPerson() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
public class U_upPerson extends JFrame {
private JPanel contentPane;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
U_upPerson frame = new U_upPerson();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public U_upPerson() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 621, 343);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u59D3\u540D\uFF1A");
lblNewLabel.setBounds(27, 103, 58, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u6027\u522B\uFF1A");
lblNewLabel_1.setBounds(27, 175, 58, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u8054\u7CFB\u7535\u8BDD\uFF1A");
lblNewLabel_2.setBounds(25, 233, 94, 18);
contentPane.add(lblNewLabel_2);
textField_1 = new JTextField(); //�Ա�
textField_1.setBounds(95, 172, 170, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField(); //��ϵ�绰
textField_2.setBounds(95, 232, 170, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("\u804C\u4E1A\uFF1A");
lblNewLabel_3.setBounds(362, 103, 58, 15);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u6240\u5728\u5355\u4F4D\uFF1A");
lblNewLabel_4.setBounds(341, 162, 79, 40);
contentPane.add(lblNewLabel_4);
textField_3 = new JTextField(); //ְҵ
textField_3.setBounds(434, 100, 142, 21);
contentPane.add(textField_3);
textField_3.setColumns(10);
JButton btnNewButton = new JButton("\u8FD4\u56DE");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //
setVisible(false);
}
});
btnNewButton.setBounds(479, 231, 97, 23);
contentPane.add(btnNewButton);
textField_4 = new JTextField(); //���ڵ�λ
textField_4.setBounds(430, 172, 142, 21);
contentPane.add(textField_4);
textField_4.setColumns(10);
textField = new JTextField(); //����
textField.setBounds(97, 100, 168, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("\u8BF7\u4FEE\u6539\u4E3A\u7537\u6216\u5973");
lblNewLabel_5.setForeground(Color.RED);
lblNewLabel_5.setBounds(110, 206, 97, 15);
contentPane.add(lblNewLabel_5);
lblNewLabel_5.setVisible(false);
JLabel lblNewLabel_6 = new JLabel("\u4FEE\u6539\u6210\u529F");
lblNewLabel_6.setBounds(351, 264, 79, 15);
contentPane.add(lblNewLabel_6);
lblNewLabel_6.setVisible(false);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 585, 67);
contentPane.add(panel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 46, 300, 150);
panel.add(scrollPane);
String []Name = {"����֤��", "����", "�Ա�", "��ϵ�绰","ְҵ","���ڵ�λ"};
Object [][] rowData = new Object [1][6];
JTable table = new JTable(rowData, Name);
table.setBounds(297, 179, -279, -124);
table.setRowHeight(30); //�����и�
table.getColumnModel().getColumn(0).setPreferredWidth(110); //��һ���п�
table.setPreferredScrollableViewportSize(new Dimension(550 ,300)); //���ù�������ӿڴ�С�������ô�С����������Ҫ�϶���������
scrollPane.setViewportView(table);
String num0 = null;
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select top 1* from �����˺� order by ʱ�� desc";
ResultSet rs = state.executeQuery(sql0); //���һ��
String s1 = new String();
while(rs.next()) {
s1 = rs.getString(1);
}
num0 = s1;//�˺�
Statement stateCnt = dbConn.createStatement();
String sql1 = "select * from ������ where ����֤��='"+num0+"'";
ResultSet rs_1 = stateCnt.executeQuery(sql1);
int i = 0;
while(rs_1.next() && i<rowData.length) {
rowData[i][0] = rs_1.getString(1);
rowData[i][1] = rs_1.getString(2);
rowData[i][2] = rs_1.getString(3);
rowData[i][3] = rs_1.getString(4);
rowData[i][4] = rs_1.getString(5);
rowData[i][5] = rs_1.getString(6);
i++;
}
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JButton btnNewButton_1 = new JButton("\u786E\u8BA4\u4FEE\u6539");
btnNewButton_1.addActionListener(new ActionListener() { //��
public void actionPerformed(ActionEvent e) {
String name = new String(textField.getText());
String work = new String(textField_3.getText());
String sex = new String(textField_1.getText());
String phone = new String(textField_2.getText());
String site = new String(textField_4.getText());
String num = new String();
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
try {
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
//String sql1="update ͼ�����Ա set ����=name,�Ա�=sex,ְ��=work,��ϵ�绰=phone where ����='N10014' "; //��
//state.executeUpdate(sql1); //��sql����ϴ������ݿ�ִ��
String sql0 = "select top 1* from �����˺� order by ʱ�� desc";
ResultSet rs = state.executeQuery(sql0); //���һ��
String s1 = new String();
while(rs.next()) {
s1 = rs.getString(1);
}
num = s1;//����
PreparedStatement pst = null;
if (name.equals("") == false) {
String sql = "update ������ set ����=? where ����֤��=?";
pst = dbConn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, num);
pst.executeUpdate();
}
if(sex.equals("") == false) {
try {
String sql = "update ������ set �Ա�=? where ����֤��=?";
pst = dbConn.prepareStatement(sql);
pst.setString(1, sex);
pst.setString(2, num);
pst.executeUpdate();
lblNewLabel_5.setVisible(false);
}
catch(Exception ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
lblNewLabel_5.setVisible(true);
lblNewLabel_6.setVisible(false);
}
}
if(work.equals("") == false) {
String sql = "update ������ set ְҵ=? where ����֤��=?";
pst = dbConn.prepareStatement(sql);
pst.setString(1, work);
pst.setString(2, num);
pst.executeUpdate();
}
if(phone.equals("") == false) {
String sql = "update ������ set ��ϵ�绰=? where ����֤��=?";
pst = dbConn.prepareStatement(sql);
pst.setString(1, phone);
pst.setString(2, num);
pst.executeUpdate();
}
if(site.equals("") == false) {
String sql = "update ������ set ���ڵ�λ=? where ����֤��=?";
pst = dbConn.prepareStatement(sql);
pst.setString(1, site);
pst.setString(2, num);
pst.executeUpdate();
}
Statement stateCnt = dbConn.createStatement();
String sql1 = "select * from ������ where ����֤��='"+num+"'";
ResultSet rs_1 = stateCnt.executeQuery(sql1);
int i = 0;
while(rs_1.next() && i<rowData.length) {
rowData[i][0] = rs_1.getString(1);
rowData[i][1] = rs_1.getString(2);
rowData[i][2] = rs_1.getString(3);
rowData[i][3] = rs_1.getString(4);
rowData[i][4] = rs_1.getString(5);
rowData[i][5] = rs_1.getString(6);
i++;
}
//ˢ��
TableModel tml = new DefaultTableModel(rowData,Name);
table.setModel(tml);
pst.close();
dbConn.close();
if(lblNewLabel_5.isVisible()) {
lblNewLabel_6.setVisible(false);
}else {
lblNewLabel_6.setVisible(true);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_1.setBounds(341, 231, 93, 23);
contentPane.add(btnNewButton_1);
}
}
//当您运行这段代码时,将创建一个名为U_upPerson的窗口,
// 并在窗口中显示一个表单用于修改人员信息。
//
//窗口的布局使用了Null Layout,意味着组件的位置和大小需要手动设置。
// 窗口的大小被设置为621x343像素。
//
//界面上有以下组件:
//
//JLabel:用于显示标签文本。
//JTextField:用于用户输入文本。
//JButton:用于触发特定动作的按钮。
//JScrollPane:用于在表格不够大时显示滚动条。
//在窗口的顶部是一个JPanel,用于容纳一些其他组件。
// 这个面板使用了FlowLayout布局管理器,并添加了一个JScrollPane作为子组件。
//
//在JScrollPane中,创建了一个空的表格JTable,
// 将其放置在滚动窗格中。表格使用了自定义的表头和数据模型来显示人员信息。
// 表头列名为"身份证号"、"姓名"、"性别"、"联系电话"、"职业"和"所在单位"。
//
//通过数据库连接获取到的人员信息将会填充到表格的数据模型中,并显示在界面上。
//
//界面下方有一个"确认修改"按钮和一个"返回"按钮。点击"确认修改"按钮后,
// 程序将从文本框中获取用户输入的信息,并将其更新到数据库中,
// 并将更新后的信息刷新显示在表格中。点击"返回"按钮后,程序将关闭当前窗口。
//
//这是一个简单的人员信息修改界面的代码示例。您可以根据实际需求进行扩展和修改。
//
//希望这次的解答能够满足您的需求,如果还有其他问题,请随时提问。
UpdateBook
package Testv1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class UpdateBook extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UpdateBook frame = new UpdateBook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//这段代码是UpdateBook类的定义,继承自JFrame类,
// 用于创建一个修改图书信息的窗口应用程序。
// 在UpdateBook类中有三个成员变量contentPane、
// textField和textField_1,
// 分别用于存储内容面板和两个文本框组件。
// 它还包含了一个构造函数和一个静态main方法。
//
//在main方法中,使用EventQueue.invokeLater()方法创建一个新线程,
// 在新线程中创建UpdateBook对象,并设置其可见性。
// 该方式确保了创建窗口应用程序的过程是在事件调度线程(Event Dispatch Thread)中进行的,
// 从而避免了在多线程情况下可能导致的线程安全问题。
//
//这段代码还没有完成整个窗口应用程序的实现,
// 需要在UpdateBook类中添加更多的组件和事件监听器等功能才能完整地实现更新图书信息的功能。
/**
* Create the frame.
*/
public UpdateBook() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 612, 458);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u7F16\u8F91\u56FE\u4E66");
lblNewLabel.setFont(new Font("����", Font.PLAIN, 20));
lblNewLabel.setBounds(255, 5, 142, 35);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u8F93\u5165\u4E66\u53F7");
lblNewLabel_1.setBounds(20, 54, 54, 15);
contentPane.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(84, 51, 131, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setBounds(10, 128, 54, 15);
contentPane.add(lblNewLabel_2);
JButton btnNewButton_1 = new JButton("\u8FD4\u56DE");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
btnNewButton_1.setBounds(493, 386, 93, 23);
contentPane.add(btnNewButton_1);
//这段代码是UpdateBook类的构造函数,
// 其中包含了窗口应用程序的界面设计代码。在该构造函数中,
// 首先设置了窗口关闭操作,并设定了窗口的大小和位置,
// 然后创建了一个内容面板对象contentPane,将其边框设为5像素的空边框,
// 并设置其布局方式为null(绝对布局)。
//
//在内容面板中,首先添加了一个“编辑图书”(“Edit Book”)的标签,字体大小设置为20。
// 然后添加了一个“输入书号”(“Input Book ID”)的标签和一个文本框组件textField,
// 用于输入要修改的图书的编号。此外,还添加了一个空白的标签和一个返回按钮组件btnNewButton_1。
// 返回按钮的单击事件包含一个ActionListener接口实现,用于将当前窗口设置为不可见。
//
//整个窗口应用程序的界面还没有完全实现,
// 需要在UpdateBook类中添加更多的组件和事件处理器等功能才能完整地实现更新图书信息的功能。
JPanel panel = new JPanel();
panel.setBounds(10, 86, 576, 262);
contentPane.add(panel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 46, 300, 150);
panel.add(scrollPane);
String []Name = {"���", "����", "����", "������","����","���ݼ��","״̬","����"};
Object [][] rowData = new Object [100][8];
JTable table = new JTable(rowData, Name);
table.setBounds(297, 179, -279, -124);
table.setRowHeight(30); //�����и�
table.getColumnModel().getColumn(0).setPreferredWidth(110); //��һ���п�
table.setPreferredScrollableViewportSize(new Dimension(550 ,300)); //���ù�������ӿڴ�С�������ô�С����������Ҫ�϶���������
scrollPane.setViewportView(table);
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select * from ͼ��";
ResultSet rs = state.executeQuery(sql0);
//String s1 = new String();
int i = 0;
while(rs.next() && i<rowData.length) {
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2] = rs.getString(3);
rowData[i][3] = rs.getString(4);
rowData[i][4] = rs.getString(5);
rowData[i][5] = rs.getString(6);
rowData[i][6] = rs.getString(7);
rowData[i][7] = rs.getString(8);
i++;
}
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() { //��ѯ
public void actionPerformed(ActionEvent e) {
String str0 = new String(textField.getText());
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select * from ͼ�� where ���='"+str0+"'" ;
ResultSet rs = state.executeQuery(sql0);
//这段代码定义了一个JPanel对象panel,位置和大小都在内容面板contentPane内,
// 用于存放一个带滚动条的表格table,以展示数据库中的图书信息。
// 在panel中添加了一个JScrollPane对象scrollPane,
// 设置其边界和位置,然后在scrollPane中添加了一个JTable对象table,
// 通过设置表格的列名和数据模型(即rowData数组)来显示图书信息。
// 设置表格的行高、列宽、显示大小等属性,以便更好地展示图书信息。
//
//该段代码还包括了从数据库中获取图书信息的代码。
// 首先建立与数据库的连接,并通过Statement对象执行SQL命令进行查询。
// 查询数据保存在ResultSet对象rs中,
// 并逐行遍历ResultSet对象将图书信息分别保存在rowData数组的对应位置。
// 最后,关闭与数据库的连接。
//
//在代码最后,定义了一个按钮btnNewButton,
// 添加了一个ActionListener事件监听器,用于实现根据输入的书号信息查询对应的图书信息。
// 在ActionListener的方法中,先获取用户输入的图书编号信息,
// 然后与数据库中的图书信息进行匹配,若匹配成功则在表格中展示对应的图书信息。
int i = 0;
while(rs.next() && i<rowData.length) {
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2] = rs.getString(3);
rowData[i][3] = rs.getString(4);
rowData[i][4] = rs.getString(5);
rowData[i][5] = rs.getString(6);
rowData[i][6] = rs.getString(7);
rowData[i][7] = rs.getString(8);
i++;
}
while(i<rowData.length) {
rowData[i][0] = null;
rowData[i][1] = null;
rowData[i][2] = null;
rowData[i][3] = null;
rowData[i][4] = null;
rowData[i][5] = null;
rowData[i][6] = null;
rowData[i][7] = null;
i++;
}
//ˢ��
TableModel tml = new DefaultTableModel(rowData,Name);
table.setModel(tml);
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(248, 50, 93, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_2 = new JButton("\u4FEE\u6539");
btnNewButton_2.addActionListener(new ActionListener() { //ȷ����
public void actionPerformed(ActionEvent e) {
M_Up_book Upbook = new M_Up_book();
Upbook.setVisible(true);
}
});
btnNewButton_2.setBounds(20, 358, 93, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("\u590D\u4F4D");
btnNewButton_3.addActionListener(new ActionListener() { //��λ
public void actionPerformed(ActionEvent e) {
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Statement state = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
state = dbConn.createStatement();
String sql0 = "select * from ͼ��";
ResultSet rs = state.executeQuery(sql0);
//String s1 = new String();
//该段代码在按钮btnNewButton_2和btnNewButton_3的ActionListener
// 事件监听器中实现了图书信息的修改和还原操作。在按钮btnNewButton_2的事件监听器中,
// 首先创建一个新的M_Up_book类对象Upbook,
// 并将其设置为可见状态,用于实现图书信息的修改功能。
// 在M_Up_book类中会定义实现修改操作的界面和事件处理器,
// 提供用户修改图书信息并将修改后的信息更新到数据库中。
//
//在按钮btnNewButton_3的事件监听器中,通过与数据库建立连接,
// 并执行查询命令获取全部图书信息。然后逐行遍历查询结果,
// 并将图书信息保存在rowData数组中。如果查询结果的行数小于rowData的长度,
// 则将剩余的空白行设置为空。
// 最后,将表格的数据模型更新为包含最新图书信息的DefaultTableModel对象,
// 以实现将表格数据还原为最初状态,以便用户重新进行查询和修改操作。
int i = 0;
while(rs.next() && i<rowData.length) {
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2] = rs.getString(3);
rowData[i][3] = rs.getString(4);
rowData[i][4] = rs.getString(5);
rowData[i][5] = rs.getString(6);
rowData[i][6] = rs.getString(7);
rowData[i][7] = rs.getString(8);
i++;
}
while(i<rowData.length) {
rowData[i][0] = null;
rowData[i][1] = null;
rowData[i][2] = null;
rowData[i][3] = null;
rowData[i][4] = null;
rowData[i][5] = null;
rowData[i][6] = null;
rowData[i][7] = null;
i++;
}
//ˢ��
TableModel tml = new DefaultTableModel(rowData,Name);
table.setModel(tml);
dbConn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_3.setBounds(449, 50, 93, 23);
contentPane.add(btnNewButton_3);
//该段代码定义了按钮btnNewButton_3的事件监听器,
// 用于重置表格数据。当用户想要重新进行查询操作或者想要将表格还原为最初状态时,
// 单击该按钮即可清空表格中的所有数据。
//
//事件监听器通过与数据库建立连接并查询全部图书信息来获取表格数据,
// 然后将rowData数组中所有元素置为空。
// 最后,将表格的数据模型更新为包含最新图书信息的DefaultTableModel对象,
// 以实现将表格数据还原为最初状态。
JLabel lblNewLabel_3 = new JLabel("\u8981\u5220\u9664\u7684\u56FE\u4E66\u4E66\u53F7");
lblNewLabel_3.setBounds(255, 362, 100, 15);
contentPane.add(lblNewLabel_3);
textField_1 = new JTextField();
textField_1.setBounds(356, 359, 80, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("\u64CD\u4F5C\u6210\u529F");
lblNewLabel_4.setBounds(248, 390, 54, 15);
contentPane.add(lblNewLabel_4);
lblNewLabel_4.setVisible(false);
JLabel lblNewLabel_5 = new JLabel("\u64CD\u4F5C\u5931\u8D25");
lblNewLabel_5.setForeground(Color.RED);
lblNewLabel_5.setBounds(161, 390, 54, 15);
contentPane.add(lblNewLabel_5);
lblNewLabel_5.setVisible(false);
JButton btnNewButton_4 = new JButton("\u5220\u9664");
btnNewButton_4.addActionListener(new ActionListener() {//ɾ��
public void actionPerformed(ActionEvent e) {
String num = new String (textField_1.getText());//���Ҫɾ�������
try {
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=LibraryManagement";
String userName="sa";
String userPwd="QWEASDZXC000000";
Connection dbConn = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConn = DriverManager.getConnection(dbURL,userName,userPwd);
String sql0 = "delete from ͼ�� where ���= ?";
PreparedStatement pst = dbConn.prepareStatement(sql0);//ɾ��
pst.setString(1, num);//���
pst.addBatch();
pst.executeBatch();
Statement state = dbConn.createStatement();
String sql = "select * from ͼ��";
ResultSet rs = state.executeQuery(sql);
//这段代码向窗口中添加了一些用于删除图书信息的元素。
// 首先添加了一个“要删除的图书图书号”的标签lblNewLabel_3
// 和一个文本框textField_1,用户可以在该文本框中输入要删除的图书编号。
// 接着添加了一个表示“操作成功”的标签lblNewLabel_4
// 和一个表示“操作失败”的标签lblNewLabel_5。
//
// 这些标签的可见性都是初始化为不可见。
//然后向窗口中添加了一个“删除”按钮btnNewButton_4,
// 该按钮添加了一个ActionListener事件监听器。
// 当用户单击该按钮时,程序会从textField_1中获取要删除的图书编号,
// 然后与数据库中的图书信息进行匹配,根据匹配结果执行相应的操作。
// 如果匹配成功,则在图书信息表格中删除对应的图书信息;
// 如果匹配失败,则弹出操作失败的提示信息lblNewLabel_5。
//
//在匹配成功的情况下,需要根据新更新的数据重新生成一个DefaultTableModel对象,
// 并将其赋值给table对象。以便在删除图书信息后及时刷新窗口中的表格,
// 并将表格显示为新的内容。
int i = 0;
while(rs.next() && i<rowData.length) {
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2] = rs.getString(3);
rowData[i][3] = rs.getString(4);
rowData[i][4] = rs.getString(5);
rowData[i][5] = rs.getString(6);
rowData[i][6] = rs.getString(7);
rowData[i][7] = rs.getString(8);
i++;
}
while(i<rowData.length) {
rowData[i][0] = null;
rowData[i][1] = null;
rowData[i][2] = null;
rowData[i][3] = null;
rowData[i][4] = null;
rowData[i][5] = null;
rowData[i][6] = null;
rowData[i][7] = null;
i++;
}
//ˢ��
TableModel tml = new DefaultTableModel(rowData,Name);
table.setModel(tml);
dbConn.close();
//lblNewLabel_4.setVisible(true);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_5.setVisible(true);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
lblNewLabel_5.setVisible(true);
}
if(lblNewLabel_4.isVisible()) {
lblNewLabel_5.setVisible(false);
}
if(lblNewLabel_5.isVisible()) {
lblNewLabel_4.setVisible(false);
}
}
});
btnNewButton_4.setBounds(150, 358, 93, 23);
contentPane.add(btnNewButton_4);
}
}
//该段代码的主要作用是实现删除图书信息的功能。事件监听器使用PreparedStatement对象执行SQL命令,
// 在数据库中删除对应图书编号的图书信息。然后根据同样的方法从数据库中读取最新的图书信息,
// 并根据读取结果更新rowData数组中的数据,
// 然后将更新后的数据模型更新到table对象中以更新窗口的图书信息表格。
//
//在操作过程中,程序通过设置标签lblNewLabel_4和lblNewLabel_5的可见性来提示操作是否成功。
// 如果操作成功,则显示lblNewLabel_4,否则显示lblNewLabel_5。
// 同时,还需要通过相应的判断控制标签的可见性,使其在不同状态下正确显示。
//
//总的来说,这段代码实现了一个简单的图书管理系统,
// 允许用户在图书信息表格中查询、修改、删除相关信息。
// 通过连接和操作数据库以及创建窗口UI元素来实现这些功能。
UpdatePassword
UpdateUser