KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sqlnavigator > SqlService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.sqlnavigator;
20
21 import java.sql.*;
22 import java.util.*;
23
24 import org.lucane.common.*;
25 import org.lucane.common.net.ObjectConnection;
26 import org.lucane.server.*;
27 import org.lucane.server.database.*;
28
29
30 public class SqlService
31 extends Service
32 {
33     private DatabaseAbstractionLayer layer;
34     
35     public SqlService()
36     {
37     }
38     
39     public void init(Server parent)
40     {
41         this.layer = parent.getDBLayer();
42     }
43     
44     
45     public void process(ObjectConnection oc, Message message)
46     {
47         SqlAction sa = (SqlAction)message.getData();
48         switch(sa.action)
49         {
50             case SqlAction.GET_DRIVER_INFO:
51                 getDriverInfo(oc);
52             break;
53             case SqlAction.GET_TABLE_NAMES:
54                 getTableNames(oc);
55             break;
56             case SqlAction.EXECUTE_QUERY:
57                 executeQuery(oc, sa.params);
58             break;
59         }
60     }
61     
62     
63     
64     private void executeQuery(ObjectConnection oc, Object JavaDoc object)
65     {
66         try
67         {
68             SqlResult result = new SqlResult();
69             
70             Connection c = layer.getConnection();
71             Statement stm = c.createStatement();
72             ResultSet rs = stm.executeQuery((String JavaDoc)object);
73             
74             try {
75                 //-- column names
76
ResultSetMetaData meta = rs.getMetaData();
77                 int cols = meta.getColumnCount();
78                 for(int i=0;i<cols;i++)
79                     result.addColumn(meta.getColumnLabel(i+1));
80                 
81                 //-- data
82
while(rs.next())
83                 {
84                     Vector line = new Vector();
85                     
86                     for(int i=0;i<cols;i++)
87                         line.add(rs.getString(i+1));
88                     
89                     result.addLine(line);
90                 }
91             } catch(SQLException sqle) {
92                 //probably not a select statement
93
}
94             
95             oc.write(result);
96             c.close();
97         }
98         catch(Exception JavaDoc e)
99         {
100             e.printStackTrace();
101         }
102     }
103     
104     
105     
106     private void getTableNames(ObjectConnection oc)
107     {
108         Vector data = new Vector();
109         
110         try
111         {
112             Connection connection = layer.getConnection();
113             DatabaseMetaData dbmd = connection.getMetaData();
114             String JavaDoc[] types = {"TABLE", "VIEW"};
115             ResultSet rs = dbmd.getTables(null,null,null,types);
116             
117             //field n3 is the table name
118
while(rs.next())
119                 data.addElement(rs.getString(3));
120             
121             rs.close();
122             connection.close();
123             oc.write(data);
124         }
125         catch(Exception JavaDoc e)
126         {
127             e.printStackTrace();
128         }
129     }
130     
131     
132     private void getDriverInfo(ObjectConnection oc)
133     {
134         try
135         {
136             Connection connection = layer.getConnection();
137             DatabaseMetaData dbmd = connection.getMetaData();
138             String JavaDoc info = dbmd.getDriverName() + " " + dbmd.getDriverVersion();
139             connection.close();
140             
141             oc.write(info);
142         } catch(Exception JavaDoc e) {
143             e.printStackTrace();
144         }
145     }
146 }
147
Popular Tags