Rico Strydom
Rico Strydom

Reputation: 549

Connection and SqlServerConnection mismatch

Apologies for terminilogy used.

I have a generic class that I want to use to fill JXComboBoxes. It looks like this:

package Fillers;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jdesktop.swingx.JXComboBox;

public class ComboBoxFiller {

    private Connection connConnection;  
    private JXComboBox cboComboBox;
    private String strComboBoxQuery;

    public ComboBoxFiller(Connection connUToolDb, JXComboBox cboComboBox, String strComboBoxQuery) throws SQLException 
    {
        this.connConnection = connUToolDb;
        this.cboComboBox = cboComboBox;
        this.strComboBoxQuery = strComboBoxQuery;   
        Fill();
    }

    public void Fill() throws SQLException {
        ResultSet rsComboBox = null;
        PreparedStatement prepstmntComboBbox = null;
        try {       
            prepstmntComboBbox = connConnection.prepareStatement(strComboBoxQuery,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rsComboBox = prepstmntComboBbox.executeQuery();
            cboComboBox.removeAllItems();    
            while (rsComboBox.next())
            {
                String ComboBoxItem = rsComboBox.getString(1);
                cboComboBox.addItem(ComboBoxItem);
            }                   
        } 
        finally {
            try {
                rsComboBox.close();
                prepstmntComboBbox.close();
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
        }   
    }
}

The Database I use is a SqlServer database. To connect to the database I use another generic class. It looks like this.

package DbConnections;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcSQLServerConnection {

    private Connection connJdbcSqlServer;

    public void Connect(String dbURL) throws ClassNotFoundException {
        try {
            //DriverManager.registerDriver(new SQLServerDriver());
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            setConnJdbcSqlServer(DriverManager.getConnection(dbURL));
            System.out.println("Connected to SqlServer");           
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public Connection getConnJdbcSqlServer() {
        return connJdbcSqlServer;
    }

    public void setConnJdbcSqlServer(Connection connJdbcSqlServer) {
        this.connJdbcSqlServer = connJdbcSqlServer;
    }

    public void Close(){
        try {
            connJdbcSqlServer.close();
            System.out.println("SqlServer Connection Closed...");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

}

The problem now comes in when I want to fill the a combobox using the SqlServerConnection (com.microsoft.sqlserver.jdbc.SQLServerDriver) because the JxComboBoxFiller expects a "normal" Connection (java.sql.Connection).

How do I bridge this? I just cant wrap my head around this.

Upvotes: 0

Views: 46

Answers (1)

Wajdy Essam
Wajdy Essam

Reputation: 4340

get the connection from your JdbcSQLServerConnection by calling getConnJdbcSqlServer and pass the returned object to your ComboBoxFilter

for example:

JdbcSQLServerConnection sqlConnection= new JdbcSQLServerConnection(path);
sqlConnection.Connect();

Connection connection = sqlConnection.getConnJdbcSqlServer();
ComboBoxFiller(connection,...);

Upvotes: 1

Related Questions