java - How to retrieve sql azure data into a list view? -
string[] items = new string[10]; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // create variable connection string. string connectionurl = "jdbc:sqlserver://servername.database.windows.net;" + "databasename=school;user=username@servername;password=userpassword"; // declare jdbc objects. connection con = null; statement stmt = null; resultset rs = null; setlistadapter(new arrayadapter<string>(this, android.r.layout.list_item, new arraylist())); new addstringtask().execute(); try { // establish connection. class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); con = drivermanager.getconnection(connectionurl); // create , execute sql statement returns data. string sql = "select top 10 * dbo.tbl"; stmt = con.createstatement(); rs = stmt.executequery(sql); // iterate through data in result set , display it. while (rs.next()) { string items = rs.getarray(2) + " " + rs.getarray(3); } } // handle errors may have occurred. catch (exception e) { e.printstacktrace(); } { if (rs != null) try { rs.close(); } catch(exception e) {} if (stmt != null) try { stmt.close(); } catch(exception e) {} if (con != null) try { con.close(); } catch(exception e) {} } } class addstringtask extends asynctask<void, string, void> { @override protected void doinbackground(void... unused) { return(null); } @override protected void onprogressupdate(string... item) { ((arrayadapter)getlistadapter()).add(item[0]); } @override protected void onpostexecute(void unused) { toast .maketext(asyncdemo.this, "done!", toast.length_short) .show(); } } in while loop inside oncreate 10 rows , 2 columns table in remote database sql azure store in string array. string array gets outputted user list view. jdbc driver download link min.bz/wc4am (non-windows users compressed file. last 1 listed)also updated link.
android-java-sqlazure combination, congrats!
on other hand, why access database (sql azure) directly mobile app? has few drawbacks:
- you need include sql server adapter , jdbc libraries on mobile application, gonna take @ least 1 mb in size.
- your app users able see database password few tricks. big security vulnerability system! users can drop tables.
- mobile apps (especially android) should not preferred consume database directly (since android activity cycle policy, apps can pause time , database connection might dropped meanwhile, , various unexpected exceptions due reason.).
therefore should create web service or rest api java or asp.net can execute queries through proxy , gives results in json, or xml or format java can parse , adapt arrayadapter.
Comments
Post a Comment