📄 bookupdate.java
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.sql.*;
import javax.swing.JComboBox;
public class bookUpdate extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
private JLabel jLabel6 = null;
private JLabel jLabel7 = null;
private JTextField jTextFieldpress = null;
private JTextField jTextFieldauthor = null;
private JTextField jTextFieldaddress = null;
private JTextField jTextFieldpressdate = null;
private JTextField jTextFieldprice = null;
private JTextField jTextFieldcomment = null;
private JButton jButtonClear = null;
private JButton jButtondicision = null;
private JButton jButtonupdate = null;
private JButton jButtonCancel = null;
ResultSet rs;
private DBManager db=new DBManager();
private JComboBox jComboBoxname = null;
public bookUpdate() throws HeadlessException {
// TODO 自动生成构造函数存根
super();
initialize();
}
public bookUpdate(GraphicsConfiguration arg0) {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public bookUpdate(String arg0) throws HeadlessException {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public bookUpdate(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);
// TODO 自动生成构造函数存根
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(420, 393);
this.setContentPane(getJContentPane());
this.setTitle("更新书籍信息");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()
String sql="select Bookname from Books";
rs=db.getResult(sql);
boolean isexist=false;
try{
isexist=rs.first();
}
catch(SQLException ex){
}
if(!isexist){
JOptionPane.showMessageDialog(null, "没有用户,请确认", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
else{
try{
rs.beforeFirst();
//循环遍历rs中的数据,将其加到jcomboboxbookname中
while(rs.next()){
jComboBoxname.addItem(rs.getString("Bookname"));
}
}
catch(SQLException ex){
}
}
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel7 = new JLabel();
jLabel7.setText("备注");
jLabel7.setSize(new Dimension(60, 20));
jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
jLabel7.setLocation(new Point(40, 225));
jLabel6 = new JLabel();
jLabel6.setText("价格");
jLabel6.setSize(new Dimension(60, 20));
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel6.setLocation(new Point(40, 195));
jLabel5 = new JLabel();
jLabel5.setText("出版日期");
jLabel5.setSize(new Dimension(60, 20));
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setLocation(new Point(40, 165));
jLabel4 = new JLabel();
jLabel4.setText("地址");
jLabel4.setSize(new Dimension(60, 20));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setLocation(new Point(40, 135));
jLabel3 = new JLabel();
jLabel3.setText("作者");
jLabel3.setSize(new Dimension(60, 20));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setLocation(new Point(40, 105));
jLabel2 = new JLabel();
jLabel2.setText("出版社");
jLabel2.setSize(new Dimension(60, 20));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setLocation(new Point(40, 75));
jLabel1 = new JLabel();
jLabel1.setText("名称");
jLabel1.setSize(new Dimension(60, 20));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setLocation(new Point(40, 45));
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(7, 14, 244, 18));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setText("输入书名点击确定,将调出此书相关信息");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
jContentPane.add(jLabel4, null);
jContentPane.add(jLabel5, null);
jContentPane.add(jLabel6, null);
jContentPane.add(jLabel7, null);
jContentPane.add(getJTextFieldpress(), null);
jContentPane.add(getJTextFieldauthor(), null);
jContentPane.add(getJTextFieldaddress(), null);
jContentPane.add(getJTextFieldpressdate(), null);
jContentPane.add(getJTextFieldprice(), null);
jContentPane.add(getJTextFieldcomment(), null);
jContentPane.add(getJButtonClear(), null);
jContentPane.add(getJButtondicision(), null);
jContentPane.add(getJButtonupdate(), null);
jContentPane.add(getJButtonCancel(), null);
jContentPane.add(getJComboBoxname(), null);
}
return jContentPane;
}
/**
* This method initializes jTextFieldpress
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldpress() {
if (jTextFieldpress == null) {
jTextFieldpress = new JTextField();
jTextFieldpress.setLocation(new Point(135, 75));
jTextFieldpress.setSize(new Dimension(166, 20));
}
return jTextFieldpress;
}
/**
* This method initializes jTextFieldauthor
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldauthor() {
if (jTextFieldauthor == null) {
jTextFieldauthor = new JTextField();
jTextFieldauthor.setLocation(new Point(135, 105));
jTextFieldauthor.setSize(new Dimension(166, 20));
}
return jTextFieldauthor;
}
/**
* This method initializes jTextFieldaddress
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldaddress() {
if (jTextFieldaddress == null) {
jTextFieldaddress = new JTextField();
jTextFieldaddress.setLocation(new Point(135, 135));
jTextFieldaddress.setSize(new Dimension(166, 20));
}
return jTextFieldaddress;
}
/**
* This method initializes jTextFieldpressdate
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldpressdate() {
if (jTextFieldpressdate == null) {
jTextFieldpressdate = new JTextField();
jTextFieldpressdate.setLocation(new Point(135, 165));
jTextFieldpressdate.setSize(new Dimension(166, 20));
}
return jTextFieldpressdate;
}
/**
* This method initializes jTextFieldprice
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldprice() {
if (jTextFieldprice == null) {
jTextFieldprice = new JTextField();
jTextFieldprice.setLocation(new Point(135, 195));
jTextFieldprice.setSize(new Dimension(166, 20));
}
return jTextFieldprice;
}
/**
* This method initializes jTextFieldcomment
*
* @return javax.swing.JTextField
*/
private JTextField getJTextFieldcomment() {
if (jTextFieldcomment == null) {
jTextFieldcomment = new JTextField();
jTextFieldcomment.setLocation(new Point(135, 225));
jTextFieldcomment.setSize(new Dimension(166, 20));
}
return jTextFieldcomment;
}
/**
* This method initializes jButtonClear
*
* @return javax.swing.JButton
*/
private JButton getJButtonClear() {
if (jButtonClear == null) {
jButtonClear = new JButton();
jButtonClear.setLocation(new Point(38, 293));
jButtonClear.setText("清空");
jButtonClear.setSize(new Dimension(60, 20));
jButtonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
jComboBoxname.setSelectedItem("");
jTextFieldauthor.setText("");
jTextFieldpress.setText("");
jTextFieldaddress.setText("");
jTextFieldpressdate.setText("");
jTextFieldprice.setText("");
jTextFieldcomment.setText("");
//jTextFieldtotalnum.setText("");
}
});
}
return jButtonClear;
}
/**
* This method initializes jButtondicision
*
* @return javax.swing.JButton
*/
private JButton getJButtondicision() {
if (jButtondicision == null) {
jButtondicision = new JButton();
jButtondicision.setLocation(new Point(103, 293));
jButtondicision.setText("确定");
jButtondicision.setSize(new Dimension(60, 20));
jButtondicision.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
if(jComboBoxname.getSelectedItem().toString().trim().equals("")){
JOptionPane.showMessageDialog(null, "书名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
//生成sql语句
String strSQL;
strSQL="select * from Books where Bookname='"+jComboBoxname.getSelectedItem().toString().trim()+"'";
rs=db.getResult(strSQL);
boolean isexist=false;
try{
isexist=rs.first();
}
catch(SQLException ex1){
}
if(!isexist){
JOptionPane.showMessageDialog(null, "此书不存在", "错误", JOptionPane.ERROR_MESSAGE);
jComboBoxname.setSelectedItem("");
}
else{
try{
//先清空上次信息
jTextFieldauthor.setText("");
jTextFieldpress.setText("");
jTextFieldaddress.setText("");
jTextFieldpressdate.setText("");
jTextFieldprice.setText("");
jTextFieldcomment.setText("");
//jTextFieldtotalnum.setText("");
rs.first();//将光标移到首位置,并写入查询信息
jTextFieldpress.setText(rs.getString("Press"));
jTextFieldauthor.setText(rs.getString("Author"));
jTextFieldaddress.setText(rs.getString("Address"));
jTextFieldpressdate.setText(rs.getString("Pressdate"));
jTextFieldprice.setText(rs.getString("Price"));
jTextFieldcomment.setText(rs.getString("Comment"));
//jTextFieldtotalnum.setText(rs.getString("TotalNum"));
}
catch(SQLException ex){
}
}
}
});
}
return jButtondicision;
}
/**
* This method initializes jButtonupdate
*
* @return javax.swing.JButton
*/
private JButton getJButtonupdate() {
if (jButtonupdate == null) {
jButtonupdate = new JButton();
jButtonupdate.setLocation(new Point(168, 293));
jButtonupdate.setText("更新");
jButtonupdate.setSize(new Dimension(60, 20));
jButtonupdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
String strSQL;
strSQL="update Books set Press='"+jTextFieldpress.getText().trim()+"',";
strSQL=strSQL+"Author='"+jTextFieldauthor.getText().trim()+"',";
strSQL=strSQL+"Address='"+jTextFieldaddress.getText().trim()+"',";
strSQL=strSQL+"Pressdate='"+jTextFieldpressdate.getText().trim()+"',";
strSQL=strSQL+"Price='"+jTextFieldprice.getText().trim()+"',";
strSQL=strSQL+"Comment='"+jTextFieldcomment.getText().trim()+"'";
//strSQL=strSQL+",TotalNum='"+jTextFieldtotalnum.getText().trim()+"'";
strSQL=strSQL+" where Bookname='"+jComboBoxname.getSelectedItem().toString().trim()+"'";System.out.println(strSQL);
//由数据库操作
if(db.executeSql(strSQL)){
JOptionPane.showMessageDialog(null, "更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
buttonexit();
}
else{
JOptionPane.showMessageDialog(null, "更新失败,请重新操作", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
}
return jButtonupdate;
}
/**
* This method initializes jButtonCancel
*
* @return javax.swing.JButton
*/
public void buttonexit(){
this.dispose();
}
private JButton getJButtonCancel() {
if (jButtonCancel == null) {
jButtonCancel = new JButton();
jButtonCancel.setLocation(new Point(233, 293));
jButtonCancel.setText("退出");
jButtonCancel.setSize(new Dimension(60, 20));
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
buttonexit();
}
});
}
return jButtonCancel;
}
/**
* This method initializes jComboBoxname
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBoxname() {
if (jComboBoxname == null) {
jComboBoxname = new JComboBox();
jComboBoxname.setLocation(new Point(135, 45));
jComboBoxname.setEditable(true);
jComboBoxname.setSize(new Dimension(166, 20));
}
return jComboBoxname;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -