Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. private static final String url = "jdbc:mysql://172.0.0.1:3306/intalum";
  2. private static final String user = "pepe";
  3. private static final String pass = "123456";
  4. private Button buttonLoad;
  5. private TextView firstName;
  6.  
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. firstName = (TextView) findViewById(R.id.firstName);
  14. buttonLoad = (Button) findViewById(R.id.buttonLoad);
  15.  
  16. buttonLoad.setOnClickListener(new View.OnClickListener() {
  17.  
  18.  
  19. @Override
  20. public void onClick(View v) {
  21. new MyTask().execute();
  22.  
  23.  
  24. }
  25.  
  26.  
  27. });
  28. }
  29. private class MyTask extends AsyncTask<Void, Void, Void> {
  30. private String fName = "";
  31.  
  32. @Override
  33. protected Void doInBackground(Void... params) {
  34. try {
  35. Class.forName("com.mysql.jdbc.Driver");
  36. Connection con = DriverManager.getConnection(url, user, pass);
  37. Statement st = con.createStatement();
  38. String sql = "select * from cursos";
  39. final ResultSet rs = st.executeQuery(sql);
  40.  
  41.  
  42. rs.next();
  43.  
  44. fName = rs.getString(2);
  45.  
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49.  
  50. return null;
  51. }
  52.  
  53. @Override
  54.  
  55. protected void onPostExecute(Void result) {
  56.  
  57. firstName.setText(fName);
  58. super.onPostExecute(result);
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement