您的位置:首页 > 数据库

一个通用的SQL客户程序

2013-01-10 11:54 176 查看
能够与任何关系数据库建立连接,并且交互地执行SQL命令,该客户程序能够连接任何jdbc数据源,也可以提交要执行的sqlSELECT命令和非SELECT命令。对于SELECT查询命令,显示执行结果;对于非select命令,显示执行状态。(我的电脑上只有mysql,所以只测了mysql,没有问题)

如下图所示:



代码实现:

1 import java.awt.*;
2 import java.awt.event.ActionEvent;
3 import java.awt.event.ActionListener;
4 import java.sql.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8
9 public class SQLClient extends JApplet{
10     //连接数据库
11     private Connection connection;
12     //执行sql命令
13     private Statement statement;
14     //输入sql命令 的文本框
15     private JTextArea jtaSQLCommand=new JTextArea();
16     //显示sql命令的执行结果的文本框
17     private JTextArea jtaSQLResult=new JTextArea();
18     //连接数据库的JDBC相关信息,包括用户名,密码,数据库url,数据库驱动器
19     JTextField jtfUserName=new JTextField();
20     JPasswordField jpfPassword=new JPasswordField();
21     JComboBox jcboURL=new JComboBox(new String[]{
22             "jdbc:mysql://localhost/world",
23             "jdbc:odbc:exampleMDBDataSource",
24             "jdbc:oracle:thin:@liang.armstrong.edu:1521:orcl"
25     });
26     JComboBox jcboDriver = new JComboBox(new String[]{
27             "com.mysql.jdbc.Driver",
28             "sun.jdbc.odbc.jdbcOdbcDriver",
29             "oracle.jdbc.driver.OracleDriver"
30     });
31
32     JButton jbtExecuteSQL =new JButton("Execute SQL Command");
33     JButton jbtClearSQLCommand=new JButton("Clear");
34     JButton jbtConnectDB1=new JButton("Connect to DataBase");
35     JButton jbtClearSQLResult=new JButton("Clear Result");
36
37     //create titled borders
38     Border titledBorder1=new TitledBorder("Enter an SQL Command");
39     Border titledBorder2=new TitledBorder("SQL Execute Result");
40     Border titledBorder3=new TitledBorder("Enter Database Information");
41
42     JLabel jlblConnectionStatus=new JLabel("No connection now");
43
44     public void init(){
45         JScrollPane jScrollPane1=new JScrollPane(jtaSQLCommand);
46         jScrollPane1.setBorder(titledBorder1);
47         JScrollPane jScrollPane2=new JScrollPane(jtaSQLResult);
48         jScrollPane2.setBorder(titledBorder2);
49
50         JPanel jPanel1=new JPanel(new FlowLayout(FlowLayout.RIGHT));
51         jPanel1.add(jbtClearSQLCommand);
52         jPanel1.add(jbtExecuteSQL);
53
54         JPanel jPanel2=new JPanel();
55         jPanel2.setLayout(new BorderLayout());
56         jPanel2.add(jScrollPane1,BorderLayout.CENTER);
57         jPanel2.add(jPanel1,BorderLayout.SOUTH);
58         jPanel2.setPreferredSize(new Dimension(100,100));
59
60         JPanel jPanel3=new JPanel();
4000

61         jPanel3.setLayout(new BorderLayout());
62         jPanel3.add(jlblConnectionStatus,BorderLayout.CENTER);
63         jPanel3.add(jbtConnectDB1,BorderLayout.EAST);
64
65         JPanel jPanel4=new JPanel();
66         jPanel4.setLayout(new GridLayout(4,1,10,5));
67         jPanel4.add(jcboDriver);
68         jPanel4.add(jcboURL);
69         jPanel4.add(jtfUserName);
70         jPanel4.add(jpfPassword);
71
72         JPanel jPanel5=new JPanel();
73         jPanel5.setLayout(new GridLayout(4,1,10,5));
74         jPanel5.add(new JLabel("JDBC Driver"));
75         jPanel5.add(new JLabel("Database URL"));
76         jPanel5.add(new JLabel("UserName"));
77         jPanel5.add(new JLabel("Password"));
78
79         JPanel jPanel6 = new JPanel();
80         jPanel6.setLayout(new BorderLayout());
81         jPanel6.setBorder(titledBorder3);
82         jPanel6.add(jPanel4,BorderLayout.CENTER);
83         jPanel6.add(jPanel5,BorderLayout.WEST);
84
85         JPanel jPanel7 = new JPanel();
86         jPanel7.setLayout(new BorderLayout());
87         jPanel7.add(jPanel3,BorderLayout.SOUTH);
88         jPanel7.add(jPanel6,BorderLayout.CENTER);
89
90         JPanel jPanel8 = new JPanel();
91         jPanel8.setLayout(new BorderLayout());
92         jPanel8.add(jPanel2,BorderLayout.CENTER);
93         jPanel8.add(jPanel7,BorderLayout.WEST);
94
95         JPanel jPanel9=new JPanel(new FlowLayout(FlowLayout.LEFT));
96         jPanel9.add(jbtClearSQLCommand);
97
98         jcboURL.setEditable(true);
99         jcboDriver.setEditable(true);
100
101         add(jPanel8,BorderLayout.NORTH);
102         add(jScrollPane2,BorderLayout.CENTER);
103         add(jPanel9,BorderLayout.SOUTH);
104         //点击execute按钮执行sql语句
105         jbtExecuteSQL.addActionListener(new ActionListener(){
106             public void actionPerformed(ActionEvent arg0) {
107                 executeSQL();
108             }
109         });
110         //连接数据库
111         jbtConnectDB1.addActionListener(new ActionListener(){
112             public void actionPerformed(ActionEvent e) {
113                 // TODO Auto-generated method stub
114                 connectToDB();
115             }
116         });
117         //中兴清除命令的
118         jbtClearSQLCommand.addActionListener(new ActionListener(){
119             public void actionPerformed(ActionEvent e) {
120                 jtaSQLCommand.setText(null);
121             }
122         });
123
124         jbtClearSQLResult.addActionListener(new ActionListener(){
125             public void actionPerformed(ActionEvent e){
126                 jtaSQLResult.setText(null);
127             }
128         });
129     }
130
131     private void connectToDB(){
132         String driver=(String)jcboDriver.getSelectedItem();
133         String url=(String)jcboURL.getSelectedItem();
134         String userName=jtfUserName.getText().trim();
135         String password=new String(jpfPassword.getPassword());
136
137         //连接数据库
138         try{
139             Class.forName(driver);
140             connection=DriverManager.getConnection(url,userName,password);
141             jlblConnectionStatus.setText("Connected to "+url);
142         }catch(java.lang.Exception ex){
143             ex.printStackTrace();
144         }
145     }
146
147     private void executeSQL(){
148         if(connection==null){
149             jtaSQLResult.setText("Please connect to a database first");
150             return;
151         }else{
152             String sqlCommands=jtaSQLCommand.getText().trim();
153             //trim返回字符串的副本,忽略前导空白和尾部空白。
154             String[] commands=sqlCommands.replace('\n', ' ').split(";");
155
156             for(String aCommand: commands){
157                 if(aCommand.trim().toUpperCase().startsWith("SELECT")){
158                     //是否以前缀SELECT开始
159                     processSQLSelect(aCommand);
160                 }
161                 else{
162                     processSQLNonSelect(aCommand);
163                 }
164             }
165         }
166     }
167 //执行选择命令
168     private void processSQLSelect(String sqlCommand) {
169
170         try{
171             statement = connection.createStatement();
172             ResultSet resultSet=statement.executeQuery(sqlCommand);
173
174             int columnCount=resultSet.getMetaData().getColumnCount();
175             String row=" ";
176             for(int i=1;i<=columnCount;++i)
177                 row+=resultSet.getMetaData().getColumnName(i)+"\t";
178
179             jtaSQLResult.append(row+"\n");
180             while(resultSet.next()){
181                 row=" ";
182                 for(int i=1;i<=columnCount;++i)
183                     row+=resultSet.getString(i)+"\t";
184                 jtaSQLResult.append(row+'\n');
185             }
186         }catch(SQLException ex){
187             jtaSQLResult.setText(ex.toString());
188         }
189     }
190     //执行非选择命令
191     private void processSQLNonSelect(String sqlCommand) {
192         try{
193             statement=connection.createStatement();
194             statement.executeUpdate(sqlCommand);
195             jtaSQLResult.setText("SQL command excuted");
196         }catch(SQLException ex){
197             jtaSQLResult.setText(ex.toString());
198         }
199     }
200
201 }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: