我想将我的应用程序连接到SQL Server数据库
我创建了这个loginActivity:
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LoginActivity extends AppCompatActivity
{
// Declaring layout button, edit texts
Button login;
EditText username,password;
ProgressBar progressBar;
// End Declaring layout button, edit texts
// Declaring connection variables
Connection con;
String un,pass,db,ip;
//End Declaring connection variables
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Getting values from button, texts and progress bar
login = (Button) findViewById(R.id.btnlogin);
username = (EditText) findViewById(R.id.txtStagiaireName);
password = (EditText) findViewById(R.id.txtStagiairePass);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
// End Getting values from button, texts and progress bar
// Declaring Server ip, username, database name and password
ip = "192.168.1.9:1433";
db = "dbcom";
un = "sa";
pass = "123456";
//End Declaring Server ip, username, database name and password
// Setting up the function when button login is clicked
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
checkLogin.execute("");
}
});
//End Setting up the function when button login is clicked
}
public class CheckLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
@Override
protected void onPreExecute()
{
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String r)
{
progressBar.setVisibility(View.GONE);
Toast.makeText(LoginActivity.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess)
{
Toast.makeText(LoginActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
//finish();
}
}
@Override
protected String doInBackground(String... params)
{
String usernam = username.getText().toString();
String passwordd = password.getText().toString();
if(usernam.trim().equals("")|| passwordd.trim().equals(""))
z = "Please enter Username and Password";
else
{
try
{
con = connectionclass(un, pass, db, ip); // Connect to database
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
// Change below query according to your own database.
String query = "select * from Stagiaire where username= '" + usernam.toString() + "' and password = '"+ passwordd.toString() +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
z = "Login successful";
isSuccess=true;
con.close();
}
else
{
z = "Invalid Credentials!";
isSuccess = false;
}
}
}
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 +";databaseName="+ database + ";user=" + user+ ";password=" + password + ";";
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;
}
}
当我编译项目并输入用户名和密码时,我总是得到这个Toast:检查您的Internet访问权限! 这意味着connectionURL = null 但为什么?如果我的代码有任何问题,请通知我!
12-02 22:43:23.116 2388-2388/com.reddivx.ismo.advancedismo I/art: Not late-enabling -Xcheck:jni (already on)
12-02 22:43:23.116 2388-2388/com.reddivx.ismo.advancedismo W/art: Unexpected CPU variant for X86 using defaults: x86
12-02 22:43:23.300 2388-2388/com.reddivx.ismo.advancedismo W/System: ClassLoader referenced unknown path: /data/app/com.reddivx.ismo.advancedismo-2/lib/x86
12-02 22:43:23.314 2388-2388/com.reddivx.ismo.advancedismo I/InstantRun: starting instant run server: is main process
12-02 22:43:23.394 2388-2388/com.reddivx.ismo.advancedismo W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-02 22:43:24.630 2388-2388/com.reddivx.ismo.advancedismo W/gralloc_ranchu: Gralloc pipe failed
[ 12-02 22:43:24.632 2388: 2388 D/ ]
HostConnection::get() New Host Connection established 0x9ae3d7c0, tid 2388
12-02 22:43:24.674 2388-2409/com.reddivx.ismo.advancedismo I/OpenGLRenderer: Initialized EGL, version 1.4
12-02 22:43:24.674 2388-2409/com.reddivx.ismo.advancedismo D/OpenGLRenderer: Swap behavior 1
12-02 22:45:19.093 2388-2388/com.reddivx.ismo.advancedismo W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-02 22:46:44.606 2388-2968/com.reddivx.ismo.advancedismo E/error here 1 :: Network error IOException: Connection refused
答案 0 :(得分:0)
我不相信你可以在Android上使用JDBC开箱即用。 请参阅this SO question
如果要在ANdroid上使用JDBC,还要检查this library