Guest User

Untitled

a guest
Jun 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. package com.reumann.examples;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.Statement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11.  
  12. public class PersistenceJDBC {
  13.  
  14. public List getSystems() {
  15. List<NPPSystem> results = new ArrayList<NPPSystem>(); //good practice to return empty collections, not null
  16. Connection conn = null;
  17. try {
  18. Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
  19. conn = DriverManager.getConnection("jdbc:jtds:sybase://d1npp.nielsenmedia.com:2025;DatabaseName=NPP_GUI", "npp_user", "npp_user");
  20. Statement stmt = conn.createStatement();
  21. ResultSet rs = stmt.executeQuery("SELECT nppSystemID, statusCodeID, nppSystemName, archiveDays, versionNumber, internalModeFlag FROM NPPSystem");
  22. while( rs.next() ) {
  23. results.add( new NPPSystem( rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getInt(4), rs.getString(5), rs.getString(6) ) );
  24. }
  25. //show results with for loop
  26. for(NPPSystem system: results ) {
  27. System.out.print("nppSystemID: "+ system.getNppSystemID() );
  28. System.out.println(" --- nppSystemName: "+ system.getNppSystemName() );
  29. }
  30.  
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. if ( conn != null ) {
  35. try {
  36. conn.close();
  37. } catch (SQLException ex) {
  38. ex.printStackTrace();
  39. }
  40. }
  41. }
  42. return results;
  43. }
  44.  
  45. }
  46.  
  47. class NPPSystem {
  48. /*
  49. * In theory you should be able to avoid getters and just have public properties, but many frameworks and usages actually
  50. * look to call the getters by reflection, so you really need the getters. For example, EL expressions
  51. * in JSP pages, need to have a getter backed bean, not just properties (unless the spec recently changed
  52. * that I'm unaware of
  53. */
  54. private int nppSystemID;
  55. private int statusCodeID;
  56. private String nppSystemName;
  57. private int archiveDays;
  58. private String versionNumber;
  59. private String internalModeFlag;
  60.  
  61. /* note that these have to be set in the correct order, and if you only want to se a few of the properties, you'll need
  62. * extra constructors, or just rely on setting properties manually. Compare this to groovy which has implied types of constructors that
  63. * you can just call like NPPSystem(nppSystemID: 4, nppSystemName: "foobar" )
  64. * I can just quickly set the fields I want in an object (in any order) using the above syntax without even needing a constructor
  65. */
  66. public NPPSystem(int nppSystemID, int statusCodeID, String nppSystemName, int archiveDays, String versionNumber, String internalModeFlag) {
  67. this.nppSystemID = nppSystemID;
  68. this.statusCodeID = statusCodeID;
  69. this.nppSystemName = nppSystemName;
  70. this.archiveDays = archiveDays;
  71. this.versionNumber = versionNumber;
  72. this.internalModeFlag = internalModeFlag;
  73. }
  74.  
  75.  
  76. public int getArchiveDays() {
  77. return archiveDays;
  78. }
  79.  
  80. public void setArchiveDays(int archiveDays) {
  81. this.archiveDays = archiveDays;
  82. }
  83.  
  84. public String getInternalModeFlag() {
  85. return internalModeFlag;
  86. }
  87.  
  88. public void setInternalModeFlag(String internalModeFlag) {
  89. this.internalModeFlag = internalModeFlag;
  90. }
  91.  
  92. public int getNppSystemID() {
  93. return nppSystemID;
  94. }
  95.  
  96. public void setNppSystemID(int nppSystemID) {
  97. this.nppSystemID = nppSystemID;
  98. }
  99.  
  100. public String getNppSystemName() {
  101. return nppSystemName;
  102. }
  103.  
  104. public void setNppSystemName(String nppSystemName) {
  105. this.nppSystemName = nppSystemName;
  106. }
  107.  
  108. public int getStatusCodeID() {
  109. return statusCodeID;
  110. }
  111.  
  112. public void setStatusCodeID(int statusCodeID) {
  113. this.statusCodeID = statusCodeID;
  114. }
  115.  
  116. public String getVersionNumber() {
  117. return versionNumber;
  118. }
  119.  
  120. public void setVersionNumber(String versionNumber) {
  121. this.versionNumber = versionNumber;
  122. }
  123.  
  124.  
  125. }
Add Comment
Please, Sign In to add comment