Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. connection class"""
  2.  
  3. public class ConnectionClass {
  4. String classs;
  5. String db;
  6. String un;
  7. String password;
  8. String ip;
  9.  
  10. public ConnectionClass()
  11. {
  12. classs = "net.sourceforge.jtds.jdbc.Driver";
  13. db = "softwareEngineering";
  14. un = "giannis";
  15. password = "Fotinakis94";
  16. ip = "softwareengineering.database.windows.net:1433";
  17. }
  18.  
  19. public ConnectionClass(String Ip, String Classs, String Db, String Un, String Password)
  20. {
  21. ip = Ip;
  22. classs = Classs;
  23. db = Db;
  24. un = Un;
  25. password = Password;
  26. }
  27.  
  28. public String getIp()
  29. {
  30. return ip;
  31. }
  32.  
  33. public String getClasss(){
  34. return classs;
  35. }
  36.  
  37. public String getDb(){
  38. return db;
  39. }
  40.  
  41. public String getUn(){
  42. return un;
  43. }
  44. public String getPassword(){
  45. return password;
  46. }
  47.  
  48. public void setip(String Ip){
  49. ip = Ip;
  50. }
  51.  
  52. public void setdb(String Db)
  53. {
  54. db = Db;
  55. }
  56.  
  57. public void setclasss(String Classs){
  58. classs = Classs;
  59. }
  60.  
  61. public void setun(String Un){
  62. un = Un;
  63. }
  64.  
  65. public void setpassword(String Password){
  66. password = Password;
  67. }
  68. }
  69.  
  70. ///////// main actity
  71.  
  72. import java.sql.Connection;
  73. import java.sql.DriverManager;
  74. import java.sql.ResultSet;
  75. import java.sql.SQLException;
  76. import java.sql.Statement;
  77. import java.util.ArrayList;
  78. import java.util.HashMap;
  79. import java.util.List;
  80. import java.util.Map;
  81.  
  82. import android.annotation.SuppressLint;
  83. import android.content.Intent;
  84. import android.graphics.Typeface;
  85. import android.os.Bundle;
  86. import android.os.StrictMode;
  87. import android.support.v7.app.ActionBarActivity;
  88. import android.util.Log;
  89. import android.view.View;
  90. import android.widget.AdapterView;
  91. import android.widget.AdapterView.OnItemClickListener;
  92. import android.widget.Button;
  93. import android.widget.ListView;
  94. import android.widget.SimpleAdapter;
  95. import android.widget.TextView;
  96. import android.widget.Toast;
  97. import android.support.v7.app.AppCompatActivity;
  98.  
  99.  
  100. public class MainActivity extends AppCompatActivity {
  101.  
  102. TextView lblheader;
  103. Typeface font;
  104. Button btn;
  105. ListView lstroadWorks;
  106. SimpleAdapter ADAhere;
  107.  
  108. /****************************Database Connection Variables*************************************/
  109.  
  110. ConnectionClass connectionclass;
  111. String usernameS;
  112. String datets;
  113. String call, db, un, passwords;
  114. Connection connect;
  115. ResultSet rs;
  116.  
  117. @SuppressLint("NewApi")
  118. private Connection CONN(String _user, String _pass, String _DB, String _server)
  119. {
  120. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  121. StrictMode.setThreadPolicy(policy);
  122. Connection conn = null;
  123. String ConnURL = null;
  124. try{
  125. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  126. ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
  127. + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
  128. conn = DriverManager.getConnection(ConnURL);
  129. }catch (SQLException se){
  130. Log.e("ERROR", se.getMessage());
  131. }catch (ClassNotFoundException e){
  132. Log.e("ERROR", e.getMessage());
  133. }catch (Exception e){
  134. Log.e("ERROR", e.getMessage());
  135. }
  136. return conn;
  137. }
  138.  
  139. @Override
  140. protected void onCreate(Bundle savedInstanceState) {
  141. super.onCreate(savedInstanceState);
  142. setContentView(R.layout.activity_main);
  143.  
  144. lblheader = (TextView) findViewById(R.id.lblheader);
  145. lstroadWorks = (ListView) findViewById(R.id.lstRoadWorks);
  146. btn = (Button) findViewById(R.id.btnview);
  147.  
  148. /***********************DATABASE CONNECTION VARIABLE***************************************/
  149. connectionclass = new ConnectionClass();
  150. call = connectionclass.getIp();
  151. un = connectionclass.getUn();
  152. passwords = connectionclass.getPassword();
  153. db = connectionclass.getDb();
  154. connect = CONN(un, passwords, db, call);
  155. btn.setOnClickListener(new View.OnClickListener(){
  156. @Override
  157. public void onClick(View v){
  158. //TODO Auto-generated method stub
  159. String querycmd = "select * from trafficNews2";
  160. try{
  161. Statement statement = connect.createStatement();
  162. rs = statement.executeQuery(querycmd);
  163. List<Map<String, String>> data = null;
  164. data = new ArrayList<Map<String, String>>();
  165.  
  166. while (rs.next())
  167. {
  168. Map<String, String> datanum = new HashMap<String, String>();
  169. datanum.put("A", rs.getString("streetName"));
  170. datanum.put("B", rs.getString("startTime"));
  171. datanum.put("C", rs.getString("stopTime"));
  172. datanum.put("D", rs.getString("notes"));
  173. datanum.put("E", rs.getString("loc_long"));
  174. datanum.put("F", rs.getString("loc_lat"));
  175.  
  176.  
  177.  
  178.  
  179. data.add(datanum);
  180. }
  181. String[] fromwhere = {"A", "B", "C", "D", "E", "F"};
  182.  
  183.  
  184. int[] viewswhere = {R.id.lblcountryname, R.id.lblStart, R.id.lblStop};
  185.  
  186. ADAhere = new SimpleAdapter(MainActivity.this, data, R.layout.listtemplate, fromwhere, viewswhere);
  187. lstroadWorks.setAdapter(ADAhere);
  188. }catch (SQLException e){
  189. Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
  190. }
  191. }
  192. });
  193.  
  194. lstroadWorks.setOnItemClickListener(new OnItemClickListener() {
  195. @Override
  196. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  197. //TODO Auto-generated method stub
  198. HashMap<String, Object> obj = (HashMap<String, Object>) ADAhere.getItem(position);
  199. String VechicleId = (String) obj.get("A");
  200. String startTime = (String) obj.get("B");
  201. String stoptTime = (String) obj.get("C");
  202. String notes = (String) obj.get("D");
  203. String loclong = (String) obj.get("E");
  204. String loclat = (String) obj.get("F");
  205.  
  206.  
  207. Toast.makeText(MainActivity.this, VechicleId, Toast.LENGTH_LONG).show();
  208. Intent in = new Intent(MainActivity.this,Main2Activity.class);
  209. in.putExtra("value",VechicleId);
  210. in.putExtra("start",startTime);
  211. in.putExtra("stop",stoptTime);
  212. in.putExtra("note",notes);
  213.  
  214. in.putExtra("longitude", loclong);
  215. in.putExtra("latitude", loclat);
  216.  
  217.  
  218. startActivity(in);
  219.  
  220. }
  221. });
  222. }
  223. }
  224.  
  225. $$$ mainactivity
  226.  
  227. package com.example.mario.roadworks;
  228.  
  229.  
  230. import android.content.Intent;
  231. import android.support.v7.app.AppCompatActivity;
  232. import android.os.Bundle;
  233. import android.view.View;
  234. import android.widget.TextView;
  235.  
  236. public class Main2Activity extends AppCompatActivity {
  237. Double longitude;
  238. Double latituble;
  239. @Override
  240. protected void onCreate(Bundle savedInstanceState) {
  241. super.onCreate(savedInstanceState);
  242. setContentView(R.layout.activity_main2);
  243.  
  244. Bundle bundle = getIntent().getExtras();
  245. String t = bundle.getString("value");
  246. String sta = bundle.getString("start");
  247. String sto = bundle.getString("stop");
  248. String not = bundle.getString("note");
  249. String loc = bundle.getString("longitude");
  250. String lat = bundle.getString("latitude");
  251. longitude = Double.parseDouble(loc);
  252. latituble = Double.parseDouble(lat);
  253.  
  254. TextView title = (TextView) findViewById(R.id.txtTitle);
  255. TextView start = (TextView) findViewById(R.id.txtStartTime);
  256. TextView stop = (TextView) findViewById(R.id.txtStopTime);
  257. TextView note = (TextView) findViewById(R.id.txtNotes);
  258.  
  259. title.setText(t);
  260. start.setText(sta);
  261. stop.setText(sto);
  262. note.setText(not);
  263.  
  264.  
  265. }
  266. public void goToMap(View view)
  267. {
  268. Intent intent = new Intent(Main2Activity.this, MapsActivity.class);
  269. intent.putExtra("longitude",longitude);
  270. intent.putExtra("latitude",latituble);
  271. startActivity(intent);
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement