Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.".
- import java.sql.*;
- public class DatabaseHelper {
- public static void main(String[] args) throws Exception {
- String custID = "1";
- 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;";
- String queryString = "SELECT FirstName, LastName FROM Tbl_Customer WHERE CustomerID = " + custID;
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- Connection myConnection = DriverManager.getConnection(connectionString);
- Statement myStatement = myConnection.createStatement();
- ResultSet myResultSet = myStatement.executeQuery(queryString);
- myResultSet.next();
- String firstName = myResultSet.getString("FirstName");
- String lastName = myResultSet.getString("LastName");
- System.out.println(firstName);
- System.out.println(lastName);
- myStatement.close();
- myConnection.close();
- }
- }
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.TextView;
- import java.sql.*;
- public class LandingPage extends AppCompatActivity {
- TextView txtFirst, txtLast;
- String firstName, lastName, customerID;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_landing_page);
- txtFirst = (TextView)findViewById(R.id.txtFirstName);
- txtLast = (TextView)findViewById(R.id.txtLastName);
- customerID = getIntent().getStringExtra("custID");
- new myAsyncTask(LandingPage.this).execute();
- txtFirst.setText(firstName);
- txtLast.setText(lastName);
- }
- public class myAsyncTask extends AsyncTask<Void, Void, String> {
- 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;";
- String queryString = "SELECT FirstName, LastName FROM Tbl_Customer WHERE CustomerID = " + customerID;
- Context context;
- public myAsyncTask(Context context) {
- System.out.println("Async instantiated.");
- this.context = context;
- }
- protected void onPreExecute() {
- System.out.println("Pre-executing Async task.");
- }
- protected String doInBackground(Void... params) {
- try {
- System.out.println("Attempting to connect to DB...");
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- Connection con = DriverManager.getConnection(connectionString);
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery(queryString);
- while (rs.next()) {
- System.out.println("Sorting through ResultSet...");
- firstName = rs.getString("FirstName");
- lastName = rs.getString("LastName");
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return "Complete";
- }
- protected void onPostExecute(String result) {
- if (result.equals("Complete")) {
- System.out.println("Async task complete.");
- }
- }
- }
- }
- Connection con = DriverManager.getConnection(connectionString);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etID = (EditText)findViewById(R.id.etCustID);
- btnSubmit = (Button)findViewById(R.id.btnSubmit);
- btnSubmit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- customerID = etID.getText().toString();
- myIntent = new Intent(MainActivity.this, LandingPage.class);
- myIntent.putExtra("custID", customerID);
- startActivity(myIntent);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement