2

I am using android studio to develop an application and using Azure Sql Server to host my database. The problem is I was able to connect to my database on SQL server but it has an error of Object not found in my database.

I found out that it might be connecting to my master database instead of the database I want it to connect to. Is there any solution to solve the problem?

package com.example.lenovo.testing1;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;

import java.sql.*;

public class ConnectionClass {
    String hostName = "haozailai.database.windows.net";
    String dbName = "haozailai";
    String user = "username";
    String password = "password";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        String ConnURL;
        Connection conn = null;

        try {

            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            String url = String.format("jdbc:jtds:sqlserver://haozailai.database.windows.net:1433;database=haozailai;user=username;password=password;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
            conn = DriverManager.getConnection(url);

        }catch (SQLException se)
        {
            Log.e("error here 1 : ", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
           Log.e("error here 2 : ", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error here 3 : ", e.getMessage());
        }


        return conn;


    }
}

Picture of my database structure

EnCi Chu
  • 21
  • 2

2 Answers2

1

I tried to connect my sqlserver via java jdbc and did not reproduce your issue.

I can connect to my application db successfully.

My test code:

import java.sql.*;

public class Test {

    public static final String url = "jdbc:sqlserver://***.database.windows.net:1433;database=***;user=***password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
    public static final String name = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static Connection conn = null;
    public static PreparedStatement pst = null;
    public static Statement stmt = null;
    public static ResultSet rs = null;

    public static void main(String[] args) {

        try {

            String SQL = "select * from dbo.Student";
            Class.forName(name);
            conn = DriverManager.getConnection(url);

            stmt = conn.createStatement();
            rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void close() {
        try {
            conn.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

After some research, I found out it is because of your connect url.

You need to modify your connect url :

jdbc:jtds:sqlserver://haozailai.database.windows.net:1433/<your application db name> ...

You could refer to the pages below for more details.

  1. https://sourceforge.net/p/jtds/discussion/104389/thread/a672d758/

  2. how to connect sql server using JTDS driver in Android


Update answer:

I have made a slight adjustment to your connect URL and can connect to my application database normally.

    try {

        String SQL = "select * from dbo.Student";

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String url = String.format("jdbc:jtds:sqlserver://***.database.windows.net:1433/<your database name>;user=***;password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
        conn = DriverManager.getConnection(url);

        stmt = conn.createStatement();
        rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Notice that remove the database=*** and add "/<your database name>" after your host string.

Please refer to the above code and try again.Any concern, please let me know.

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • My connect URL is directly get from Azure Portal. It linked to that server, but it linked to master database which from System Databases Folder (I think it is default database in MS SQL Server 2017) but not the database that i created. – EnCi Chu Nov 10 '17 at 05:35
  • @EnCiChu Have you tried the url I mentioned in my answer. Add "/your db name" after the host string – Jay Gong Nov 10 '17 at 05:38
  • Yes, but it is still not working. It can't even connect to the server from android studio – EnCi Chu Nov 10 '17 at 05:53
  • Oh yeah.. sorry for the late reply. Yeah it is working. Thank you – EnCi Chu Nov 14 '17 at 10:31
  • @EnCiChu You could mark the answer for others' reference on the forum.Thank you. – Jay Gong Nov 23 '17 at 01:33
0

I know this answer is waay too late, but I found this video that totally works: https://www.youtube.com/watch?v=WJBs0zKGqH0

The thing is, you have to download a jtds jar, the guy in the video says where you can get it from and also, you need to add "jtds" before "sqlserver" in connection url and edit the way the 'databe' is written in the connection url, like this: jdbc:jtds:sqlserver://serverName.database.windows.net:portNr:DatabaseName=dbName;user=....

Kiva
  • 53
  • 1
  • 10
  • 1
    Please post a summary of what happens in the video here in case the video gets removed. – Skami Jun 05 '19 at 19:44