felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Penzov <peter.pen...@gmail.com>
Subject Re: Connect a bundle to a data base
Date Fri, 16 Mar 2012 09:30:26 GMT
What Application do you use?

This code I use for Glassfish and Oracle:

Activator:

import javax.sql.DataSource;
import java.sql.SQLException;

import java.util.Properties;
import org.DX_57.osgi.SH_27.api.SessionHandle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;


public class SessionHandleApp implements BundleActivator {

    public static final String DSNAME = "jdbc/Oracle";
    public ServiceTracker st;

    @Override
    public void start(final BundleContext bc) throws Exception {
        debug("Activator started");


        Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
                + "=" + DataSource.class.getName() + ")" + "(jndi-name="
                + DSNAME + ")" + ")");
        st = new ServiceTracker(bc, filter, null) {

            @Override
            public Object addingService(ServiceReference reference) {
                DataSource ds = (DataSource) bc.getService(reference);
                try {
                    debug(ds.getConnection().toString());

                SessionHandle sh = new SessionHandleImpl();
                                sh.setDataSource(ds);
                    ServiceRegistration registerService =
bc.registerService(SessionHandle.class.getName(), sh, new Properties());

                } catch (SQLException e) {
                }

                return super.addingService(reference);
            }

            @Override
            public void removedService(ServiceReference reference,
                    Object service) {
                super.removedService(reference, service);
            }

        };
        st.open();
    }

    public void stop(BundleContext bc) throws Exception {
        boolean ungetService =
bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
        st.close();
    }

    private void debug(String msg) {
        System.out.println("JDBCBundleActivator: " + msg);
    }

}


Simple SQL query:

public class SessionHandleImpl implements SessionHandle {

       public DataSource ds;

       /** method for checking password into the Oracle database */
       @Override
       public String CheckUserDB(String userToCheck) throws SQLException {
            String storedPassword = null;
            String SQL_Statement = null;

            if (ds == null) throw new SQLException();
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException();

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       SQL_Statement = "SELECT Passwd from USERS WHERE
Username = ?";

                       PreparedStatement passwordQuery =
conn.prepareStatement(SQL_Statement);
                       passwordQuery.setString(1, userToCheck);

                       ResultSet result = passwordQuery.executeQuery();

                       if(result.next()){
                            storedPassword = result.getString("Passwd");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {
                conn.close();

                }

       return storedPassword;
       }
}

You have to edit the code - name of the datasource and the java classes.

Best wishes
Peter


On Fri, Mar 16, 2012 at 11:10 AM, elMateo <soyelmateo@gmail.com> wrote:

> Hello all,
>
> I need to connect a bundle to a MySQL data base and I don't know how I can
> do it.
>
> I know that I can't do the same that in a normal Java application, but I
> don't find the correct form to do this.
>
> Could anybody help me?
>
> Thanks in advance, Jesus
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message