引用表时“无效的对象名称”

时间:2017-03-26 15:02:32

标签: android sql sql-server jdbc jtds

我正在尝试从Android应用程序连接到SQL Server数据库。连接有效但我因为表

而得到了预期
  

java.sql.SQLException:无效的对象名称'dbo.Names'。

我发送给服务器的SQL代码是

stmt.execute("INSERT INTO [dbo].[Names] ([name]) VALUES ('Test')");

有什么想法吗?

编辑:

以下是代码:

static String serverIp = "xxx.database.windows.net:1433";
static String serverDb = "xxxApp";
static String serverUserName = "xxx";
static String serverPassword = "xxx";

Connection con;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CheckLogin checkLogin = new CheckLogin();
    checkLogin.execute("");

}

public class CheckLogin extends AsyncTask<String, String, String> {

    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(String r) {

    }

    @Override
    protected String doInBackground(String... params) {

        try {
            con = connectionclass(serverUserName, serverPassword, serverDb, serverIp);

            if (con == null) {
                z = "Check Internet Access";
            } else {

                Statement stmt = con.createStatement();
                stmt.execute("INSERT INTO [dbo].[Names] ([name]) VALUES ('Test')");

                String x = ""; // DEBUG point
            }
        } catch (Exception ex) {
            isSuccess = false;
            z = ex.getMessage();
        }

        return z;
    }


    @SuppressLint("NewApi")
    public Connection connectionclass(String user, String password, String database, String server) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";database=" + database + ";user=" + user + ";password=" + password + ";encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";

            connection = DriverManager.getConnection(ConnectionURL);
        } 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 connection;
    }

}

1 个答案:

答案 0 :(得分:1)

SQL语句失败,因为您没有为jTDS使用正确的连接URL格式,因此您实际上并未连接到String变量serverDb指定的数据库。

您正在尝试使用jTDS无法识别的名为database的连接网址参数:

String serverDb = "myDb";
String connUrl = "jdbc:jtds:sqlserver://localhost:49242;database=" + serverDb;
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
    System.out.println(conn.getCatalog());  // prints: master
} catch (Exception e) {
    e.printStackTrace(System.err);
}

相反,您应该使用documentation

中描述的server:port/database格式
String serverDb = "myDb";
String connUrl = "jdbc:jtds:sqlserver://localhost:49242/" + serverDb;
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
    System.out.println(conn.getCatalog());  // prints: myDb
} catch (Exception e) {
    e.printStackTrace(System.err);
}