我无法使用JDBC与Android Studio连接到SQL Server Express

时间:2017-12-06 02:01:49

标签: java android android-studio jdbc

我正在尝试连接到SQL Server(localhost)以便能够在数据库上保存信息,但是当我从MainActivity.java转到Main2Activity.java时出于某种原因尝试实现这一点时,我的应用程序停止工作大约20秒然后提出一个祝酒消息(连接被拒绝),请告知我做错了什么

Main2Activity.java

package com.example.omar.externaldatabaseconnection;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity {

EditText et_name, et_age, et_address, et_email ,et_telephone;
Button btn_save;
DatabaseConnection db;

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

if(Build.VERSION.SDK_INT > 9){
StrictMode.ThreadPolicy policy = new 
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

et_name = (EditText) findViewById(R.id.et_name);
et_age = (EditText) findViewById(R.id.et_age);
et_address = (EditText) findViewById(R.id.et_address);
et_email = (EditText) findViewById(R.id.et_email);
et_telephone = (EditText) findViewById(R.id.et_telephone);
btn_save = (Button) findViewById(R.id.btn_save);
db = (DatabaseConnection) getApplication();
db.open();

btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.save("insert into Employees values 
('"+et_name.getText().toString().trim()+"', 
'"+et_age.getText().toString().trim()+"', 
'"+et_address.getText().toString().trim()+"', 
'"+et_email.getText().toString().trim()+"', 
'"+et_telephone.getText().toString().trim()+"')");
finish();
}
});
}

}

DatabaseConnection.java

package com.example.omar.externaldatabaseconnection;

import android.app.Application;
import android.widget.Toast;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;


public class DatabaseConnection extends Application {

Connection connection;
String url = "";
String DBName = "EmployeesDB";
String DBUserName = "PC NAME";
String DBPassword = "MSSQL Password";
String HostName = "10.0.2.2";
String Port = "MY PORT NUMBER";
Statement statement;

public void open(){
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url = "jdbc:jtds:sqlserver://" + HostName +":"+Port+";"+ "databaseName=" + 
DBName + ";user=" + DBUserName + ";password="+ DBPassword + " 
instance=SQLEXPRESS;";
connection = DriverManager.getConnection(url, DBUserName, DBPassword);
}
catch (Exception e){
Toast.makeText(this, e.getCause().getMessage(), Toast.LENGTH_LONG).show();

}
}

public void save (String sql){
try {
statement = connection.createStatement();
statement.executeQuery(sql);
}
catch (Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

1 个答案:

答案 0 :(得分:2)

Sir android是一个移动平台,你只是无法将android应用程序与任何数据库服务器即Sql server,mysql或oracle直接连接,你必须创建一个本地SQLITE数据库并通过从任何数据库获取数据来填充它通过WEB服务再次提到数据库服务器,如果你选择php,asp,java等来制作你的WEB服务,它完全取决于你。填充SQLITE数据库后,您就可以在本地SQLITE数据库上执行查询。

简单来说,WEB SERVICE是Android应用程序和数据库服务器之间的桥梁,您必须使用它。