Android数据库连接错误API低于23

时间:2016-04-08 11:12:40

标签: android database api

(首先,我不是母语为英语的人,所以我的英语非常糟糕)

我一直在研究Android APP。基本上我需要连接到SQL Server 2012 Express数据库。一切正常,直到我使用23的不同API。我在其他主题上发现了这个错误,但没有回答对我有用。这是错误:

04-08 10:56:06.510 1761-1761/com.example.*****.controlcodigosbox E/AndroidRuntime: FATAL EXCEPTION: main
      java.lang.IllegalStateException: Could not execute method for android:onClick
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
          at android.view.View.performClick(View.java:4204)
          at android.view.View$PerformClick.run(View.java:17355)
          at android.os.Handler.handleCallback(Handler.java:725)
          at android.os.Handler.dispatchMessage(Handler.java:92)
          at android.os.Looper.loop(Looper.java:137)
          at android.app.ActivityThread.main(ActivityThread.java:5041)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:511)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
          at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:511)
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
          at android.view.View.performClick(View.java:4204) 
          at android.view.View$PerformClick.run(View.java:17355) 
          at android.os.Handler.handleCallback(Handler.java:725) 
          at android.os.Handler.dispatchMessage(Handler.java:92) 
          at android.os.Looper.loop(Looper.java:137) 
          at android.app.ActivityThread.main(ActivityThread.java:5041) 
          at java.lang.reflect.Method.invokeNative(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:511) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
          at dalvik.system.NativeStart.main(Native Method) 
       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.*****.controlcodigosbox.ConnectionClass.CONN(ConnectionClass.java:32)
          at com.example.*****.controlcodigosbox.MainActivity.onClickRegistrar(MainActivity.java:95)
          at java.lang.reflect.Method.invokeNative(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:511) 
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
          at android.view.View.performClick(View.java:4204) 
          at android.view.View$PerformClick.run(View.java:17355) 
          at android.os.Handler.handleCallback(Handler.java:725) 
          at android.os.Handler.dispatchMessage(Handler.java:92) 
          at android.os.Looper.loop(Looper.java:137) 
          at android.app.ActivityThread.main(ActivityThread.java:5041) 
          at java.lang.reflect.Method.invokeNative(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:511) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
          at dalvik.system.NativeStart.main(Native Method) 

我一直在对代码和错误日志进行一些研究,这似乎是数据库连接的错误,但我真的没有发现可能是什么。 问题是,APP在API 23上工作得很好。但是当我使用API​​ 17模拟器设备时,这个错误就出现了。我在API 17上启动了这个项目。我已经尝试创建一个API 10项目而且它没有用。

这是布局:

<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
    android:id="@+id/lblCodigo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="@string/introducirCodigo"
    android:textSize="20dp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editCodigo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblCodigo"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:hint="@string/edit"
    android:singleLine="true" />

<LinearLayout
    android:id="@+id/layoutMuestra"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editCodigo"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    android:layout_marginBottom="20dp"
    android:visibility="gone"
    >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Material: " />

        <TextView
            android:id="@+id/lblMaterial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gama: " />

        <TextView
            android:id="@+id/lblGama"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ancho: " />

        <TextView
            android:id="@+id/lblAncho"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Diametro: " />

        <TextView
            android:id="@+id/lblDiametro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

<TextView
    android:id="@+id/lblBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/layoutMuestra"
    android:layout_centerHorizontal="true"
    android:text="@string/seleccionaCantidad"
    android:textSize="20dp"
    android:textStyle="bold" />

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblBox"
    android:layout_centerHorizontal="true">

    <Spinner
        android:id="@+id/spinnerBox"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp" />


    <EditText
        android:id="@+id/editCantidad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/spinnerBox"
        android:hint="@string/cantidad"
        android:singleLine="true" />
</RelativeLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/layout"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp">

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:onClick="onClickRegistrar"
        android:text="@string/darAlta" />
</LinearLayout>

