package market; import java.awt.*;
import java.awt.event.*; import java.sql.*;
class JieMian extends Frame implements ActionListener { Button button1,button2,button3,button4,button5, button6,button7,button8,button9; TextField text,text1,text2,text3,text4; TextArea text5; Label label,label1,label2,label3; JieMian(String s) {
super(s); setLayout(new FlowLayout()); setBounds(100,100,500,400); label=new Label(\"编号\"); label1=new Label(\"品名\"); label2=new Label(\"数量\"); label3=new Label(\"单价\"); button1=new Button(\"添加\"); button2=new Button(\"删除\"); button3=new Button(\"定位\"); button4=new Button(\"清空\"); button5=new Button(\"付款\"); button6=new Button(\"清单\"); button7=new Button(\"清屏\"); button8=new Button(\"第一件商品\"); button9=new Button(\"最后一件商品\"); text=new TextField(10); text1=new TextField(10); text2=new TextField(10); text3=new TextField(10); text4=new TextField(10); text5=new TextArea(); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this);
add(label); add(text); add(label1); add(text1); add(label2); add(text2); add(label3); add(text3); add(button1); add(button2); add(button3); add(text4); add(button4); add(button5); add(button6); add(button7); add(button8); add(button9); add(text5); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); validate(); } public void actionPerformed(ActionEvent e) {
Connection cn = null; Statement stmt = null; ResultSet rs=null; double sum=0;
String url=\"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Market\"; String driverName=\"com.microsoft.jdbc.sqlserver.SQLServerDriver\"; //数据库 try { Class.forName(driverName); } catch(ClassNotFoundException ex) { System.out.println(\"加载驱动失败\");
ex.printStackTrace(); } try { cn=DriverManager.getConnection(url,\"sa\ stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(e.getSource()==button1) { String count=text.getText(); String name=text1.getText(); String num=text2.getText(); String price=text3.getText(); String s=null;
stmt.executeUpdate(\"insert into client(Count,Name,Num,Price)values('\"+count+\"','\"+name+\"','\"+num+\"','\"+price+\"')\"); s=\"\"+count+\"添加纪录\"+name+num+\"斤\"+\",单价\"+price+\"元\"; text5.append(s+'\\n'); }
if(e.getSource()==button2) {
String count=text.getText(); String s;
stmt.executeUpdate(\"delete from client where Count='\"+count+\"'\"); s=\"删除\"+count; text5.append(s+'\\n'); }
if(e.getSource()==button3) {
rs=stmt.executeQuery(\"SELECT * FROM client\"); int row=Integer.parseInt(text4.getText()); rs.absolute(row);
String count=rs.getString(\"Count\"); String name=rs.getString(\"Name\"); String num=rs.getString(\"Num\"); String price=rs.getString(\"Price\"); String s=\"定位到\"+count+\"客户购买\"+name+num+\"斤\"+\",单价\"+price+\"元\";;
text5.append(s+'\\n'); }
if(e.getSource()==button4) { String s=null; text.setText(s); text1.setText(s); text2.setText(s); text3.setText(s); text4.setText(s); }
if(e.getSource()==button5) { sum=Integer.parseInt(text2.getText())*Integer.parseInt(text3.getText()); String s=\"共需付\"+sum+\"元\"; text5.append(s+'\\n'); }
if(e.getSource()==button6) { String count=text.getText(); String name=text1.getText(); String num=text2.getText(); String price=text3.getText(); String s=\"购物纪录:\"+count+\"购买\"+name+num+\"斤\"+\",单价\"+price+\"元\"+'\\n'+\"共需付\"+sum+\"元\";
text5.append(s+'\\n'); } if(e.getSource()==button7) { String s=null; text.setText(s); text1.setText(s); text2.setText(s); text3.setText(s); text4.setText(s); text5.setText(s); } if(e.getSource()==button8) { rs=stmt.executeQuery(\"SELECT * FROM client\"); rs.next();
String count=rs.getString(\"Count\"); String name=rs.getString(\"Name\"); String num=rs.getString(\"Num\"); String price=rs.getString(\"Price\");
String s=\"第一个记录:\"+count+name+num+\"斤\"+\",单价\"+price+\"元\";
text5.append(s+'\\n'); } if(e.getSource()==button9) { rs=stmt.executeQuery(\"SELECT * FROM client\");
rs.last();
String count=rs.getString(\"Count\"); String name=rs.getString(\"Name\"); String num=rs.getString(\"Num\"); String price=rs.getString(\"Price\");
String s=\"最后一个记录:\"+count+name+num+\"斤\"+\",单价\"+price+\"元\";
text5.append(s+'\\n'); }
//rs.close(); stmt.close(); cn.close(); }
catch(Exception ex) {
ex.printStackTrace();
System.out.println(\"连接失败\"); } } public static void main(String[] args) { new JieMian(\"超市管理系统\"); } }