错误:程序包net.sourceforge.jtds.jdbc不存在

时间:2019-04-05 12:41:15

标签: android sql-server

我正在尝试从android studio连接到SQL Server数据库 我正在使用jtds,并且已经将“ jtds-1.3.1.jar”模块导入android studio

在行下面的导入部分出现错误

import net.sourceforge.jtds.jdbc.*;

,错误是:

"error: package net.sourceforge.jtds.jdbc does not exist"

。我不知道我想念什么。

package com.example.mycar4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
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;
import net.sourceforge.jtds.jdbc.*;


public class database 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;
String usernam,passwordd;
//End Declaring connection variables


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

    // Getting values from button, texts and progress bar
    login = (Button) findViewById(R.id.btn_Login);
    username = (EditText) findViewById(R.id.et_username);
    password = (EditText) findViewById(R.id.et_password);
    progressBar = (ProgressBar) findViewById(R.id.pbbar);
    progressBar.setVisibility(View.GONE);
    // End Getting values from button, texts and progress bar

    // Declaring Server ip, username, database name and password
    ip = "***";
    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(database.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess)
        {
            Toast.makeText(database.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
                {
                    String query = "select * from login 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").newInstance();
        ConnectionURL = "jdbc:jtds:sqlserver://sql5009.mywindowshosting.com;database=DB_A2C00B_login;user=DB_A2C00B_login_admin;password=login@123";
//            ConnectionURL = 
"jdbc:jtds:sqlserver://192.168.1.9;database=msss;instance=SQLEXPRESS;Network 
Protocol=NamedPipes" ;


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

}

2 个答案:

答案 0 :(得分:0)

要将Android App与SQL Server连接,您需要在两者之间设置一个Web(Http)服务器。您可以使用Java(J2EE)或php等任何熟悉的语言来构建此Web服务器。

此网络服务器将由您的SQL服务器托管,并将接收来自Android应用的Http请求,进行处理,将请求作为查询转发到SQL服务器,然后将其作为REST / JSON返回给您的应用(当前通过Web发送数据的格式)或SOAP / XML(通过Web发送数据的旧方法)。

App >> Web服务器>>数据库服务器

Web服务器应为RESTful,并且应用程序可以通过Retrofit2库通过Http请求与之交互。

这里有一些资源可以帮助您入门;

  1. Retrofit,作者:Square。
  2. Retrofit Github存储库。
  3. Simple CRUD example with Java RESTful Web Service,一个简短的教程。
  4. RESTful Web Services: A Tutorial Android + Java Web Services

答案 1 :(得分:0)

如果您使用的是android studio或eclipse,则应将jtds jar文件放在正确的位置 它位于项目应用程序内的libs(库)文件夹中。将其放入libs>右键单击,然后添加库。

请注意,您应该使用1.2.7或更低版​​本,该版本受支持程度较高

jtds location in project