简体   繁体   English

如何使用JDBC在ListView或TableView中显示来自SQL Server的数据

[英]How to display data from SQL Server in a ListView or TableView using JDBC

I have connected to my SQL Server using JTDS v.1.2.8 我已使用JTDS v.1.2.8连接到我的SQL Server

I am new with the JDBC Plugin and I am trying to figure it out. 我是JDBC插件的新手,我正在努力弄清楚。 I also did check the forums for a related question/answer but I couldn't find one, so here I am, asking :) 我也确实在论坛上检查了相关的问题/答案,但是找不到,所以在这里,我问:)

What I want to achieve is that I want to display the data I get from the SQL Database in a better way, like a listView or a tableView. 我想要实现的是我想以更好的方式显示从SQL数据库获得的数据,例如listView或tableView。

This is the code I'm using at the moment, i don't really know how to make it works 这是我目前正在使用的代码,我真的不知道如何使它工作

Class Model
    public class Model
{
    public String nom;
    public String prenom;
    public String mail;
    public String tel;
    public String date;
    public String caissier;
    public String vendeur;
    public String estheticienne;;

    // Getters and Setters omitted


}

///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////

 public class Magasin extends AppCompatActivity {
        ListView list;


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


    Button btn = (Button) findViewById(R.id.btnData);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            GetData retrieveData = new GetData();
            retrieveData.execute("");

        }
    });
}

private class GetData extends AsyncTask<String,String,String> {
    String msg = "";

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://" +
            DbStrings.DATABASE_URL + "/" + DbStrings.DATABASE_NAME;
    @Override
    protected void onPreExecute(){
    }
    @Override
    protected String doInBackground(String... params){
        Connection conn = null;
        Statement stmt = null;
        List<Model> list = new ArrayList<Model>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);

            stmt = conn.createStatement();
            String sql = "SELECT * FROM login";
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();



            while (rs.next()){
                Model utilisateur = new Model();
                utilisateur.prenom = rs.getString("prenom");
                utilisateur.nom = rs.getString("nom");
                utilisateur.mail = rs.getString("email");
                utilisateur.tel = rs.getString("tel");
                utilisateur.date = rs.getString("date");
                utilisateur.caissier = rs.getString("caissiere");
                utilisateur.vendeur = rs.getString("vendeuse");
                utilisateur.estheticienne = rs.getString("estheticienne");
                list.add(utilisateur);

            }
            msg = "Données récupérées !";

            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException connError){
            msg = "An exception was thrown for JDBC.";
            connError.printStackTrace();
        } catch (ClassNotFoundException e) {
            msg = "A class not found exception was thrown.";
            e.printStackTrace();

        }
        return msg;

    }

}

} }

Any comment or answer to point me in the solution of this is very appreciated. 非常感谢在此解决方案中指出我的任何意见或答案。

Thanks for your time. 谢谢你的时间。

You should create a ListView with the content rows and above a layout with the header row. 您应该创建一个具有内容行的ListView,并在一个具有标题行的布局上方。 And make the column division on the layout that you will use to represent each row on the ListView. 并在布局上进行列划分,以用于表示ListView上的每一行。

Other way is create a TableLayout inside a scroll! 另一种方法是在滚动中创建TableLayout!

Dont perform Server operation on main thread, here is a code from my project 不要在主线程上执行服务器操作,这是我项目中的代码

/*JDBC Connection URL*/
private static final String url = "jdbc:mysql://192.168.10.0:8000/etm";
/*JDBC Connection USER NAME*/
private static final String user = "admin";
/*JDBC Connection PASSWORD*/
private static final String pass = "admin";
/*ArrayList to store bus stop names*/
private static ArrayList<String> busStops;




private static class RetrieveSQL extends AsyncTask<Void, Void, ArrayList> {

        @Override
        protected ArrayList doInBackground(Void... voids) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (Exception e) {
                System.err.println("Cannot create connection");
            }
            Connection connection;
            busStops.clear();
            try {
                connection = DriverManager.getConnection(url, user, pass);
                Statement st = connection.createStatement();
                ResultSet rs = st.executeQuery("select * from stops");
                while (rs.next()) {
                    busStops.add(rs.getString("c_stop_name"));
                }
            } catch (Exception e) {
                System.err.println("Error");
            }
            return busStops;
        }

        @Override
        protected void onPostExecute(ArrayList busList) {
            super.onPostExecute(busList);
            mView.displayBusStopFromMySqlServer(busList);
        }
    }

onPostExecute() will return arraylist, pass it to the adapter. onPostExecute()将返回arraylist,并将其传递给适配器。

Use Recyclerview than Listview 使用Recyclerview而不是Listview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM