Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. import com.mysql.jdbc.jdbc2.optional.*;
  4.  
  5. import java.util.*;
  6.  
  7. import javax.naming.*;
  8. import javax.sql.DataSource;
  9.  
  10. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  11.  
  12. public class MainProgram {
  13. static MainProgram program = new MainProgram();
  14.  
  15. // ALL USER INPUTS "KNAPPAR"
  16.  
  17. UserHandler uh = new UserHandler();
  18.  
  19. // SKAPA KOPPLING TILL DATABAS (connection)
  20. // INITIERA STATEMENTS ETC
  21.  
  22. public static DataSource getMySQLDataSource() { // Funkar denna som connect
  23. MysqlDataSource mysqlDS = null;
  24.  
  25. try {
  26. mysqlDS = new MysqlDataSource();
  27. mysqlDS.setURL("jdbc:mysql://localhost:3306/mydb");
  28. mysqlDS.setUser("root");
  29. mysqlDS.setPassword("tgcmxzqw9");
  30.  
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return mysqlDS;
  35. }
  36.  
  37. public Connection getConnection() throws SQLException {
  38. Connection connection = null;
  39.  
  40. // Get the MySqlDataSource
  41.  
  42. System.out.println("Verifying access");
  43. DataSource dataSource = getMySQLDataSource();
  44.  
  45. // Get connection from the database
  46.  
  47. System.out.println("Connecting database...");
  48. connection = dataSource.getConnection();
  49.  
  50.  
  51. return connection;
  52.  
  53. }
  54.  
  55. private int getUserID(int inputUserID) {
  56.  
  57. PreparedStatement prstmt = null;
  58. int userID = -1;
  59. Connection connection = null;
  60. try {
  61. connection = getConnection();
  62. System.out.println("Database connected!");
  63. // Execute query
  64.  
  65. // egentligen ska prepareStatement innehålla "GET_USERNAME
  66.  
  67. String sql = "SELECT userName FROM USER WHERE userID = ?";
  68.  
  69. prstmt = connection.prepareStatement(sql);
  70.  
  71. prstmt.setInt(1, inputUserID);
  72.  
  73. ResultSet rs = prstmt.executeQuery();
  74.  
  75. while (rs.next()) {
  76.  
  77. // Retrieve by column name
  78.  
  79. userID = rs.getInt("userID");
  80.  
  81. // Display values
  82.  
  83. System.out.println("userID" + userID);
  84.  
  85. }
  86. rs.close();
  87. } catch (SQLException se) {
  88. se.printStackTrace();
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92.  
  93. finally {
  94. // Finally block used to close resources
  95. closePrstmt(prstmt);
  96. closeConnection(connection);
  97. }
  98. return userID;
  99. }
  100.  
  101. public void closePrstmt(PreparedStatement prstmt){
  102. try {
  103. if (prstmt != null) {
  104. prstmt.close();
  105. }
  106. } catch (SQLException sqlException) {
  107. sqlException.printStackTrace();
  108. }
  109.  
  110. }
  111.  
  112. public void closeConnection(Connection connection){
  113. try {
  114. if (connection != null) {
  115. connection.close();
  116. }
  117. } catch (SQLException sqlException) {
  118. sqlException.printStackTrace();
  119. }
  120. }
  121.  
  122. public static void main(String[] args) throws SQLException {
  123.  
  124.  
  125.  
  126. // program.testAllMethods();
  127. program.getUserID(0);
  128.  
  129. }
  130.  
  131. public void testAllMethods() throws SQLException {
  132. uh.getUsername(0);
  133. uh.setUsername(0);
  134. uh.getParticipantCounter(0);
  135. uh.increaseParticipantCounter(0);
  136. uh.getHostCounter(0);
  137. uh.increaseHostCounter(0);
  138. uh.getGender(0);
  139. uh.setGender(0);
  140. uh.getAge(0);
  141. uh.setAge(0);
  142. uh.getName(0);
  143. uh.setName(0);
  144. }
  145.  
  146.  
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement