0

Hi guys i have tried some codes but none work i was hoping if someone could help me. Bellow i will put my code

AndroidManisfest:

<?xml version="1.0" encoding="utf-8"?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andre.marques.moldartsqlserver">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".InicioActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".OrdemFabricoActivity"></activity>
</application>
</manifest>

activity_ordemfabrico.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:id="@+id/EnterPays_atomPaysList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:listitem="@layout/ofselect"
    android:layout_below="@+id/layout_inf" >
</ListView>
</LinearLayout>

ofselect.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeightSmall"
android:orientation="horizontal"
android:showDividers="middle"
android:divider="?android:dividerVertical"
android:dividerPadding="8dp"
android:gravity="center">


<TextView android:id="@+id/Codart"
    style="?android:textAppearanceMedium"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:listPreferredItemPaddingLeft" />

<TextView android:id="@+id/quantidade"
    style="?android:textAppearanceMedium"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content" />
</LinearLayout>

ConnectionClass:

public class ConnectionClass {
String ip;
String classs;
String db;
String un;
String password;
public ConnectionClass() {
    classs = "net.sourceforge.jtds.jdbc.Driver";
    db = "DatabaseName";
    un = "user";
    password = "XXXXXXXX";
    ip = "192.xxx.x.xxx";
}
public ConnectionClass(String Ip, String Classs, String Db, String Un, String Password) {
    ip = Ip;
    classs = Classs;
    db = Db;
    un = Un;
    password = Password;
}
public String getip() {
    return ip;
}
public String getclasss() {
    return classs;
}
public String getdb() {
    return db;
}
public String getun() {
    return un;
}
public String getpassword() {
    return password;
}
public void setip(String Ip) {
    ip = Ip;
}
public void setdb(String Db) {
    db = Db;
}
public void setclasss(String Classs) {
    classs = Classs;
}
public void setun(String Un) {
    un = Un;
}
public void setpassword(String Password) {
    password = Password;
}
}

OrdemFabricoActivity:

public class OrdemFabricoActivity extends Activity {
ConnectionClass connectionclass;
ListView lst;
SimpleAdapter ADAhere;
String usernameS;
String datets;
String call, db, un, passwords;
Connection connect;
ResultSet rs;
@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB, String _server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
        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;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ordemfabrico);
    lst = (ListView) findViewById(R.id.EnterPays_atomPaysList);
    /************* CONNECTION DATABASE VARIABLES ***************/
    connectionclass = new ConnectionClass();
    call = connectionclass.getip();
    un = connectionclass.getun();
    passwords = connectionclass.getpassword();
    db = connectionclass.getdb();
    connect = CONN(un, passwords, db, call);

    String querycmd = "select NUMOF, COD_ART, DESC_ART, OBSERVACOES, QUANT from tabof where tabof.ID_TO <> 5";
    try {
        Statement statement = connect.createStatement();
        rs = statement.executeQuery(querycmd);
        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();
        while (rs.next()) {
            Map<String, String> datanum = new HashMap<String, String>();
            datanum.put("A", rs.getString("COD_ART"));
            //datanum.put("B", rs.getString("QUANT"));
            data.add(datanum);
        }
        String[] fromwhere = { "A" };
        int[] viewswhere = { R.id.Codart };
        ADAhere = new SimpleAdapter(OrdemFabricoActivity.this, data, R.layout.ofselect, fromwhere, viewswhere);
        lst.setAdapter(ADAhere);
    } catch (SQLException e) {
        Toast.makeText(OrdemFabricoActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
    }
}
}

This is my code the result that i get with this code it is when i go to this activity my phone screen is black and nothing happen. I wanted to put in the labels Codart and quant the value of some camps of the database if someone know how to do this please help me.

André Marques
  • 180
  • 1
  • 15

1 Answers1

0

I observed from your code, you are trying to connect database using JDBC driver. JDBC is not supported by android.

But Oracle provide the JDBC driver for android.

https://docs.oracle.com/cd/E17076_04/html/installation/build_android_jdbc.html

According oracle docs, you must have knowledge of NDK to build and configure the JDBC. I think its too complex process to configure. JDBC is very slow compared to SQLITE because no extra server configuration required to access data from sqlite database.

If your purpose is to store data in local database you must prefer SQLite3 , that is embedded with android,fast accessing and recommended by Google. Its too easy to use.

You can not create connection with sql server and android directly. If you want to store data on local database and then want to show record in listview, use sqlite. You can use following tutorial.

HOMEDATABASEAndroid SQLite Database Tutorial Android SQLite Database Tutorial

If you really want to store data in sql-server, you must use client-server connection. Create web-services to access data from server, and consume it in android and populate the listview. For example, PHP and MySQL. Refer this tutorial

I thinks it help you.

USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • but i saw several tutorials about JDBC in android, i have downloaded the lib like they said i really wanted to do it like they do with JDBC – André Marques Apr 28 '16 at 07:43
  • Yes, you may be able to configure connection with JDBC. Its not recommended. Check this answer http://stackoverflow.com/questions/12233145/connecting-to-mysql-from-android-with-jdbc – USKMobility Apr 28 '16 at 08:41