Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. public String getDatabase() {
  2.  
  3. String connectionString = "";
  4.  
  5. connectionString = "jdbc:sqlserver://semicoloniv.database.windows.net:1433;database=Data;user=SemicolonIV@semicoloniv;password=Semicolon04;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
  6.  
  7. // Declare the JDBC objects.
  8. Connection connection = null;
  9. Statement statement = null;
  10. ResultSet resultSet = null;
  11. PreparedStatement prepsInsertPerson = null;
  12. PreparedStatement prepsUpdateAge = null;
  13.  
  14. try {
  15. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  16. } catch (ClassNotFoundException e) {
  17. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  18. e.printStackTrace();
  19. }
  20.  
  21. try {
  22.  
  23. connection = DriverManager.getConnection(connectionString);
  24.  
  25. return "Connected";
  26.  
  27. } catch (Exception e) {
  28.  
  29. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  30. e.printStackTrace();
  31.  
  32. } finally {
  33. // Close the connections after the data has been handled.
  34. if (prepsInsertPerson != null) try {
  35. prepsInsertPerson.close();
  36. } catch (Exception e) {
  37. }
  38. if (prepsUpdateAge != null) try {
  39. prepsUpdateAge.close();
  40. } catch (Exception e) {
  41. }
  42. if (resultSet != null) try {
  43. resultSet.close();
  44. } catch (Exception e) {
  45. }
  46. if (statement != null) try {
  47. statement.close();
  48. } catch (Exception e) {
  49. }
  50. if (connection != null) try {
  51. connection.close();
  52. } catch (Exception e) {
  53. }
  54. }
  55.  
  56. return "Not Connected";
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement