How to list more than one row of the bank in a Listview using JDBC?

0

How to list more than 1 database data using JDBC in a ListView on Android? I have in a user 13 rows, and I would like to retrieve the 13 and not only 1. Below the code I use:

import android.os.Bundle;                      

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public  class Tela2 extends MyActivity {

    static TextView useron;

    static ListView listgeral;

    static EditText email, descricao, codigo;


    Button atualiza;


    TabHost.TabSpec spec1, spec2;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela2);


        //Crio um objeto tabhost
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        //pego a váriavel do tabhost para setar as configurações
        spec1 = tabHost.newTabSpec("Aba 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Meus Pacotes");


        spec2 = tabHost.newTabSpec("Aba 2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("Novo rastreamento");


        tabHost.addTab(spec1);
        tabHost.addTab(spec2);


        //Crio uma váriavel contendo os componentes "TextView"
        listgeral = (ListView) findViewById(R.id.valor1);


        atualiza = (Button) (findViewById(R.id.atualiza));


        //Crio um TextView de usuario online
        useron = (TextView) findViewById(R.id.usuario);


        //Seto o valor que é recebido da classe "MyActivity"
        useron.setText(dominios);



        String url = "jdbc:mysql://localhost";

        String driver = "com.mysql.jdbc.Driver";
        //String contendo o usu�rio do banco
        String userbanco = "kappauni";

        //String contendo a senha do usuario do banco
        String passbanco = "teste";

        java.sql.Connection con = null;

        try {
            Class.forName(driver).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            con = DriverManager.getConnection(url, userbanco, passbanco);
            Statement st = null;
            st = con.createStatement();
            ResultSet rs = null;

            rs = st.executeQuery("SELECT * FROM encomendas where dominio ='"+useron.getText()+"'");

while (rs.next()){


            String valorcodigo = rs.getString("codigorastreio");
                String  valordescricao = rs.getString("descricao");


                String[] stringcodigo = {valorcodigo.toString()+"\t"+"\t"+"\t"+"\t"+"\t"+valordescricao.toString()};


                ArrayAdapter<String> arraycodigo = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringcodigo);

                listgeral.setAdapter(arraycodigo);

}

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //Método que ao clicar no botão adicionar, ele vai adicionar os valores no banco


    public void Adicionar(View add) throws Exception {


        email = (EditText) findViewById(R.id.form1);


        descricao = (EditText) findViewById(R.id.form2);

        codigo = (EditText) findViewById(R.id.form3);


        BancoInsert conexaoinsert = new BancoInsert();

        conexaoinsert.insert();

    }


//BOtão de atualizar

    public void atualizar(View upd){


        Toast.makeText(Tela2.this,"Atualizou",Toast.LENGTH_SHORT).show();

    }


        }
    
asked by anonymous 02.11.2014 / 17:30

1 answer

1

Hello, I believe you are new to the Android world. It is neither possible nor recommended to access an external bank directly from an Android application. The recommended way to access external data of your application is through REST requests from your web server, your app will consume this data, being able to make other requests to update the database, but always through a web server. See this post on this:

  

link

A very good and simple lib for making REST calls is the retrofit

link

On your code, setAdapter () only needs to be done once, so you do not need to be inside the "for". The correct would be you pass an array of strings where each string is the tuple (line) of the bank.

[] s

    
05.11.2014 / 00:36