Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:android:database_mysql [2018/05/02 16:00]
smayr [ItemAdapter]
swdev:android:database_mysql [2018/05/02 17:22] (current)
smayr
Line 1: Line 1:
 = Database: MySQL = = Database: MySQL =
 +== Using RESTful API ==
 +
 +== Using Java Connector (no API) ==
 ==== DB Connector ==== ==== DB Connector ====
 Download the appropriate Java database connector: Download the appropriate Java database connector:
Line 142: Line 145:
     ListView lstProducts;     ListView lstProducts;
     TextView lblProgress;     TextView lblProgress;
-    Map<String, Double> mapProducts = new LinkedHashMap<String, Double>();+    Map<String,Double> mapProducts = new LinkedHashMap<String,Double>();
    
     @Override     @Override
Line 176: Line 179:
              
       // Example: 192.168.0.2:3306       // Example: 192.168.0.2:3306
-      static final String DB_URL = "jdbc:mysql://"++      static final String DB_URL = "jdbc:mysql://" +
           DbStrings.DATABASE_URL + "/" +           DbStrings.DATABASE_URL + "/" +
           DbStrings.DATABASE_NAME;           DbStrings.DATABASE_NAME;
Line 195: Line 198:
                Class.forName(JDBC_DRIVER);                Class.forName(JDBC_DRIVER);
                conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);                conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);
-               stmt = conn.createStatement(); +                
-               String sql = "SELECT * FROM products";+               stmt         = conn.createStatement(); 
 +               String sql   = "SELECT * FROM products";
                ResultSet rs = stmt.executeQuery(sql);                ResultSet rs = stmt.executeQuery(sql);
 +               
                while(rs.next()) {                while(rs.next()) {
-                   String name = rs.getString("name");+                   String name  = rs.getString("name");
                    double price = rs.getDouble("price");                    double price = rs.getDouble("price");
                                        
Line 212: Line 217:
                                
            } catch (SQLException connError) {            } catch (SQLException connError) {
-              msg = "Exception was thrown for JDBC."; +               msg = "Exception was thrown for JDBC."; 
-              connError.printStackTrace();+               connError.printStackTrace();
            } catch (ClassNotFoundException ex) {            } catch (ClassNotFoundException ex) {
-              msg = "A 'Class not Found' exception was thrown."; +               msg = "A 'Class not Found' exception was thrown."; 
-              ex.printStackTrace();+               ex.printStackTrace();
            } finally {            } finally {
                try {                try {
                    if (stmt != null) { stmt.close(); }                    if (stmt != null) { stmt.close(); }
                } catch (SQLException ex) {                } catch (SQLException ex) {
-                   stmt.close();+                   ex.printStackTrace();
                }                }
                try {                try {
                    if (conn != null) { conn.close(); }                    if (conn != null) { conn.close(); }
                } catch (SQLException ex) {                } catch (SQLException ex) {
-                   conn.close();+                   ex.printStackTrace();
                }                }
            }            }
Line 234: Line 239:
        @Override        @Override
        protected void onPostExecute(String msg) {        protected void onPostExecute(String msg) {
 +
            lblProgress.setText(this.msg);            lblProgress.setText(this.msg);
 +           
            if (mapProducts.size() > 0) {            if (mapProducts.size() > 0) {
                itemAdapter = new ItemAdapter(thisContext, mapProducts);                itemAdapter = new ItemAdapter(thisContext, mapProducts);
-               lstProducts.setAdapter(itemAdapter());+               lstProducts.setAdapter(itemAdapter);
            }            }
        }        }
Line 348: Line 355:
  
 See: [[https://gist.github.com/cofearabi/5039135]] See: [[https://gist.github.com/cofearabi/5039135]]
 +
 +
 == References == == References ==
 See also: See also: