Guest User

Untitled

a guest
Dec 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import android.annotation.SuppressLint;
  2. import android.os.Bundle;
  3. import android.os.StrictMode;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6. import android.widget.GridView;
  7. import android.widget.SimpleAdapter;
  8. import android.widget.Toast;
  9.  
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20.  
  21. public class GridViewActivity extends AppCompatActivity {
  22.  
  23. GridView gridview;
  24. ArrayList<String> arrayList;
  25.  
  26. String ip, db, un, passwords;
  27. Connection connect;
  28. PreparedStatement stmt;
  29. ResultSet rs;
  30.  
  31. @Override
  32.  
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_grid_view);
  36.  
  37. ip = "MyServerIP";
  38. un = "MyUserId";
  39. passwords = "MyPass";
  40. db = "Mydb";
  41.  
  42. gridview = (GridView) findViewById(R.id.gridview);
  43.  
  44. connect = CONN(un, passwords, db, ip);
  45. String query = "select user,password from Usertbl";
  46.  
  47.  
  48. try {
  49.  
  50. connect = CONN(un, passwords, db, ip);
  51. Statement statement = connect.createStatement();
  52. rs = statement.executeQuery(query);
  53. List<Map<String, String>> data = null;
  54. data = new ArrayList<Map<String, String>>();
  55.  
  56. while (rs.next()) {
  57.  
  58. Map<String, String> datanum = new HashMap<String, String>();
  59. datanum.put("A", rs.getString("user"));
  60. datanum.put("B", rs.getString("password"));
  61.  
  62. data.add(datanum);
  63. }
  64.  
  65. String[] from = { "A", "B" };
  66. int[] views = { R.id.txtcountry, R.id.txtcontinent };
  67. final SimpleAdapter ADA = new SimpleAdapter(GridViewActivity.this,data, R.layout.activity_grid_view, from, views);
  68.  
  69. gridview.setAdapter(ADA);
  70.  
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73.  
  74. }
  75.  
  76. }
  77.  
  78. @SuppressLint("NewApi")
  79.  
  80. private Connection CONN(String _user, String _pass, String _DB,String _server) {
  81.  
  82. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  83.  
  84. StrictMode.setThreadPolicy(policy);
  85. Connection conn = null;
  86. String ConnURL = null;
  87.  
  88. try {
  89.  
  90. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  91. ConnURL = "jdbc:jtds:sqlserver://MyServerIP:MyPort;databaseName=Mydb;instance=MySqlServer;user=MyUserId;password=MyPass";
  92. conn = DriverManager.getConnection(ConnURL);
  93.  
  94. if (conn == null){
  95. Toast.makeText(GridViewActivity.this,"Database Connection failed",Toast.LENGTH_LONG).show();
  96. }
  97. else {
  98. Toast.makeText(GridViewActivity.this,"Database Connection Success",Toast.LENGTH_LONG).show();
  99. }
  100.  
  101. } catch (SQLException se) {
  102.  
  103. Log.e("ERRO", se.getMessage());
  104.  
  105. } catch (ClassNotFoundException e) {
  106.  
  107. Log.e("ERRO", e.getMessage());
  108.  
  109. } catch (Exception e) {
  110.  
  111. Log.e("ERRO", e.getMessage());
  112.  
  113. }
  114.  
  115. return conn;
  116.  
  117. }
  118.  
  119. }
Add Comment
Please, Sign In to add comment