Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:android:database_mysql [2018/05/02 15:48] smayr [MainActivity] |
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 48: | Line 51: | ||
public class DbStrings | public class DbStrings | ||
{ | { | ||
- | | + | |
- | static final String DATABASE_NAME = " | + | static final String DATABASE_NAME = " |
- | static final String USERNAME | + | static final String USERNAME |
- | static final String PASSWORD | + | static final String PASSWORD |
} | } | ||
</ | </ | ||
Line 84: | Line 87: | ||
map = m; | map = m; | ||
products = new ArrayList< | products = new ArrayList< | ||
- | prices | + | prices |
| | ||
mInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | mInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
Line 142: | Line 145: | ||
ListView lstProducts; | ListView lstProducts; | ||
TextView lblProgress; | TextView lblProgress; | ||
- | Map< | + | Map< |
@Override | @Override | ||
Line 176: | Line 179: | ||
| | ||
// Example: 192.168.0.2: | // Example: 192.168.0.2: | ||
- | static final String DB_URL = " | + | static final String DB_URL = " |
DbStrings.DATABASE_URL + "/" | DbStrings.DATABASE_URL + "/" | ||
DbStrings.DATABASE_NAME; | DbStrings.DATABASE_NAME; | ||
Line 195: | Line 198: | ||
| | ||
conn = DriverManager.getConnection(DB_URL, | conn = DriverManager.getConnection(DB_URL, | ||
- | stmt = conn.createStatement(); | + | |
- | | + | stmt |
+ | | ||
| | ||
+ | |||
| | ||
- | | + | |
| | ||
Line 212: | Line 217: | ||
} catch (SQLException connError) { | } catch (SQLException connError) { | ||
- | | + | msg = " |
- | connError.printStackTrace(); | + | |
} catch (ClassNotFoundException ex) { | } catch (ClassNotFoundException ex) { | ||
- | | + | msg = "A 'Class not Found' exception was thrown."; |
- | 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: | ||
| | ||
| | ||
+ | |||
| | ||
+ | |||
if (mapProducts.size() > 0) { | if (mapProducts.size() > 0) { | ||
| | ||
- | | + | |
} | } | ||
} | } | ||
Line 245: | Line 252: | ||
=== Example Quick Connection === | === Example Quick Connection === | ||
+ | '' | ||
+ | <code xml> | ||
+ | < | ||
+ | xmlns: | ||
+ | android: | ||
+ | android: | ||
+ | tools: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | '' | ||
<code java> | <code java> | ||
package com.example.andmysql; | package com.example.andmysql; | ||
Line 326: | Line 353: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | See: [[https:// | ||
+ | |||
+ | |||
== References == | == References == | ||
See also: | See also: | ||
- | * [[swdev: | + | * [[swdev: |
- | * [[https://developer.android.com/training/ | + | * [[https://www.youtube.com/watch? |
- | * [[https:// | + | |
* [[https:// | * [[https:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// |