繁体   English   中英

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

[英]“Invalid object name” when referring to a table

我正在尝试从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;
    }

}

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

您正在尝试使用一个连接的URL参数中指定database是JTDS不承认:

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);
}

相反,您应该使用文档中描述的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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM