📄 example10_6.java
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
class DataWindow extends JFrame implements ActionListener
{ JTextField 待查商品名称_text,商品价格_text,
更新商品名称_text,更新商品价格_text,
新增商品名称_text,新增商品价格_text;
JButton 查询按扭,更新按扭,新增按钮;
int 查询记录=0;
Connection Con=null;
Statement Stmt=null;
String conURL="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=TestDB.mdb";
DataWindow()
{ super("简易商品管理系统");
setBounds(150,150,300,120);
setVisible(true);setLayout(new GridLayout(3,1));
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){System.out.println(e.getMessage());}
try{
Con=DriverManager.getConnection(conURL);
Stmt=Con.createStatement();
}
catch(SQLException ee) {System.out.println(ee.getMessage());}
待查商品名称_text=new JTextField(10);
商品价格_text=new JTextField(10);
更新商品名称_text=new JTextField(10);
更新商品价格_text=new JTextField(10);
新增商品名称_text=new JTextField(10);
新增商品价格_text=new JTextField(10);
查询按扭=new JButton("查询");
更新按扭=new JButton("更新");
新增按钮=new JButton("填加");
JPanel p1=new JPanel(),p2=new JPanel(),p3=new JPanel();
p1.add(new JLabel("查询的商品名称:"));
p1.add( 待查商品名称_text);
p1.add(new JLabel("显示商品价格:"));
p1.add(商品价格_text);
p1.add(查询按扭);
p2.add(new JLabel("输入商品名称:"));
p2.add( 更新商品名称_text);
p2.add(new JLabel("输入调整价格:"));
p2.add(更新商品价格_text);
p2.add(更新按扭);
p3.add(new JLabel("输入新增商品名称:"));
p3.add( 新增商品名称_text);
p3.add(new JLabel("输入商品价格:"));
p3.add(新增商品价格_text);
p3.add(新增按钮);
add(p1);add(p2);add(p3);
查询按扭.addActionListener(this);
更新按扭.addActionListener(this);
新增按钮.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0); } } );
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==查询按扭)
{ 查询记录=0;
try{ 查询();}
catch(SQLException ee) {System.out.println(ee.getMessage());}
}
else if(e.getSource()==更新按扭)
{ try{ 更新();}
catch(SQLException ee) {System.out.println(ee.getMessage());}
}
else if(e.getSource()==新增按钮)
{ try{ 新增();}
catch(SQLException ee) {System.out.println(ee.getMessage());}
}
}
public void 查询() throws SQLException
{ String cname,ename;
Con=DriverManager.getConnection(conURL);
ResultSet rs=Stmt.executeQuery("SELECT * FROM 商品表 ");
while (rs.next())
{ ename=rs.getString("品名"); cname=rs.getString("单价");
if(ename.equals( 待查商品名称_text.getText().trim()))
{ 商品价格_text.setText(cname);查询记录=1;
break;
}
}
Con.close();
if(查询记录==0)
{ 商品价格_text.setText("没有该商品"); }
}
public void 更新() throws SQLException
{ String s1="'"+更新商品名称_text.getText().trim()+"'",
s2="'"+更新商品价格_text.getText().trim()+"'";
String temp="UPDATE 商品表 SET 单价 ="+s2+" WHERE 品名 = "+s1 ;
Con=DriverManager.getConnection(conURL);
Stmt.executeUpdate(temp); Con.close();
}
public void 新增() throws SQLException
{ String s1="'"+新增商品名称_text.getText().trim()+"'",
s2="'"+新增商品价格_text.getText().trim()+"'";
String temp="INSERT INTO 商品表 VALUES ("+s1+","+s2+")";
Con=DriverManager.getConnection(conURL);
Stmt.executeUpdate(temp);
Con.close();
}
}
public class Example10_6
{ public static void main(String args[])
{
DataWindow window=new DataWindow();
window.validate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -