barnacle.m
barnacle.m

Reputation: 2190

Newbie android developer trying to connect to SQL Server 2008

I am fairly new to android development, and I am trying to connect my app to a SQL Server 2008 database. I get a an error saying "Could not find the database driver net.sourceforge.jtds.jdbc.Driver" when attempting to connect. Here is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_course);
        // Show the Up button in the action bar.
        setupActionBar();
        setCurrentDateOnView();
        addListenerOnButton();
        TextView textview7 = (TextView) findViewById(R.id.textView7);
        String connectionurl = "jdbc:jtds:sqlserver:winsqls01.cpt.wa.co.za;databaseName=Courses; user=*;Password=*;";
try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        textview7.setText("Successful");
        Connection con = DriverManager.getConnection(connectionurl);
        // Create and execute an SQL statement that returns some data.
        String SQL = "SELECT * FROM Courses";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next())
        {
            textview7.setText(rs.getString(0));
        }
} 

    catch (ClassNotFoundException e) {

         textview7.setText("Could not find the database driver " + e.getMessage());

               } catch (SQLException e) {          
                   textview7.setText("Could not connect to the database " + e.getMessage());

               }

catch (Exception e) {
    e.printStackTrace();
} 







        dpResult1 = (DatePicker) findViewById(R.id.dpResult1);
        dpResult1.setVisibility(View.GONE);
        dpResult2 = (DatePicker) findViewById(R.id.dpResult2);
        dpResult2.setVisibility(View.GONE);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

My import for the driver is import net.sourceforge.jtds.jdbc.*; but it says that I am not actually using this import. Can anybody help?

Upvotes: 0

Views: 2061

Answers (1)

alex
alex

Reputation: 5684

Have you looked at this solution ?

Which version of the jdbc driver and which version of Java are you using? User aiolos suggests to go back to the older version 1.2.6 to get it working. If you are using 1.3.0 you have to use Java 7, too. Java 6 and below fails to load the driver.

UPDATE

Connectionstring should be in that format

 jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

so try yours like that:

 jdbc:jtds:sqlserver://winsqls01.cpt.wa.co.za:__PORT__;databaseName=Courses;user=*;password=*;

UPDATE 2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_course);
    // Show the Up button in the action bar.
    setupActionBar();
    setCurrentDateOnView();
    addListenerOnButton();
    TextView textview7 = (TextView) findViewById(R.id.textView7);
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        textview7.setText("Successful");
        Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://winsqls01.cpt.wa.co.za:__PORT__;databaseName=Courses", "__USERNAME__", "__PASSWORD__");
        // Create and execute an SQL statement that returns some data.
        String SQL = "SELECT * FROM Courses";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next())
        {
            textview7.setText(rs.getString(0));
        }
    } 

    catch (ClassNotFoundException e) {
        textview7.setText("Could not find the database driver " + e.getMessage());
    } 
    catch (SQLException e) {          
        textview7.setText("Could not connect to the database " + e.getMessage());
    }
    catch (Exception e) {
        textview7.setText("Some other error occured: " + e.getMessage());
        e.printStackTrace();
    }
} 

Try my code above, its your code restructured and change all vars marked with underscores to your needs. Another note, are you sure that both database name and table name is equal "Courses"?

Upvotes: 1

Related Questions