1 package org.paneris.jal.controller;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.ResultSetMetaData;
6 import java.sql.Statement;
7 import java.util.Vector;
8
9 import org.paneris.jal.model.DBConnectionManager;
10 import org.paneris.user.model.User;
11 import org.webmacro.Template;
12 import org.webmacro.servlet.HandlerException;
13 import org.webmacro.servlet.PanerisPage;
14 import org.webmacro.servlet.WebContext;
15
16
17
18
19
20 public class POSQL extends PanerisPage {
21
22 private static final long serialVersionUID = 1L;
23 private DBConnectionManager connMgr = DBConnectionManager.getInstance();
24
25 public Template handle(WebContext context) throws HandlerException {
26 String db = (String)context.getForm("db");
27 context.put("db",db);
28 String templateName = null;
29 try {
30 templateName = User.checkLoggedIn("controller",this.getClass().getName(), context, " access the Database Administration pages.");
31 } catch (Exception e) {
32 throw new HandlerException("Could not get user:" + e.toString());
33 }
34 if (templateName == null) {
35 templateName = (String)context.getForm("wmtemplate");
36 if (templateName == null) templateName="jal/view/POSQL.wm";
37 context.put("query","");
38 context.put("exception","");
39 context.put("result","");
40 String query = (String)context.getForm("query");
41 if (query != null) {
42 context.put("query",query);
43 Connection conn = connMgr.getConnection("Admin",db);
44 try {
45 Statement s = conn.createStatement();
46 if (query.toLowerCase().startsWith("select")) {
47 ResultSet rs = s.executeQuery(query);
48 context.put("result","query done");
49 Vector results = new Vector();
50 Vector titles = null;
51 ResultSetMetaData meta = rs.getMetaData();
52 while (rs.next()) {
53 Vector result = new Vector();
54 if (titles==null) {
55 titles = new Vector();
56 for (int i=1; i< meta.getColumnCount()+1; i++) {
57 titles.addElement(meta.getColumnName(i));
58 }
59 }
60 for (int i=1; i< meta.getColumnCount()+1; i++) {
61 result.addElement(rs.getString(i));
62 }
63 results.addElement(result);
64 }
65 context.put("results",results);
66 context.put("titles",titles);
67 } else {
68 s.executeUpdate(query);
69 context.put("result","update done");
70 }
71 s.close();
72 connMgr.freeConnection(db,conn);
73 } catch (Exception e) {
74 context.put("exception",e.toString());
75 } finally {
76 connMgr.freeConnection(db, conn);
77 }
78 }
79 }
80
81 context.put("returnURL",context.getForm("returnURL"));
82 context.put("caption",context.getForm("caption"));
83
84
85 try {
86 return (Template) context.getBroker().get("template",templateName);
87 } catch (Exception e) {
88 throw new HandlerException("Could not locate template: " + templateName);
89 }
90 }
91
92 }