Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3.  
  4. public interface ServerConnector
  5. {
  6. Connection getConnection();
  7. String getConnectionURL();
  8. String getConnectionDetails();
  9. String getTablesSchemaQuery();
  10. String getDBName();
  11. String getServername();
  12. String getUserName();
  13. }
  14. ------------
  15.  
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.SQLException;
  19.  
  20.  
  21.  
  22. public class OracleServerConnector implements ServerConnector {
  23. private final String servidor ="jdbc:oracle:thin:@localhost:1521:xe";
  24. private String user= null;
  25. private String password=null;
  26.  
  27. public OracleServerConnector(){}
  28.  
  29. public OracleServerConnector(String user, String password)
  30. {
  31. this.user = user;
  32. this.password = password;
  33. try {
  34. DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
  35.  
  36.  
  37. } catch (SQLException e) {
  38.  
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44.  
  45. @Override
  46. public Connection getConnection() {
  47. Connection conn = null;
  48. try {
  49. conn = DriverManager.getConnection
  50. (servidor, user, password);
  51. } catch (SQLException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. return conn;
  56. }
  57.  
  58. @Override
  59. public String getConnectionURL() {
  60. return String.format("Conexion a ORACLEdb : %s como %s identificado por %s", servidor, user, password);
  61. }
  62.  
  63. @Override
  64. public String getConnectionDetails() {
  65. String texto= "Nada que ver aqui";
  66. return texto;
  67. }
  68.  
  69. @Override
  70. public String getTablesSchemaQuery() {
  71. return String.format("select * from user_tables");
  72.  
  73. }
  74.  
  75. @Override
  76. public String getDBName() {
  77. return user;
  78. }
  79.  
  80. @Override
  81. public String getServername() {
  82.  
  83. return servidor;
  84. }
  85.  
  86. @Override
  87. public String getUserName() {
  88. return user;
  89.  
  90. }
  91.  
  92. }
  93. ---------------
  94.  
  95. import java.sql.Connection;
  96. import java.sql.PreparedStatement;
  97. import java.sql.ResultSet;
  98. import java.sql.SQLException;
  99. import java.sql.Statement;
  100.  
  101. public class Manager {
  102. Connection con = null;
  103. ServerConnector scb = null;
  104. public Manager(){}
  105.  
  106. public Manager(ServerConnector con)
  107. {
  108. this.scb = con;
  109. }
  110. public boolean setConnectionBehavior(ServerConnector connector)
  111. {
  112. if (connector == null)
  113. {
  114. throw new IllegalArgumentException("Server Connector cannot be null!");
  115. }
  116. scb = connector;
  117. return true;
  118. }
  119. public boolean openConnection()
  120. {
  121. try
  122. {
  123. if (scb == null) throw new IllegalStateException ("Cannot open connection without a defined connection behavior!");
  124.  
  125. if (con != null) closeConnection(false);
  126. con = scb.getConnection();
  127. }
  128. catch (Exception ex)
  129. {
  130.  
  131. ex.printStackTrace();
  132. return false;
  133. }
  134.  
  135. if (con == null) return false;
  136. return true;
  137. }
  138. public boolean closeConnection(boolean keepAlive)
  139. {
  140. try
  141. {
  142. //if not null and open
  143. if (con != null)
  144. {
  145. if (!con.isClosed())
  146. {
  147. //close it
  148. con.close();
  149. }
  150. }
  151. if (!keepAlive)
  152. {
  153. con = null;
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158.  
  159. ex.printStackTrace();
  160. return false;
  161. }
  162. return true;
  163. }
  164. public boolean isConnected()
  165. {
  166. return con != null;
  167. }
  168. public boolean ExecuteNonQuery(String query)
  169. {
  170. try
  171. {
  172. Statement st = null;
  173. st = con.createStatement();
  174. int i = st.executeUpdate(query);
  175. if (i == -1){
  176. System.out.println("db error : " + query);
  177. return false;
  178. }
  179. st.close();
  180. }
  181. catch (Exception ex)
  182. {
  183.  
  184. ex.printStackTrace();
  185. return false;
  186. }
  187. return true;
  188. }
  189. public ResultSet ExecuteQuery(String query)
  190. {
  191. ResultSet rs = null;
  192. try {
  193. Statement stmt = con.createStatement();
  194. rs = stmt.executeQuery(query);
  195. } catch (SQLException e) {
  196. // TODO Auto-generated catch block
  197. e.printStackTrace();
  198. }
  199.  
  200. return rs;
  201.  
  202. }
  203.  
  204. public ResultSet ExecuteRSGetTableSchema()
  205. {
  206. try
  207. {
  208. PreparedStatement pst = con.prepareStatement(GetTablesSchemaQuery());
  209. return pst.executeQuery();
  210. }
  211. catch (SQLException e)
  212. {
  213. // TODO Add better logging
  214. e.printStackTrace();
  215. }
  216. return null;
  217. }
  218. public Connection GetConnection()
  219. {
  220. return con;
  221. }
  222. public String GetConnectionURL()
  223. {
  224. return scb.getConnectionURL();
  225. }
  226. public String GetTablesSchemaQuery()
  227. {
  228. return scb.getTablesSchemaQuery();
  229. }
  230. public String GetDBName()
  231. {
  232. return scb.getDBName();
  233. }
  234.  
  235. /**
  236. * Get the name of the server.
  237. * @return name of the server.
  238. */
  239. public String GetServerName()
  240. {
  241. return scb.getServername();
  242. }
  243.  
  244. /**
  245. * Get the name of the user.
  246. * @return name of the user.
  247. */
  248. public String GetUserName()
  249. {
  250. return scb.getUserName();
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement