Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- public interface ServerConnector
- {
- Connection getConnection();
- String getConnectionURL();
- String getConnectionDetails();
- String getTablesSchemaQuery();
- String getDBName();
- String getServername();
- String getUserName();
- }
- ------------
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class OracleServerConnector implements ServerConnector {
- private final String servidor ="jdbc:oracle:thin:@localhost:1521:xe";
- private String user= null;
- private String password=null;
- public OracleServerConnector(){}
- public OracleServerConnector(String user, String password)
- {
- this.user = user;
- this.password = password;
- try {
- DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public Connection getConnection() {
- Connection conn = null;
- try {
- conn = DriverManager.getConnection
- (servidor, user, password);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return conn;
- }
- @Override
- public String getConnectionURL() {
- return String.format("Conexion a ORACLEdb : %s como %s identificado por %s", servidor, user, password);
- }
- @Override
- public String getConnectionDetails() {
- String texto= "Nada que ver aqui";
- return texto;
- }
- @Override
- public String getTablesSchemaQuery() {
- return String.format("select * from user_tables");
- }
- @Override
- public String getDBName() {
- return user;
- }
- @Override
- public String getServername() {
- return servidor;
- }
- @Override
- public String getUserName() {
- return user;
- }
- }
- ---------------
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Manager {
- Connection con = null;
- ServerConnector scb = null;
- public Manager(){}
- public Manager(ServerConnector con)
- {
- this.scb = con;
- }
- public boolean setConnectionBehavior(ServerConnector connector)
- {
- if (connector == null)
- {
- throw new IllegalArgumentException("Server Connector cannot be null!");
- }
- scb = connector;
- return true;
- }
- public boolean openConnection()
- {
- try
- {
- if (scb == null) throw new IllegalStateException ("Cannot open connection without a defined connection behavior!");
- if (con != null) closeConnection(false);
- con = scb.getConnection();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- return false;
- }
- if (con == null) return false;
- return true;
- }
- public boolean closeConnection(boolean keepAlive)
- {
- try
- {
- //if not null and open
- if (con != null)
- {
- if (!con.isClosed())
- {
- //close it
- con.close();
- }
- }
- if (!keepAlive)
- {
- con = null;
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean isConnected()
- {
- return con != null;
- }
- public boolean ExecuteNonQuery(String query)
- {
- try
- {
- Statement st = null;
- st = con.createStatement();
- int i = st.executeUpdate(query);
- if (i == -1){
- System.out.println("db error : " + query);
- return false;
- }
- st.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- return false;
- }
- return true;
- }
- public ResultSet ExecuteQuery(String query)
- {
- ResultSet rs = null;
- try {
- Statement stmt = con.createStatement();
- rs = stmt.executeQuery(query);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return rs;
- }
- public ResultSet ExecuteRSGetTableSchema()
- {
- try
- {
- PreparedStatement pst = con.prepareStatement(GetTablesSchemaQuery());
- return pst.executeQuery();
- }
- catch (SQLException e)
- {
- // TODO Add better logging
- e.printStackTrace();
- }
- return null;
- }
- public Connection GetConnection()
- {
- return con;
- }
- public String GetConnectionURL()
- {
- return scb.getConnectionURL();
- }
- public String GetTablesSchemaQuery()
- {
- return scb.getTablesSchemaQuery();
- }
- public String GetDBName()
- {
- return scb.getDBName();
- }
- /**
- * Get the name of the server.
- * @return name of the server.
- */
- public String GetServerName()
- {
- return scb.getServername();
- }
- /**
- * Get the name of the user.
- * @return name of the user.
- */
- public String GetUserName()
- {
- return scb.getUserName();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement