How to List more than one bank line in a Listview using JDBC?

Asked

Viewed 685 times

0

How to list more than 1 database data using JDBC on a Listview on Android? I have in a user 13 lines, and would like to recover the 13 and not just 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();

    }


        }
  • 1

    Do you give this lot of space in your code even or got badly formatted when posted here? It gets hard to keep track of the code.

  • 1

    Using Mysql localhost database on an Android device? Android itself has Sqlite, why not use it?

  • It is only for testing, because the same will be used in my hosting as I already used.

1 answer

1

Hello, I believe you are new in the world Android. It is neither possible nor recommended to access an external database 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 and may make other requests to update the database, but always through a web server. See this post (in English) about this:

https://stackoverflow.com/a/12233178/2129877

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

http://square.github.io/retrofit/

About your code, setAdapter() only needs to be done once, so you don’t need to be in the "for". Correct would be you pass an array of strings where each string is the tuple (line) of the database.

[]s

  • Possible until it is... But, as you said, it is not recommended. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.