这是数据库连接类:

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionClass {
String ip = "192.168.**.**:1433";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "bd****";
String un = "**";
String password = "******";


@SuppressLint("NewApi")
public Connection CONN() {
    StrictMode.ThreadPolicy policy = new      StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {
        Class.forName(classs);
        ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" + "databaseName=" + db + ";user=" + un + ";password="
                + password + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}
}

以下是主要代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class MainActivity extends AppCompatActivity {
private String idDiametro, idMaterial, idAncho, idGama;
private String codigoS;
private LinearLayout layoutMuestra;
private TextView material, diametro, ancho, gama;
private Spinner spinnerBox;
private String[] arrayBox;
private EditText codigo, cantidad;
private ConnectionClass connectionClass;

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

    layoutMuestra = (LinearLayout) findViewById(R.id.layoutMuestra);
    spinnerBox = (Spinner) findViewById(R.id.spinnerBox);
    arrayBox = getResources().getStringArray(R.array.arrayBox);
    codigo = (EditText) findViewById(R.id.editCodigo);
    connectionClass = new ConnectionClass();
    material = (TextView) findViewById(R.id.lblMaterial);
    diametro = (TextView) findViewById(R.id.lblDiametro);
    ancho = (TextView) findViewById(R.id.lblAncho);
    gama = (TextView) findViewById(R.id.lblGama);
    cantidad = (EditText) findViewById(R.id.editCantidad);

    Toast.makeText(MainActivity.this, "¡LA PRUEBA BUENA!", Toast.LENGTH_SHORT).show();

    ArrayAdapter <CharSequence> adaptador = ArrayAdapter.createFromResource(this, R.array.arrayBox, android.R.layout.simple_spinner_dropdown_item);
    spinnerBox.setAdapter(adaptador);


    codigo.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            if (codigo.getText().toString().equals("")) {
                layoutMuestra.setVisibility(View.GONE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (codigo.getText().toString().equals("")) {
                layoutMuestra.setVisibility(View.GONE);
            }
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (codigo.getText().toString().equals("")) {
                layoutMuestra.setVisibility(View.GONE);
            }
        }
    });

}

public void onClickRegistrar(View v) {
    Boolean visible = true;
    codigoS = codigo.getText().toString();
    if(cantidad.getText().toString().equals("")){
        cantidad.setText("0");
    }
    if (codigo.getText().toString().equals(""))  {
        Toast.makeText(MainActivity.this, "¡Error! ¡Introduce un código!", Toast.LENGTH_SHORT).show();
    } else if (codigo.getText().toString().length() != 15 && codigo.getText().toString().length() != 13){
        Toast.makeText(MainActivity.this, "¡Error! ¡Introduce un código válido!", Toast.LENGTH_SHORT).show();
    }else {
        String msg = null;
        //SEPARO EL CODIGO EN LOS RESPECTIVO SUBSTRINGS
        idMaterial = codigoS.substring(2, 3);
        idDiametro = codigoS.substring(3, 7);
        idAncho = codigoS.substring(7, 11);
        idGama = codigoS.substring(11);

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                Toast.makeText(MainActivity.this, "¡NO SE HA CONECTADO A LA BD!", Toast.LENGTH_SHORT).show();
                msg = "Error in connection with SQL server";
            } else {
                String query = "select valor from MATERIAL where id='" + idMaterial + "'";
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                if(rs.next()){
                    material.setText(rs.getString(1));
                } else {
                    Toast.makeText(MainActivity.this, "¡NO HA ENCONTRADO EL MATERIAL!", Toast.LENGTH_SHORT).show();
                    visible = false;
                }

                query = "select valor from DIAMETRO where id='" + idDiametro + "'";
                stmt = con.createStatement();
                rs = stmt.executeQuery(query);
                if(rs.next()){
                    diametro.setText(rs.getString(1));
                } else {
                    Toast.makeText(MainActivity.this, "¡NO HA ENCONTRADO EL DIAMETRO!", Toast.LENGTH_SHORT).show();
                    visible = false;
                }

                query = "select valor from ANCHO where id='" + idAncho + "'";
                stmt = con.createStatement();
                rs = stmt.executeQuery(query);
                if(rs.next()){
                    ancho.setText(rs.getString(1));
                } else {
                    Toast.makeText(MainActivity.this, "¡NO HA ENCONTRADO EL ANCHO!", Toast.LENGTH_SHORT).show();
                    visible = false;
                }


                query = "select valor from GAMA where id='" + idGama + "'";
                stmt = con.createStatement();
                rs = stmt.executeQuery(query);
                if(rs.next()){
                    gama.setText(rs.getString(1));
                } else {
                    Toast.makeText(MainActivity.this, "¡NO HA ENCONTRADO LA GAMA!", Toast.LENGTH_SHORT).show();
                    visible = false;
                }

                if(visible == true){
                    layoutMuestra.setVisibility(View.VISIBLE);
                }

            }

        } catch (Exception ex) {
            msg = "Exceptions";
        }
        //GUARDAMOS LOS DATOS EN EL EXCEL
        //OBTENGO LA CANTIDAD DE BOX Y DE UNIDADES
        int cantBox = Integer.parseInt(arrayBox[spinnerBox.getSelectedItemPosition()]);
        int cantUnidad = Integer.parseInt(cantidad.getText().toString());

    }
}

}

我正在使用最新版本的jtds.jdbe驱动程序。

0 个答案:

没有答案