📄 commframe.java
字号:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
/*public class rr
{
public static void main(String args[])
{
new CommFrame();
}
}
*/
class Aiuk
{
int m=0;
Connection con;
PreparedStatement sql,sqk;
ResultSet rs;
Aiuk(String s)
{
String s1="select,SELECT";
for(int k=0;k<=3;k++)
{
if((s.regionMatches(k,"select",0,6))||(s.regionMatches(k,"SELECT",0,6)))
m=2;
}//for
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException eq){System.out.println(eq);}
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
if(m==0)
{
sqk=con.prepareStatement(s);
sqk.executeUpdate();
}
else
{
sql=con.prepareStatement(s);
rs=sql.executeQuery();
while(rs.next())
{
int a=rs.getInt("a");
int b=rs.getInt("b");
System.out.println(a+"*****"+b);
}//while
}//else
con.close();
}//try
catch(SQLException et)
{
System.out.println(et);
}
}//liu()
}
class InputAreak extends Panel implements ActionListener
{
File f=null;
Box baseBox ,boxV1,boxV2;
TextField ComNo,ComName,ComPrice,ComDiscount,Channel;
Button button;String s;
InputAreak(File f)
{
setBackground(Color.orange);
this.f=f;
ComNo=new TextField(12);
ComName=new TextField(12);
ComPrice=new TextField(12);
ComDiscount=new TextField(12);
Channel=new TextField(12);
button=new Button("录入");
button.addActionListener(this);
boxV1=Box.createVerticalBox();
boxV1.add(new Label("商品编号"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品名称"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品价格"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品折扣"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("进货渠道"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("单击录入"));
boxV2=Box.createVerticalBox();
boxV2.add(ComNo);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComName);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComPrice);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComDiscount);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Channel);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
}
public void actionPerformed(ActionEvent e)
{
String s1,s2,s3;
int t;
float f;
if(e.getSource()==button)
{
t=Integer.parseInt(ComNo.getText());
s1=ComName.getText();
s2=ComPrice.getText();
f=Float.valueOf(ComDiscount.getText()).floatValue();
s3=Channel.getText();
s="insert into COMMODITY_ID values("+t+",'"+s1+"','"+s2+"',"+f+",'"+s3+"'" +")";
System.out.println(s);
new Aiuk(s);
}
}
}
class DelAreak extends Panel implements ActionListener,ItemListener
{
Label label;
Button button;
Checkbox box1,box2;
CheckboxGroup one;
File f=null;
TextField text;
DelAreak(File f)
{
label=new Label("请按其中一项删除:");
setBackground(Color.orange);
this.f=f;
one=new CheckboxGroup();
add(label);
box1=new Checkbox("商品编号",true,one); box1.addItemListener(this);
box2=new Checkbox("商品名称",false,one);box2.addItemListener(this);
add(box1);add(box2);
text=new TextField(8);
add(text);
// text.addActionListener(this);
button=new Button("确认");add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s;
if(box1.getState())
{
int hh=Integer.parseInt(text.getText());
s="delete * from COMMODITY_ID where 商品编号="+hh;
}
else
{
s="delete * from COMMODITY_ID where 商品名称="+"'"+text.getText()+"'";
}
//连接数据库
{
Connection con;
PreparedStatement sql;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e8)
{
System.out.println(""+e8);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.prepareStatement(s);
sql.executeUpdate();
text.setText("");
con.close();
}
catch(SQLException e9)
{
System.out.println(e9);
}
}//while
}
public void itemStateChanged(ItemEvent e)
{}
}
/*class Show extends JFrame implements ActionListener
{
File f=null;
JTable table;
Object a[][];
Object name[]={"商品编号","商品名称","商品价格","商品折扣","进货渠道"};
Connection con;
PreparedStatement sql;
ResultSet rs;
int i=0,j=0,n=0,m=0,h=0;
Show(File f)
{
this.f=f;
a=new Object[18][5];
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException ee)
{
System.out.println(ee);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.prepareStatement("select * from COMMODITY_ID");
rs=sql.executeQuery();
while(rs.next())
{
int t=rs.getInt("商品编号");
String s1=rs.getString("商品名称");
String s2=rs.getString("商品价格");
float f1=rs.getFloat("商品折扣");
String s3=rs.getString("进货渠道");
a[n++][0]=t;
a[i++][1]=s1;
a[j++][2]=s2;
a[h++][3]=f1;
a[m++][4]=s3;
}
con.close();
}
catch(SQLException w)
{System.out.println(w);}
table=new JTable(a,name);
//button.addActionListener(this);
Container con=getContentPane();
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
con.add(new JLabel("skfdjfkjsf"),BorderLayout.SOUTH);
//con.add(button,BorderLayout.SOUTH);
setSize(600,600);
//setVisible(true);
//validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e4)
{
}
}
*/
class UpdateAreak extends Panel implements ActionListener
{
Label label;
Button button1,button2;
Box baseBox ,boxV1,boxV2;
TextField ComNo,ComName,ComPrice,ComDiscount,Channel;
Connection con;
PreparedStatement sql,sqk;
ResultSet rs;
File f=null;
TextField text;
UpdateAreak(File f)
{
label=new Label("请输入商品编号:");
setBackground(Color.orange);
this.f=f;
add(label);
text=new TextField(5);
add(text);
// text.addActionListener(this);
button1=new Button("确认");
add(button1);
button1.addActionListener(this);
ComNo=new TextField(12);
ComName=new TextField(12);
ComPrice=new TextField(12);
ComDiscount=new TextField(12);
Channel=new TextField(12);
button2=new Button("确定");
button2.addActionListener(this);
boxV1=Box.createVerticalBox();
boxV1.add(new Label("商品编号"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品名称"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品价格"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("商品折扣"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("进货渠道"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new Label("更新后确定"));
boxV2=Box.createVerticalBox();
boxV2.add(ComNo);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComName);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComPrice);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(ComDiscount);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Channel);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(button2);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
}
public void actionPerformed(ActionEvent e)
{
int g=Integer.parseInt(text.getText());
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException eq)
{
System.out.println(eq);
}
if(e.getSource()==button1)
{
String s="select * from COMMODITY_ID where 商品编号="+g;
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.prepareStatement(s);
rs=sql.executeQuery();
while(rs.next())
{
int t=rs.getInt("商品编号");
String s1=rs.getString("商品名称");
String s2=rs.getString("商品价格");
float f=rs.getFloat("商品折扣");
String s3=rs.getString("进货渠道");
ComNo.setText(String.valueOf(t) );
ComName.setText(s1);
ComPrice.setText(s2);
ComDiscount.setText(String.valueOf(f));
Channel.setText(s3);
}
con.close();
}
catch(SQLException et)
{
System.out.println(et);
}
}//if
if(e.getSource()==button2)
{
int t;String s1,s2,s3;float f;
try
{
con=DriverManager.getConnection("jdbc:odbc:sun","","");
t=Integer.parseInt(ComNo.getText());
s1=ComName.getText();
s2=ComPrice.getText();
f=Float.valueOf(ComDiscount.getText()).floatValue();
s3=Channel.getText();
String z="insert into COMMODITY_ID values("+t+",'"+s1+"','"+s2+"',"+f+",'"+s3+"'" +")";
sqk=con.prepareStatement("delete * from COMMODITY_ID where 商品编号="+g);
sqk.executeUpdate();
sql=con.prepareStatement(z);
sql.executeUpdate();
con.close();
}
catch(SQLException et)
{
System.out.println(et);
}
}
}
}
class CommFrame extends Frame implements ActionListener
{
File file=null;
MenuBar bar;
Menu fileMenu;
MenuItem 录入,删除,修改,显示;
//Show show;
TextArea show;
InputAreak inputMessage;
DelAreak delMessage;
UpdateAreak updateMessage;
CardLayout card=null; //卡片式布局.
Panel pCenter;
CommFrame()
{
录入=new MenuItem("录入");
删除=new MenuItem("删除");
修改=new MenuItem("修改");
显示=new MenuItem("显示");
bar=new MenuBar();
fileMenu=new Menu("菜单选项");
fileMenu.add(录入);
fileMenu.add(删除);
fileMenu.add(修改);
fileMenu.add(显示);
bar.add(fileMenu);
setMenuBar(bar);
录入.addActionListener(this);
删除.addActionListener(this);
修改.addActionListener(this);
显示.addActionListener(this);
show=new TextArea(21,78);
add(show);
//show=new Show(file);
inputMessage=new InputAreak(file);
delMessage=new DelAreak(file);
updateMessage=new UpdateAreak(file);
show=new TextArea();
card=new CardLayout();
pCenter=new Panel();
pCenter.setLayout(card);
pCenter.add("录入",inputMessage);
pCenter.add("删除",delMessage);
pCenter.add("修改",updateMessage);
pCenter.add("显示",show);
add(pCenter,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);//System.exit(0);
}
});
setVisible(true);
setBounds(10,10,560,450);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==录入)
{
card.show(pCenter,"录入");
}
else if(e.getSource()==删除)
{
card.show(pCenter,"删除");
}
else if(e.getSource()==修改)
{
card.show(pCenter,"修改");
}
else if(e.getSource()==显示)
{
Connection con;
PreparedStatement sql;
ResultSet rs;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException we)
{System.out.println(we);}
try
{ con=DriverManager.getConnection("jdbc:odbc:sun","","");
sql=con.prepareStatement("select * from COMMODITY_ID");
rs=sql.executeQuery();
show.setText("");
while(rs.next())
{
int mo=rs.getInt("商品编号");
String name=rs.getString("商品名称");
String nn=rs.getString("商品价格");
float mm=rs.getFloat("商品折扣");
String na=rs.getString("进货渠道");
show.append("\n商品编号:"+mo+"商品名称:"+name+"商品价格:"+nn+"商品折扣:"+mm+"进货渠道:"+na);
}
con.close();
}
catch(SQLException qe)
{
System.out.println(qe);
}
card.show(pCenter,"显示");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -