Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. // This method will connect to a SQL Server M.S. database and return a double array
  2. public static String[][] get() {
  3. int numRows;
  4. int numCols = 4;
  5. String dataTable[][] = null;
  6.  
  7. // Define connection string
  8. String connectionString;
  9. String server = "jdbc:sqlserver://sql2k801.discountasp.net:1433";
  10. String databaseName = "SQL2008_841902_tr";
  11. String username = "SQL2008_841902_tr_user";
  12. String password = "52645264hrm";
  13. connectionString = server + ";databasename=" + databaseName + ";username=" + username + ";password=" + password;
  14.  
  15. // Define database (this specifies whether SQL Server / MySQL / Oracle, etc)
  16. String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  17. String query = "select * from MortgageLoanTestData";
  18.  
  19. // Perform DB operations
  20. try {
  21. Class.forName(dbDriver);
  22. Connection oConnection = DriverManager.getConnection(connectionString);
  23. Statement oStatement = oConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  24. ResultSet oResult = oStatement.executeQuery(query);
  25.  
  26. // Set the size of the array
  27. oResult.last();
  28. numRows = oResult.getRow();
  29. dataTable = new String[numRows][numCols];
  30.  
  31. // Read the DB file while there is a next element
  32. oResult.beforeFirst();
  33. int row = 0;
  34. while (oResult.next()) {
  35. dataTable[row][0] = oResult.getString(1);
  36. dataTable[row][1] = oResult.getString(2);
  37. dataTable[row][2] = oResult.getString(3);
  38. dataTable[row][3] = oResult.getString(4);
  39. row++;
  40. }
  41.  
  42. } catch (SQLException | ClassNotFoundException e) {
  43. System.out.println("ERROR Handling SQL");
  44. e.printStackTrace();
  45. }
  46.  
  47. return dataTable;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement