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/01 17:37] 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 29: | Line 32: | ||
==== Manifest ==== | ==== Manifest ==== | ||
- | Add permission to access the Internet. | + | Add permission to access the Internet |
<code xml> | <code xml> | ||
< | < | ||
< | < | ||
+ | < | ||
| | ||
< | < | ||
Line 47: | 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 83: | 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 141: | Line 145: | ||
ListView lstProducts; | ListView lstProducts; | ||
TextView lblProgress; | TextView lblProgress; | ||
- | Map< | + | Map< |
@Override | @Override | ||
Line 175: | 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 194: | Line 198: | ||
| | ||
conn = DriverManager.getConnection(DB_URL, | conn = DriverManager.getConnection(DB_URL, | ||
- | stmt = conn.createStatement(); | + | |
- | | + | stmt |
+ | | ||
| | ||
+ | |||
| | ||
- | | + | |
| | ||
Line 211: | 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 233: | Line 239: | ||
| | ||
| | ||
+ | |||
| | ||
+ | |||
if (mapProducts.size() > 0) { | if (mapProducts.size() > 0) { | ||
| | ||
- | | + | |
} | } | ||
} | } | ||
Line 242: | Line 250: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | === Example Quick Connection === | ||
+ | '' | ||
+ | <code xml> | ||
+ | < | ||
+ | xmlns: | ||
+ | android: | ||
+ | android: | ||
+ | tools: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | <code java> | ||
+ | package com.example.andmysql; | ||
+ | |||
+ | import java.sql.Connection; | ||
+ | import java.sql.DriverManager; | ||
+ | import java.sql.PreparedStatement; | ||
+ | import java.sql.ResultSet; | ||
+ | import java.sql.SQLException; | ||
+ | |||
+ | |||
+ | import android.os.Bundle; | ||
+ | import android.os.Handler; | ||
+ | import android.os.Message; | ||
+ | import android.app.Activity; | ||
+ | import android.widget.TextView; | ||
+ | |||
+ | public class MainActivity extends Activity implements Runnable | ||
+ | { | ||
+ | private String hiduke = ""; | ||
+ | private int price = 0; | ||
+ | private String errmsg = ""; | ||
+ | | ||
+ | public void run() | ||
+ | { | ||
+ | System.out.println(" | ||
+ | int count = 0; | ||
+ | | ||
+ | try { | ||
+ | Class.forName(" | ||
+ | Connection conn = DriverManager.getConnection (" | ||
+ | | ||
+ | try { | ||
+ | String sql; | ||
+ | //sql = " | ||
+ | sql = " | ||
+ | PreparedStatement qry = conn.prepareStatement(sql); | ||
+ | // | ||
+ | // | ||
+ | ResultSet rs = qry.executeQuery(); | ||
+ | while (rs.next()) { | ||
+ | hiduke = rs.getString(1); | ||
+ | price = rs.getInt(4); | ||
+ | count++; | ||
+ | System.out.println(hiduke + " | ||
+ | } | ||
+ | System.out.println(" | ||
+ | qry.close(); | ||
+ | conn.close(); | ||
+ | } catch (SQLException ex) { | ||
+ | System.out.println(" | ||
+ | errmsg = errmsg + ex.getMessage(); | ||
+ | } | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | errmsg = errmsg + ex.getMessage(); | ||
+ | } | ||
+ | |||
+ | handler.sendEmptyMessage(0); | ||
+ | } | ||
+ | | ||
+ | private Handler handler = new Handler() | ||
+ | { | ||
+ | public void handleMessage(Message msg) { | ||
+ | TextView textView = (TextView) findViewById(R.id.textView0); | ||
+ | textView.setText(" | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | | ||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) | ||
+ | { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | | ||
+ | Thread thread = new Thread(this); | ||
+ | thread.start(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | See: [[https:// | ||
+ | |||
== References == | == References == | ||
See also: | See also: | ||
- | * [[swdev: | + | * [[swdev: |
- | * [[https://developer.android.com/training/ | + | * [[https://www.youtube.com/watch? |
- | * [[https://developer.android.com/ | + | * [[https://www.simplifiedcoding.net/ |
+ | * [[http:// | ||
+ | * [[http:// |