小程序

来源:互联网 发布:如何精通c语言 编辑:程序博客网 时间:2024/04/29 20:58
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;

class Zll extends JFrame
{
JPanel panel1;
JPanel panel2;

JTextArea textArea;
JTextField textField;

JButton button1;  
JButton button2;
JButton button3;
JButton button4;

JLabel label1;
JLabel label2;

ImageIcon icon;

ZllAction action;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Zll()
{
panel1=new JPanel();
panel1.setLayout(null);
//panel1.setBackground(Color.red);
panel1.setBounds(0,0,600,100);

panel2=new JPanel();
panel2.setLayout(null);
panel2.setBackground(Color.blue);
panel2.setBounds(0,100,150,500);

icon=new ImageIcon("校外.jpg");
button1=new JButton(icon);
button1.setBounds(300,50,150,50);

button2=new JButton("查询");
button2.setBounds(15,50,80,20);

label1=new JLabel(new ImageIcon("4.jpg"));
label1.setBounds(0,0,600,100);

label2=new JLabel("请输入信息");
label2.setForeground(Color.yellow);
label2.setBounds(15,100,80,20);

textField=new JTextField(20);
textField.setBounds(10,150,100,20);

button3=new JButton("清理");
button3.setBounds(15,200,80,20);

button4=new JButton("退出");
button4.setBounds(520,70,80,30);

textArea=new JTextArea(10,5);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setBounds(150,100,450,500);

panel1.add(label1);
panel1.add(button1);
panel1.add(button4);

panel2.add(button2);
panel2.add(label2);
panel2.add(textField);
panel2.add(button3);

this.setLayout(null);
this.add(panel1);
this.add(panel2);

this.add(textArea);
}

void atob()
{
action=new ZllAction(textField,textArea,button2,button3,button4);
button2.addActionListener(action);
button3.addActionListener(action);
button4.addActionListener(action);
}

public static void main(String[] args) 
{
Zll zll=new Zll();
zll.setBounds(100,100,600,600);
zll.setVisible(true);
zll.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
zll.atob();
}
}
class ZllAction implements ActionListener 
{
JTextField textField; 
JTextArea textArea;
JButton button2;
JButton button3;
JButton button4;

ZllAction(JTextField textField,JTextArea textArea,JButton button2,JButton button3,JButton button4)
{
this.textField=textField;
this.textArea=textArea;
this.button2=button2;
this.button3=button3;
this.button4=button4;
}

public void actionPerformed(ActionEvent event)
{

JButton object=(JButton)event.getSource();

if(object==button2)
{
System.out.println(textField.getText());
Shu shu=new Shu(textArea);
try{
shu.setResult(textField.getText());
}catch(SQLException e){}
}

else if(object==button3)
{
textArea.setText("");
}
else if(object==button4)
System.exit(0);
}

}

class Shu
{  

JTextArea textArea;
private Connection con;
private Statement stat;
private ResultSet re;
String url="jdbc:mysql://localhost:3306/test";

Shu(JTextArea textArea)
{
this.textArea=textArea;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,"root","123");
stat=con.createStatement();
}
catch (ClassNotFoundException e)
{
e.getMessage();
}catch(SQLException ee)                                          
{
ee.printStackTrace();
}
}

void setResult(String text) throws SQLException
{
System.out.println(text);
 
try
{
String statement="select *from" +" "+text;
re=stat.executeQuery(statement);

while(re.next())
{
textArea.append(re.getString(1));
}
}
catch (SQLException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null,"sorry"+" "+"没有找到你要的内容");
}
finally{
stat.close();
con.close();
}

}
}