连接到MS SQL在线数据库

时间:2018-06-11 11:32:16

标签: android

你好,我是编程android的初学者 我正在尝试连接并登录到smartasp.net主机上的MS SQL数据库2012。 我使用jtds-1.3.1连接到数据库2012。 但是连接不起作用,这就出现了错误:

java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:278)
   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.VerifyError: net/sourceforge/jtds/jdbc/TdsCore
   at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:359)
   at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
   at java.sql.DriverManager.getConnection(DriverManager.java:175)
   at java.sql.DriverManager.getConnection(DriverManager.java:140)
   at com.example.kamran.login.MainActivity.connectionclass(MainActivity.java:158)
   at com.example.kamran.login.MainActivity$CheckLogin.doInBackground(MainActivity.java:106)
   at com.example.kamran.login.MainActivity$CheckLogin.doInBackground(MainActivity.java:73)
   at android.os.AsyncTask$2.call(AsyncTask.java:264)

我的代码中是否有错误?

我使用jtds-1.3.1连接数据库2012。

我的Main_activity:

public class MainActivity extends AppCompatActivity
{
// Declaring layout button, edit texts
Button login;
EditText username,password,lable;
ProgressBar progressBar;
// End Declaring layout button, edit texts

// Declaring connection variables
Connection con;
String un,pass,db,ip;
String usernam,passwordd;
//End Declaring connection variables

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

    // Getting values from button, texts and progress bar
    login = (Button) findViewById(R.id.button);
    username = (EditText) findViewById(R.id.editText);
    password = (EditText) findViewById(R.id.editText2);
    lable = (EditText) findViewById(R.id.editText3);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    // End Getting values from button, texts and progress bar

    // Declaring Server ip, username, database name and password
    ip = "SQL7002";
    db = "????????";
    un = "????????";
    pass = "??????";
    // 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)
        {
            usernam = username.getText().toString();
            passwordd = password.getText().toString();
            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(MainActivity.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess)
        {
            Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
            //finish();
        }
    }
    @Override
    protected String doInBackground(String... params)
    {

        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
                {
                    final String query = "select Spec from Password where Num= '" + usernam.toString() + "' and Pass = '"+ passwordd.toString() +"' ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            lable.setText(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 un, String pass, String db, String ip)
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    java.sql.Connection connection = null;
    String ConnectionURL = null;
    try
    {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://SQL7002.site4now.net;database=?????????;user=?????????;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;
}
}

1 个答案:

答案 0 :(得分:0)

使用jtds-1.3.0,它会起作用!