1 package pl.edu.pjwstk.iab.zad3.ejb;
2
3 import java.rmi.RemoteException;
4 import java.sql.Connection;
5 import java.sql.ResultSet;
6 import java.sql.ResultSetMetaData;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.ArrayList;
10import java.util.HashMap;
11
12import javax.ejb.CreateException;
13import javax.ejb.EJBException;
14import javax.ejb.SessionBean;
15import javax.ejb.SessionContext;
16import javax.naming.InitialContext;
17import javax.naming.NamingException;
18import javax.sql.DataSource;
19
20
35public abstract class TableBean implements SessionBean {
36
37 private SessionContext _ctx = null;
38
39 private static final String JNDI_DB_NAME = "java:comp/env/jdbc/Cloudscape";
40
41
46 public void setSessionContext(SessionContext ctx) throws EJBException,
47 RemoteException {
48 _ctx = ctx;
49 }
50
51 public void unsetSessionContext() throws EJBException, RemoteException {
52 _ctx = null;
53 }
54
55
60 public void ejbCreate() throws CreateException {
61 }
62
63
68 public ArrayList getTablesName() {
69 Connection con = null;
70 DataSource ds = null;
71 Statement st = null;
72 ResultSet rs = null;
73
74 ArrayList tables = new ArrayList();
75
76 try {
77 ds = (DataSource) new InitialContext().lookup(JNDI_DB_NAME);
78 con = ds.getConnection();
79 st = con.createStatement();
80 rs = st.executeQuery("SELECT tablename FROM sys.systables WHERE tabletype='S'");
81 while (rs.next()) {
82 tables.add(rs.getString("tablename"));
83 }
84 } catch (NamingException e) {
85 e.printStackTrace();
86 } catch (SQLException e) {
87 e.printStackTrace();
88 } finally {
89 if (con != null) {
90 try {
91 rs.close();
92 st.close();
93 con.close();
94 } catch (SQLException e1) {
95 e1.printStackTrace();
96 }
97 }
98 }
99
00 return tables;
01 }
02
03
08 public ArrayList trescTabeli(String nazwaTabeli) {
09 Connection con = null;
10 DataSource ds = null;
11 Statement st = null;
12 ResultSet rs = null;
13 ResultSetMetaData rsmd = null;
14 ArrayList list = new ArrayList();
15
16 try {
17 ds = (DataSource) new InitialContext().lookup(JNDI_DB_NAME);
18 con = ds.getConnection();
19 st = con.createStatement();
20 rs = st.executeQuery("SELECT * FROM sys." + nazwaTabeli);
21 rsmd = rs.getMetaData();
22
23 int columnCount = rsmd.getColumnCount();
24
25 if (columnCount > 0) {
26
27 while (rs.next()) {
28
29 HashMap hm = new HashMap();
30 for (int i = 1; i <= rsmd.getColumnCount(); i++) {
31 String name = rsmd.getColumnLabel(i).toUpperCase();
32 Object val = rs.getObject(i);
33
34 if (rs.wasNull()) {
35 val = null;
36 }
37 hm.put(name, val);
38 }
39 list.add(hm);
40 }
41
42 }
43 } catch (NamingException e) {
44 e.printStackTrace();
45 } catch (SQLException e) {
46 e.printStackTrace();
47 }
48 return list;
49 }
50
51}