【发布时间】:2017-03-26 15:02:31
【问题描述】:
我正在尝试从 Android 应用程序连接到 SQL Server 数据库。连接有效,但由于 Table
,我得到了期望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;
}
}
【问题讨论】:
-
您连接的数据库中是否存在该表?
-
@TZHX 是的
-
让您的应用显示
stmt.getConnection().getCatalog()返回的结果。您可能没有访问您认为的数据库。 -
@GordThompson getCatalog() 返回 'master'...
标签: android sql sql-server jdbc jtds