首页 > 代码库 > JDBC

JDBC

 

 1 package db;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 import java.io.FileInputStream;
 7 import java.sql.Connection;
 8 import java.sql.DriverManager;
 9 import java.sql.Statement;
10 import java.util.Properties;
11 
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.JTextField;
18 
19 public class QueryExecutor {
20     JFrame jf = new JFrame("查询执行器");
21     private JScrollPane scrollPane;
22     private JButton execBn = new JButton("查询");
23     //用来输入查询语句的文本框
24     private JTextField sqlField = new JTextField(45);
25     private static Connection conn;
26     private static Statement stmt;
27     //用静态初始化块来初始化Connection和Statement对象
28     static {
29         try {
30             //用Properties类加载属性文件
31             Properties prop = new Properties();
32             prop.load(new FileInputStream("mysql.ini"));
33             String drivers = prop.getProperty("driver");
34             String url = prop.getProperty("url");
35             String user = prop.getProperty("user");
36             String pass = prop.getProperty("pass");
37             Class.forName(drivers);
38             conn = DriverManager.getConnection(url, user, pass);
39             stmt = conn.createStatement();
40         } catch (Exception e) {
41             e.printStackTrace();
42         }
43     }
44     //初始化界面
45     public void init() {
46         JPanel top = new JPanel();
47         top.add(new JLabel("输入查询语句"));
48         top.add(sqlField);
49         top.add(execBn);
50         //为执行按钮,单行文本框添加事件监听器
51         execBn.addActionListener(new ExceListener());
52         sqlField.addActionListener(new ExceListener());
53         jf.add(top, BorderLayout.NORTH);
54         jf.setSize(680, 480);
55         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
56         jf.setVisible(true);
57     }
58     //定义监听器
59     class ExceListener implements ActionListener {
60 
61         @Override
62         public void actionPerformed(ActionEvent evt) {
63             
64         }
65         
66     }
67 }

 

JDBC