18 package org.turro.elephant.db;
20 import java.io.PrintWriter;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Collection;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IElement;
31 import org.turro.elephant.layout.IManageable;
39 protected Connection
conn =
null;
40 protected int lastFirst, lastCurrent, lastCount, totalCount;
51 if(
conn ==
null ||
conn.isClosed()) {
52 String driverClass = ((
IManageable)
element).getAttributes().getAttributeValue(
"attrib:jdbc-driver",
null),
53 url = ((
IManageable)
element).getAttributes().getAttributeValue(
"attrib:jdbc-url",
null),
54 user = ((
IManageable)
element).getAttributes().getAttributeValue(
"attrib:jdbc-user",
null),
55 password = ((
IManageable)
element).getAttributes().getAttributeValue(
"attrib:jdbc-password",
null);
56 if(driverClass !=
null && url !=
null) {
59 url = url.replaceAll(
"\\#ctx",
64 url = url.replaceAll(
"\\\\",
"/");
66 Class.forName(driverClass);
67 if(user !=
null && user.length() > 0 && password !=
null) {
68 conn = DriverManager.getConnection(url, user, password);
71 conn = DriverManager.getConnection(url);
74 }
catch (ClassNotFoundException | SQLException ex) {
84 public boolean positionFirst(ResultSet rs,
int first)
throws SQLException {
86 boolean hasNext =
true;
88 while((current < first) && hasNext) {
95 public boolean iterate(ResultSet rs,
int count,
int current)
throws SQLException {
97 lastCurrent = current;
104 public int findLast(ResultSet rs)
throws SQLException {
105 totalCount = lastCurrent;
112 public ResultSet
query(String sql)
throws SQLException {
114 return con.createStatement().executeQuery(sql);
117 public ResultSet
query(String sql, Collection params)
throws SQLException {
118 return query(sql, params.toArray());
121 public ResultSet
query(String sql, Object[] params)
throws SQLException {
126 PreparedStatement ps = con.prepareStatement(sql);
127 for(
int i = 0; i < params.length; i++) {
128 if(params[i] !=
null) {
129 ps.setObject(i + 1, params[i]);
132 ps.setNull(i + 1, java.sql.Types.VARCHAR);
135 return ps.executeQuery();
138 public int execute(String sql)
throws SQLException {
140 return con.createStatement().executeUpdate(sql);
143 public int execute(String sql, Collection params)
throws SQLException {
144 return execute(sql, params.toArray());
147 public int execute(String sql, Object[] params)
throws SQLException {
152 PreparedStatement ps = con.prepareStatement(sql);
153 for(
int i = 0; i < params.length; i++) {
154 if(params[i] !=
null) {
155 ps.setObject(i + 1, params[i]);
158 ps.setNull(i + 1, java.sql.Types.VARCHAR);
161 return ps.executeUpdate();
164 public void execute(String commands[])
throws SQLException {
165 for (String command : commands) {
170 public void close(ResultSet rs)
throws SQLException {
174 public void close() throws SQLException {
175 if(
conn !=
null && !
conn.isClosed())
180 if(totalCount <= lastCount)
return;
181 out.print(
"<div class='dbnav'>");
185 out.print(
"<a href='?" + param +
"=" + (
lastFirst - lastCount) +
"'>");
186 out.print(
"<<");
190 int iter = (int)Math.ceil(((
double)totalCount) / ((double)lastCount));
191 for(
int i = 0; i < iter; i++) {
193 out.print(
"<li class='active'>");
198 out.print(
"<a href='?" + param +
"=" + (i * lastCount) +
"'>");
205 out.print(
"<a href='?" + param +
"=" + (
lastFirst + lastCount) +
"'>");
206 out.print(
">>");
214 public java.util.Date
toDate(java.sql.Timestamp date) {
215 if(date ==
null)
return null;
216 return new java.util.Date(date.getTime());
220 if(date ==
null)
return null;
221 return new java.sql.Timestamp(date.getTime());
224 public java.util.Date
toDate(java.sql.Date date) {
225 if(date ==
null)
return null;
226 return new java.util.Date(date.getTime());
230 if(date ==
null)
return null;
231 return new java.sql.Date(date.getTime());
static String getRealPath(String path)
static String logMsg(String msg)
void execute(String commands[])
ElementDB(IElement element)
void renderNavigator(PrintWriter out, String param)
ResultSet query(String sql, Collection params)
java.sql.Timestamp toTimestamp(java.util.Date date)
ResultSet query(String sql)
boolean positionFirst(ResultSet rs, int first)
boolean iterate(ResultSet rs, int count, int current)
int execute(String sql, Object[] params)
Connection getConnection()
java.util.Date toDate(java.sql.Date date)
ResultSet query(String sql, Object[] params)
java.sql.Date toSqlDate(java.util.Date date)
java.util.Date toDate(java.sql.Timestamp date)
int findLast(ResultSet rs)
int execute(String sql, Collection params)