Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. W/System.err: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host vet-to-go.database.windows.net, port 1433 has failed. Error: "Connection timed out: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
  2.  
  3. import java.sql.*;
  4.  
  5. public class DatabaseHelper {
  6.  
  7. public static void main(String[] args) throws Exception {
  8. String custID = "1";
  9. String connectionString = "jdbc:sqlserver://vet-to-go.database.windows.net:1433;database=Vet To Go DB;user=myLogin@vet-to-go;password=myPass;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
  10. String queryString = "SELECT FirstName, LastName FROM Tbl_Customer WHERE CustomerID = " + custID;
  11.  
  12. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  13. Connection myConnection = DriverManager.getConnection(connectionString);
  14. Statement myStatement = myConnection.createStatement();
  15. ResultSet myResultSet = myStatement.executeQuery(queryString);
  16.  
  17. myResultSet.next();
  18. String firstName = myResultSet.getString("FirstName");
  19. String lastName = myResultSet.getString("LastName");
  20.  
  21. System.out.println(firstName);
  22. System.out.println(lastName);
  23.  
  24. myStatement.close();
  25. myConnection.close();
  26. }
  27.  
  28. }
  29.  
  30. import android.content.Context;
  31. import android.os.AsyncTask;
  32. import android.os.Bundle;
  33. import android.support.v7.app.AppCompatActivity;
  34. import android.widget.TextView;
  35.  
  36. import java.sql.*;
  37.  
  38. public class LandingPage extends AppCompatActivity {
  39.  
  40. TextView txtFirst, txtLast;
  41. String firstName, lastName, customerID;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_landing_page);
  47. txtFirst = (TextView)findViewById(R.id.txtFirstName);
  48. txtLast = (TextView)findViewById(R.id.txtLastName);
  49.  
  50. customerID = getIntent().getStringExtra("custID");
  51.  
  52. new myAsyncTask(LandingPage.this).execute();
  53.  
  54. txtFirst.setText(firstName);
  55. txtLast.setText(lastName);
  56. }
  57.  
  58. public class myAsyncTask extends AsyncTask<Void, Void, String> {
  59. String connectionString = "jdbc:sqlserver://vet-to-go.database.windows.net:1433;database=Vet To Go DB;user=myLogin@vet-to-go;password=myPass;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
  60. String queryString = "SELECT FirstName, LastName FROM Tbl_Customer WHERE CustomerID = " + customerID;
  61. Context context;
  62.  
  63.  
  64. public myAsyncTask(Context context) {
  65. System.out.println("Async instantiated.");
  66. this.context = context;
  67. }
  68. protected void onPreExecute() {
  69. System.out.println("Pre-executing Async task.");
  70. }
  71.  
  72. protected String doInBackground(Void... params) {
  73. try {
  74. System.out.println("Attempting to connect to DB...");
  75. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  76. Connection con = DriverManager.getConnection(connectionString);
  77. Statement st = con.createStatement();
  78. ResultSet rs = st.executeQuery(queryString);
  79.  
  80. while (rs.next()) {
  81. System.out.println("Sorting through ResultSet...");
  82. firstName = rs.getString("FirstName");
  83. lastName = rs.getString("LastName");
  84. }
  85.  
  86. } catch (SQLException ex) {
  87. ex.printStackTrace();
  88. } catch (ClassNotFoundException ex) {
  89. ex.printStackTrace();
  90. } catch (Exception ex) {
  91. ex.printStackTrace();
  92. }
  93. return "Complete";
  94. }
  95.  
  96. protected void onPostExecute(String result) {
  97. if (result.equals("Complete")) {
  98. System.out.println("Async task complete.");
  99. }
  100. }
  101. }
  102. }
  103.  
  104. Connection con = DriverManager.getConnection(connectionString);
  105.  
  106. @Override
  107. protected void onCreate(Bundle savedInstanceState) {
  108. super.onCreate(savedInstanceState);
  109. setContentView(R.layout.activity_main);
  110.  
  111. etID = (EditText)findViewById(R.id.etCustID);
  112. btnSubmit = (Button)findViewById(R.id.btnSubmit);
  113.  
  114. btnSubmit.setOnClickListener(new View.OnClickListener() {
  115.  
  116. @Override
  117. public void onClick(View v) {
  118. customerID = etID.getText().toString();
  119. myIntent = new Intent(MainActivity.this, LandingPage.class);
  120. myIntent.putExtra("custID", customerID);
  121.  
  122. startActivity(myIntent);
  123. }
  124. });
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement