📄 batadd.java
字号:
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
class BatAdd extends JFrame implements ActionListener
{
JLabel l_msg,l_bookname,l_company,l_author,l_wenli,l_price,l_number,l_id;
JComboBox t_bookname,t_company,t_author,t_wenli,t_price,t_number,t_id;
JButton b_clear,b_add,b_back,b_open,b_insert;
JPanel pan1,pan2,pan3,pan4,pan5,pan6,pan7,pan9,pan0,pan10;
JTextField t_path;
JFileChooser f_ch=new JFileChooser();
NetConn sql;
Statement sqll,sqlll;
ResultSet rs,rs1;
String a0,a1,a2,a3,a4,a5,a6,a7;
BatAdd()
{
super("批量添加窗口");
l_msg =new JLabel("添加书籍 ");
l_id =new JLabel("书籍编号:");
l_bookname=new JLabel("书籍名称:");
l_company =new JLabel("出版发行:");
l_author =new JLabel("书籍作者:");
l_wenli =new JLabel("文理分类:");
l_price =new JLabel("书籍售价:");
l_number =new JLabel("书籍数量:");
t_path =new JTextField("",12);
t_id =new JComboBox();
t_id.setEditable(false);
t_bookname=new JComboBox();
t_bookname.setEditable(false);
t_company =new JComboBox();
t_company.setEditable(false);
t_author =new JComboBox();
t_author.setEditable(false);
t_wenli =new JComboBox();
t_wenli.setEditable(false);
t_price =new JComboBox();
t_price.setEditable(false);
t_number =new JComboBox();
t_number.setEditable(false);
b_open =new JButton("打开");
b_insert =new JButton("导入");
b_clear =new JButton("清空");
b_add =new JButton("添加");
b_back =new JButton("返回");
b_open.addActionListener(this);
b_insert.addActionListener(this);
b_clear.addActionListener(this);
b_add.addActionListener(this);
b_back.addActionListener(this);
pan0=new JPanel();pan1=new JPanel();pan2=new JPanel();pan3=new JPanel();pan4=new JPanel();pan5=new JPanel();pan6=new JPanel();pan7=new JPanel();pan9=new JPanel();pan10=new JPanel();
pan1.add(l_msg);pan0.add(l_id);pan0.add(t_id);pan2.add(l_bookname);pan2.add(t_bookname);pan3.add(l_company);pan3.add(t_company);
pan4.add(l_author);pan4.add(t_author);pan5.add(l_wenli);pan5.add(t_wenli);pan6.add(l_price);pan6.add(t_price);
pan7.add(l_number);pan7.add(t_number);pan9.add(b_add);pan9.add(b_clear);pan9.add(b_back);
pan10.add(t_path);pan10.add(b_open);pan10.add(b_insert);
setLayout(new GridLayout(10,1));
add(pan1);add(pan10);add(pan0);add(pan2);add(pan3);add(pan4);add(pan5);add(pan6);add(pan7);add(pan9);
sql=new NetConn();
this.pack();
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
}
public void actionPerformed(ActionEvent ec)
{
if(ec.getSource()==b_back)
{
dispose();
}
else if(ec.getSource()==b_clear)
{
t_id.removeAllItems();
t_bookname.removeAllItems();
t_company.removeAllItems();
t_author.removeAllItems();
t_wenli.removeAllItems();
t_price.removeAllItems();
t_number.removeAllItems();
t_path.setText("");
l_msg=new JLabel("添加书籍 ");
}
else if(ec.getSource()==b_add)
{
if(t_bookname.getSelectedItem().equals("")||t_company.getSelectedItem().equals("")||t_author.getSelectedItem().equals(""))
{
l_msg.setText("书名,出版社和作者不能为空!");
}
else
{
try
{
sqll=sql.connect();
sqlll=sql.connect();
int count=t_id.getItemCount();
for(int x=0;x<t_id.getItemCount();x++)
{
a0="'"+t_id.getItemAt(x)+"'";
a1="'"+t_bookname.getItemAt(x)+"'";
a2="'"+t_company.getItemAt(x)+"'";
a3="'"+t_author.getItemAt(x)+"'";
a4="'"+t_wenli.getItemAt(x)+"'";
a5="'"+t_price.getItemAt(x)+"'";
a6="'"+t_number.getItemAt(x)+"'";
l_msg.setText("1");
rs=sqll.executeQuery("SELECT * FROM books where id="+a0);
rs1=sqlll.executeQuery("SELECT * FROM books where bookname="+a1);
if(rs.next()||rs1.next())
{
count--;
}
else
{
String temp="insert into books (id,bookname, company, author, wenli, price, conun) VALUES ("+a0+","+a1+","+a2+","+a3+","+a4+","+a5+","+a6+")";
sqll.executeUpdate(temp);
}
}
l_msg.setText("共导入"+t_id.getItemCount()+"条记录,其中添加成功"+count+"条记录!");
}
catch(SQLException e3)
{System.out.println(e3);}
}
}
else if(ec.getSource()==b_open)
{
int sel=f_ch.showOpenDialog(this);
if(sel==JFileChooser.APPROVE_OPTION)
{
File file=f_ch.getSelectedFile();
t_path.setText(file.getPath());
}
}
else if(ec.getSource()==b_insert)
{
String filepath=t_path.getText();
String tem=null;
BufferedReader br=null;
StringTokenizer st=null;
try{
br=new BufferedReader(new FileReader(filepath));
while (br.ready())
{
tem=br.readLine();
if (tem!=null&&tem!="")
{
st=new StringTokenizer(tem,"--");
for(int x=0;x<7;x++)
{
switch(x)
{
case 0:t_id.addItem(st.nextToken().trim());break;
case 1:t_bookname.addItem(st.nextToken().trim());break;
case 2:t_company.addItem(st.nextToken().trim());break;
case 3:t_author.addItem(st.nextToken().trim());break;
case 4:t_wenli.addItem(st.nextToken().trim());break;
case 5:t_price.addItem(st.nextToken().trim());break;
case 6:t_number.addItem(st.nextToken().trim());break;
}
}
}
}
}catch (Exception e1) {
e1.printStackTrace();
}finally{
try{
br.close();
}catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -