【问题标题】:Connecting to a SQL database in android studio using jtds-1.3.1 using an emulator使用模拟器使用 jtds-1.3.1 连接到 android studio 中的 SQL 数据库
【发布时间】:2018-01-30 14:41:30
【问题描述】:

程序员们好。我对android工作室相当陌生。这是我第一次将 Android 应用程序连接到 SQL Server。我一直使用 SQLite,这从来都不是问题。

目前我正在尝试使用带有模拟器的 jtds-1.3.1 连接到 SQL 数据库。我的应用程序在没有使用断点的情况下就崩溃了。

我的 logcat 提供给我这个:

---------崩溃开始

08-22 06:00:31.278 3273-3273/com.example.drizzybass.skyetek_login_with_sqlserver E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.drizzybass.skyetek_login_with_sqlserver, PID: 3273
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.drizzybass.skyetek_login_with_sqlserver/com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app

说实话,如果不通过断点,我不知道从哪里开始调试我的应用程序。我将 Mainactivity 到 onCreate 方法的断点设置为不占优势。

我的 MainActivity 代码如下所示:

 EditText user = (EditText) findViewById(R.id.txtUserName);
    EditText password = (EditText) findViewById(R.id.txtPassword);
    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    String strUser = user.getText().toString();
    String strPass = password.getText().toString();

    String username, pass, db, ip;
    Connection con;

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

        //Declaring server IP, username, database name and password
        ip = "xxx"; //xxx isnt the actually information
        db = "xxx";
        username = "xxx";
        pass = "xxx";

        btnLogin.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                CheckLogin checkLogin = new CheckLogin();
                checkLogin.execute("");

            }
        });
    }

    public class CheckLogin extends AsyncTask<String, String, String>
    {
        String x = "";

        Boolean isSuccess = false;//use to check whether the login fails or not

        protected String doInBackground(String... params)
        {
            if(strUser.trim().equals("") || strPass.trim().equals(""))
            {
                x = "Please enter Username and Password";
            }
            else
            {
                try
                {

                    con =  connectionclass(username, pass, db, ip);
                    if(con == null)
                    {
                        x = "Please check your internet connection";
                    }
                    else
                    {
                        String query = "select * from tblLogin where username = " + strUser.toString() + " and pass_word = " + strPass.toString();
                        Statement stat = con.createStatement();
                        ResultSet rs = stat.executeQuery(query);
                        if(rs.next())
                        {
                            x = "Login Successful";
                            isSuccess = true;
                            con.close();
                        }
                        else
                        {
                            x = "Invalid Credentials";
                            isSuccess = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    x = ex.getMessage();
                }
            }
            return x;
        }

        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 + ";user=" + user + ";password=" + password + ";";
                connection =  DriverManager.getConnection(ConnectionURL);

            }
            catch (SQLException ae)
            {
                Log.e("error here 1 : ", ae.getMessage());
            }
            catch (ClassNotFoundException e)
            {
                Log.e("error here 2 : ", e.getMessage());
            }
            catch (Exception ex)
            {
                Log.e("error here 3 : ", ex.getMessage());
            }
            return connection;

        }
    }

我的构建 gradle 模块应用如下所示:

    dependencies
            {
                compile fileTree(dir: 'libs', include: ['*.jar'])
                compile 'com.android.support:appcompat-v7:25.3.1'
                compile project(':jtds-1.3.1')
            } 

最后我的设计包含:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="40sp">

            <ImageView
                android:src="@drawable/skyelogo"
                android:textAlignment="center"
                android:layout_weight="1"

                android:layout_height="92dp"

                android:layout_gravity="center_horizontal"
                />
        </TableRow>


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/txtUserName"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="40sp"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="Username"
                android:inputType="textPersonName"
                android:text="" />
        </TableRow>



        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/txtPassword"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:hint="Password"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:password="true"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/btnLogin"
                android:onClick="btnLogin"
                android:layout_weight="1"
                android:elevation="0dp"
                android:text="LOGIN" />
        </TableRow>


        <TextView android:id="@+id/link_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:text="No account yet? Create one now"
            android:gravity="center"
            android:textSize="16dip"/>

    </TableLayout>

</RelativeLayout>

如果有人可以帮助我,将不胜感激。

【问题讨论】:

标签: java android sql-server jtds


【解决方案1】:

地点

EditText user = (EditText) findViewById(R.id.txtUserName);
EditText password = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
String strUser = user.getText().toString();
String strPass = password.getText().toString();

oncreate() 内部

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多