Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class DBConnect {
  2. private String dbValue;
  3. public String getDbValue() {
  4. return dbValue;
  5. }
  6. public void setDbValue(String dbValue) {
  7. this.dbValue = dbValue;
  8. }
  9. public static String callStoredProc() {
  10. InputStream input = null;
  11. ResultSet rs = null;
  12. PreparedStatement ps = null;
  13. Connection con = null;
  14. String result = null;
  15. try {
  16. String propPath = “.\src\props\db.properties; // property file
  17. input = new FileInputStream(propPath);
  18. Properties prop = new Properties();
  19. prop.load(input); // load property file
  20.  
  21. // get the property value
  22. String dbDriver = (prop.getProperty(“dbDriver”)); // driver from db.properties
  23. String dbURL = (prop.getProperty(“dbURL”));
  24.  
  25. Class.forName(dbDriver);
  26. con = DriverManager.getConnection(dbURL);
  27.  
  28. // Stored procedure call
  29. String stPro = (prop.getProperty(“SPSql1”));
  30. ps = con.prepareStatement(stPro);
  31. ps.setEscapeProcessing(true);
  32. ps.setQueryTimeout(90); // timeout value
  33. ps.setString(1, prop.getProperty(“VALUE1”));
  34. ps.setString(2, prop.getProperty(“VALUE2”));
  35. ps.setString(3, prop.getProperty(“VALUE3”));
  36. rs = ps.executeQuery();
  37. rs.next();
  38.  
  39.  
  40.  
  41. // Pick random value from DB range of 1-100
  42. int random = (int)(Math.random() * 100 + 1);
  43. int i=0;
  44. while (i < random) {
  45. rs.next();
  46. i++;
  47. }
  48. result = (rs.getString(“row_name”)); //prop.getProperty(“dbRow”)
  49. prop.getProperty(“dbRow”);
  50. prop.getProperty(“value2”);
  51.  
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. } catch (ClassNotFoundException e) {
  55. e.printStackTrace();
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. } finally {
  59. try {
  60. if (rs != null)
  61. input.close();
  62. if (rs != null)
  63. rs.close();
  64. if (ps != null)
  65. ps.close();
  66. if (con != null)
  67. con.close();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. return result;
  73. }
  74. }
  75. //end of class
  76.  
  77. //property file
  78. /*db.properties */
  79. dbDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver
  80. dbURL=jdbc:sqlserver:SERVER_URL;database=DB_NAME;user=USERNAME;password=PASSWORD
  81. VALUE1=aaa
  82. VALUE2=bbb
  83. VALUE3=ccc
  84. SPSql1={call storeprecdurecall (?,?,?)}
  85. dbRow=row_name
  86. DB_USERNAME=uname
  87. DB_PASSWORD=password
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement