有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Android中的Sql查询

我有很多问题没有找到答案

首先,我在MS Sql server中有两个表,一个叫做country,包含国家id和国家名称,另一个叫做family,包含国家id、家庭id、家庭名称和家庭信息。我面临的问题是,如何用Java Android编写一个查询,获取国家的id,并将其与families表中的国家id进行比较,然后每个国家都有自己的家庭

我试过内部连接,但不知道怎么做

以下是我目前的代码:

 public class InsideCountries extends AppCompatActivity {

        private ArrayList<ClassListItems> itemArrayList;  //List items Array
        private MyAppAdapter myAppAdapter; //Array Adapter
        private ListView listView;
        private TextView ttl;// Listview
        private boolean success = false; // boolean
        private ConnectionClass connectionClass; //Connection Class Variable

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_inside_countries);
            getSupportActionBar().hide();
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            ttl = (TextView) findViewById(R.id.title) ;
            ttl.setText(getIntent().getStringExtra("CountriesName"));
            listView = (ListView) findViewById(R.id.listvieww); //Listview Declaration
            connectionClass = new ConnectionClass(); // Connection Class Initialization
            itemArrayList = new ArrayList<ClassListItems>(); // Arraylist Initialization

            // Calling Async Task
            SyncData orderData = new SyncData();
            orderData.execute("");
        }

        // Async Task has three overrided methods,
        private class SyncData extends AsyncTask<String, String, String> {
            String msg = "No Data Found";
            ProgressDialog progress;

            @Override
            protected void onPreExecute() //Starts the progress dailog
            {
                progress = ProgressDialog.show(InsideCountries.this, "Synchronising",
                        "Please Wait...", true);
            }

            @Override
            protected String doInBackground(String... strings)  // Connect to the database, write query and add items to array list
            {
                try {
                    Connection conn = connectionClass.CONN(); //Connection Object
                    if (conn == null) {
                        msg = "Please Check Your Connection";
                        success = false;
                    } else {
                        // Change below query according to your own database.

                        String query = "query here";
                            Statement stmt = conn.createStatement();
                            ResultSet rs = stmt.executeQuery(query);




                            if (rs != null) // if resultset not null, I add items to itemArraylist using class created
                            {
                                while (rs.next()) {
                                    try {
                                        itemArrayList.add(new ClassListItems(rs.getString("name")));
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                                msg = "Found";
                                success = true;
                            } else {
                                msg = "No Data found!";
                                success = false;
                            }
                        }
                    } catch(Exception e)
                    {
                        e.printStackTrace();
                        Writer writer = new StringWriter();
                        e.printStackTrace(new PrintWriter(writer));
                        msg = writer.toString();
                        success = false;
                    }
                    return msg;
                }

                @Override
                protected void onPostExecute (String
                msg) // disimissing progress dialoge, showing error and setting up my listview
                {
                    progress.dismiss();
                    Toast.makeText(InsideCountries.this, msg + "", Toast.LENGTH_LONG).show();
                    if (success == false) {
                    } else {
                        try {
                            myAppAdapter = new MyAppAdapter(itemArrayList, InsideCountries.this);
                            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                            listView.setAdapter(myAppAdapter);
                        } catch (Exception ex) {

                        }

                    }
                }
            }

        public class MyAppAdapter extends BaseAdapter         //has a class viewholder which holds
        {
            public class ViewHolder
            {
                TextView textName;
            }

            public List<ClassListItems> parkingList;

            public Context context;
            ArrayList<ClassListItems> arraylist;

            private MyAppAdapter(List<ClassListItems> apps, Context context)
            {
                this.parkingList = apps;
                this.context = context;
                arraylist = new ArrayList<ClassListItems>();
                arraylist.addAll(parkingList);
            }

            @Override
            public int getCount() {
                return parkingList.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
            {

                View rowView = convertView;
                ViewHolder viewHolder= null;
                if (rowView == null)
                {
                    LayoutInflater inflater = getLayoutInflater();
                    rowView = inflater.inflate(R.layout.list_content, parent, false);
                    viewHolder = new ViewHolder();
                    viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
                    rowView.setTag(viewHolder);

                }
                else
                {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                // here setting up names and images
                viewHolder.textName.setText(parkingList.get(position).getName()+"");
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(InsideCountries.this,Inside_Families.class);
                        intent.putExtra("Family",parkingList.get(position).getName());
                        startActivity(intent);
                    }
                });

                return rowView;
            }
        }

    }

任何帮助都将被感激


共 (0) 个答案