Guest User

Untitled

a guest
Nov 5th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package com.example.jopri.databaseconnectie;
  2.  
  3. import android.content.AsyncQueryHandler;
  4. import android.content.Context;
  5. import android.content.res.Resources;
  6. import android.os.AsyncTask;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ListView;
  12. import android.widget.TextView;
  13.  
  14. import java.sql.Connection;
  15. import java.sql.Driver;
  16. import java.sql.DriverManager;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23.  
  24. public class MainActivity extends AppCompatActivity {
  25.  
  26. ItemAdapter itemAdapter;
  27. Context thisContext;
  28. ListView myListView;
  29. TextView progressTextView;
  30. Map<String, Double> fruitsMap = new LinkedHashMap<String, Double>();
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36.  
  37. Resources res = getResources();
  38. myListView = (ListView) findViewById(R.id.myListView);
  39. progressTextView = (TextView) findViewById(R.id.progressTextView);
  40. thisContext = this;
  41.  
  42. progressTextView.setText("");
  43. Button btn = (Button) findViewById(R.id.getDataButton);
  44. btn.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v){
  47. GetData retrieveData = new GetData();
  48. retrieveData.execute("");
  49. }
  50. });
  51.  
  52. }
  53.  
  54. private class GetData extends AsyncTask<String, String, String> {
  55.  
  56. String msg = "";
  57.  
  58. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  59.  
  60. static final String DB_URL = "jdbc:mysql://" +
  61. DbStrings.DATABASE_URL + "/" +
  62. DbStrings.DATABASE_NAME;
  63.  
  64. protected void onPreExecute() {
  65. progressTextView.setText("Connecting to database...");
  66. }
  67.  
  68. @Override
  69. protected String doInBackground(String... params) {
  70.  
  71. Connection conn = null;
  72. Statement stmt = null;
  73.  
  74. try {
  75. Class.forName(JDBC_DRIVER);
  76. conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);
  77.  
  78. stmt = conn.createStatement();
  79. String sql = "SELECT * FROM fruit";
  80. ResultSet rs = stmt.executeQuery(sql);
  81.  
  82. while (rs.next()) {
  83. String soort = rs.getString("soort");
  84. double prijs = rs.getDouble("prijs");
  85.  
  86. fruitsMap.put(soort, prijs);
  87. }
  88.  
  89. msg = "Process complete";
  90.  
  91. rs.close();
  92. stmt.close();
  93. conn.close();
  94.  
  95. } catch (SQLException connError) {
  96. msg = "An exception was thrown for JDBC";
  97. connError.printStackTrace();
  98. } catch (ClassNotFoundException e) {
  99. msg = "A class not found exception was thrown.";
  100. e.printStackTrace();
  101. } finally {
  102.  
  103. try {
  104. if (stmt != null) {
  105. stmt.close();
  106. }
  107.  
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. try {
  112. if (conn != null) {
  113. conn.close();
  114. }
  115.  
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119.  
  120. }
  121.  
  122. return null;
  123. }
  124.  
  125. @Override
  126. protected void onPostExecute(String msg) {
  127. progressTextView.setText(this.msg);
  128.  
  129. if(fruitsMap.size() > 0) {
  130. itemAdapter = new ItemAdapter(thisContext, fruitsMap);
  131. myListView.setAdapter(itemAdapter);
  132. }
  133. }
  134. }
  135.  
  136. } // END
  137.  
  138. package com.example.jopri.databaseconnectie;
  139.  
  140. import java.net.URL;
  141.  
  142. public class DbStrings {
  143.  
  144. static final String DATABASE_URL = "127.0.0.1:3306";
  145. static final String DATABASE_NAME = "proef";
  146. static final String USERNAME = "root";
  147. static final String PASSWORD = "password";
  148. }
  149.  
  150. package com.example.jopri.databaseconnectie;
  151.  
  152. import android.content.Context;
  153. import android.view.LayoutInflater;
  154. import android.view.View;
  155. import android.view.ViewGroup;
  156. import android.widget.BaseAdapter;
  157. import android.widget.TextView;
  158.  
  159. import java.util.ArrayList;
  160. import java.util.List;
  161. import java.util.Map;
  162.  
  163. public class ItemAdapter extends BaseAdapter {
  164.  
  165.  
  166. LayoutInflater mInflator;
  167. Map<String, Double> map;
  168. List<String> soort;
  169. List<Double> prijs;
  170.  
  171. public ItemAdapter(Context c, Map m) {
  172. mInflator = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  173. map = m;
  174. soort = new ArrayList<String>(map.keySet());
  175. prijs = new ArrayList<Double>(map.values());
  176.  
  177. };
  178.  
  179. @Override
  180. public int getCount() {
  181. return map.size();
  182. }
  183.  
  184. @Override
  185. public Object getItem(int position) {
  186. return soort.get(position);
  187. }
  188.  
  189. @Override
  190. public long getItemId(int position) {
  191. return position;
  192. }
  193.  
  194. @Override
  195. public View getView(int position, View convertView, ViewGroup parent) {
  196.  
  197. View v = mInflator.inflate(R.layout.item_layout, null);
  198. TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
  199. TextView priceTextView = (TextView) v.findViewById(R.id.priceTextView);
  200.  
  201. nameTextView.setText(soort.get(position));
  202. priceTextView.setText("€" + prijs.get(position).toString());
  203.  
  204. return v;
  205. }
  206.  
  207. }
  208.  
  209. static final String DATABASE_URL = "127.0.0.1:3306";
Add Comment
Please, Sign In to add comment