⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addbook.java

📁 用java实现图书资料管理系统 操作系统:Windows 数据库:Windows Access 数据库 开发平台:NetBeans 开发语言:Java 语言
💻 JAVA
字号:
/*
 * register.java
 *
 * Created on 2007年12月18日, 下午11:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Owner
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class addbook extends JPanel implements ActionListener
{
    Box basebox,box1,box2,box3,box4;
    JButton submit,reset;
    JTextField isbn,bookname,author,price,barcode;
    JTextArea content;
    Choice publisherlist,bookclasslist;
    Connection con;
    Statement sql;
    ResultSet rs;
    String s;
    userflag userflag;
    Panel pcenter;
    /** Creates a new instance of register */
    public addbook(Panel pcenter,userflag userflag) 
    {
        this.userflag=userflag;
        this.pcenter=pcenter;
        if(userflag.flag==false)
        {
            JOptionPane.showMessageDialog(this,"您还没登录");
            add(new Label("请先登录"),BorderLayout.CENTER);
        }
        else
        {
            isbn=new JTextField(15);
            barcode=new JTextField(15);
            bookname=new JTextField(15);
            author=new JTextField(15);
            price=new JTextField(15);
            content=new JTextArea(6,15);
            publisherlist=new Choice();
            bookclasslist=new Choice();
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            }
            catch(ClassNotFoundException e2){}
            try
            {
                con=DriverManager.getConnection("jdbc:odbc:sample","std","123");
                sql=con.createStatement();
                s="select * from publisher";
                rs=sql.executeQuery(s);
                while(rs.next())
                {
                    publisherlist.add(rs.getString("publishername"));
                }
                s="select * from bookclass";
                rs=sql.executeQuery(s);
                while(rs.next())
                {
                    bookclasslist.addItem(rs.getString("bookclassname"));
                }
            }
            catch(SQLException e3)
            {
               System.out.println(e3);
            }
            submit=new JButton("提交");
            reset=new JButton("重设");
            submit.addActionListener(this);
            reset.addActionListener(this);

            box1=Box.createVerticalBox();
            box1.add(new Label("图书标识"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("图书ISBN"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("图书名称"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("出版商"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("图书类型"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("作者"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("价格"));
            box1.add(Box.createVerticalStrut(8));
            box1.add(new Label("内容简介"));
            box1.add(Box.createVerticalStrut(90));

            box2=Box.createVerticalBox();
            box2.add(barcode);
            box2.add(Box.createVerticalStrut(8));
            box2.add(isbn);
            box2.add(Box.createVerticalStrut(8));
            box2.add(bookname);
            box2.add(Box.createVerticalStrut(8));
            box2.add(publisherlist);
            box2.add(Box.createVerticalStrut(8));
            box2.add(bookclasslist);
            box2.add(Box.createVerticalStrut(8));
            box2.add(author);
            box2.add(Box.createVerticalStrut(8));
            box2.add(price);
            box2.add(Box.createVerticalStrut(15));
            box2.add(content);


            box3=Box.createHorizontalBox();
            box3.add(box1);
            box3.add(Box.createHorizontalStrut(10));
            box3.add(box2);

            box4=Box.createHorizontalBox();
            box4.add(submit);
            box4.add(Box.createHorizontalStrut(45));
            box4.add(reset);

            basebox=Box.createVerticalBox();
            basebox.add(new Label("请填写下列图书信息:"));
            basebox.add(Box.createVerticalStrut(15));
            basebox.add(box3);
            basebox.add(Box.createVerticalStrut(15));
            basebox.add(box4);
            add(basebox);
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==submit)
        {
            if(barcode.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"图书标识不能为空");
            }
            else if(isbn.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"图书ISBN不能为空");
            }
            else  if(bookname.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"图书名字不能为空");
            }
            else  if(author.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"作者不能为空");
            }
            else  if(price.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"价格不能为空");
            }
            else
            {
                s="select * from bookinfo where barcode='"+barcode.getText()+"'";
                try{
                    rs=sql.executeQuery(s);
                    boolean flag=false;
                    flag=rs.next();
                    System.out.println(flag);
                    if(flag==true)
                        JOptionPane.showMessageDialog(this,"这本图书已经在数据库中了");
                    else if(flag==false)
                    {
                        String ss;
                        int publisherid,bookclassid;
                        publisherid=publisherlist.getSelectedIndex()+1;
                        bookclassid=bookclasslist.getSelectedIndex()+1;
                        ss="'"+barcode.getText()+"','"+isbn.getText()+"','"+bookname.getText()+"','"+publisherid+"','"+bookclassid+"','"+author.getText()+"','"+price.getText()+"','"+content.getText()+"'";
                        s="insert into bookinfo(barcode,isbn,bookname,publisherid,bookclassid,author,price,content) values("+ss+")";
                        sql.executeUpdate(s);
                        s="select * from storage where isbn='"+isbn.getText()+"'";
                        rs=sql.executeQuery(s);
                        boolean tempflag=rs.next();
                        if(tempflag==true)
                        {
                            int number=rs.getInt("number")+1;
                            System.out.println("number="+number);
                            s="update storage set [number]='"+number+"' where [isbn]='"+isbn.getText()+"'";
                            System.out.println(s);
                            Statement stmt=con.createStatement();
                            stmt.executeUpdate(s);
                        }
                        else
                        {
                            s="insert into storage values('"+isbn.getText()+"','"+bookname.getText()+"',1)";
                            sql.executeUpdate(s);
                        }
                        JOptionPane.showMessageDialog(this,"成功添加图书");
                    }
                }
                catch(SQLException ee)
                {
                    System.out.println("ee:"+ee);
                }
            }
        }
        else if(e.getSource()==reset)
        {
            barcode.setText(null);
            isbn.setText(null);
            price.setText(null);
            bookname.setText(null);
            author.setText(null);
            content.setText(null);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -