Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class DBUtil {
  4.  
  5. // Connecting to database.
  6. public static Connection getConnection() throws SQLException {
  7. String url = "jdbc:mysql://localhost:3306/database";
  8. String user = "root";
  9. String pass = "admin";
  10.  
  11. Connection connection = DriverManager.getConnection(url, user, pass);
  12. return connection;
  13. }
  14.  
  15. // Close database connection.
  16. public static void closeConnection(Connection con) {
  17. try {
  18. if(con != null) {
  19. con.close();
  20. System.out.println("Database Connection Closed!");
  21. }
  22. }catch(Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. // Close Prepared Statement.
  27. public static void closePrepStatement(PreparedStatement pStmt) {
  28. try {
  29. if(pStmt != null) {
  30. pStmt.close();
  31. System.out.println("Prepared Statemet Closed");
  32. }
  33. }catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. // Close Result Set.
  38. public static void closeResSet(ResultSet rSet) {
  39. try{
  40. if(rSet != null) {
  41. rSet.close();
  42. System.out.println("Result Set Closed");
  43. }
  44. }catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. import java.sql.Connection;
  51. import java.sql.PreparedStatement;
  52. import java.sql.ResultSet;
  53. import java.util.Scanner;
  54.  
  55. // SQL variables defined
  56. public class Source {
  57. DBUtil dbConnection = new DBUtil();
  58. Scanner input = new Scanner(System.in);
  59. Connection connection = null;
  60. PreparedStatement pStatement = null;
  61. ResultSet rSet = null;
  62.  
  63. // Constructor
  64. public Source() {
  65. getInput();
  66. displayData();
  67. }
  68. // Getting user input and saving to database
  69. private void getInput() {
  70.  
  71. System.out.println("What is your name?");
  72. String fName = input.nextLine();
  73. System.out.println("What is your second name?");
  74. String sName = input.nextLine();
  75. System.out.println("What is your proffesion?");
  76. String userProff = input.nextLine();
  77.  
  78. try {
  79. connection = DBUtil.getConnection();
  80. String sqlQuery = "INSERT INTO employees_info (firstname, secondname, proffesion) VALUES (?, ?, ?)";
  81. pStatement = connection.prepareStatement(sqlQuery);
  82. pStatement.setString(1, fName);
  83. pStatement.setString(2, sName);
  84. pStatement.setString(3, userProff);
  85. pStatement.executeUpdate();
  86. System.out.println("Record saved!");
  87.  
  88. }catch(Exception e) {
  89. e.printStackTrace();
  90. }finally {
  91. DBUtil.closePrepStatement(pStatement);
  92. DBUtil.closeConnection(connection);
  93. }
  94. }
  95. // Get data from database and display
  96. private void displayData() {
  97. System.out.println("Current records from the database:");
  98.  
  99. try {
  100. connection = DBUtil.getConnection();
  101. String sqlQuery = "SELECT * FROM employees_info";
  102. pStatement = connection.prepareStatement(sqlQuery);
  103. rSet = pStatement.executeQuery();
  104. while(rSet.next()) {
  105. System.out.println("");
  106. System.out.println("Next Record:");
  107. System.out.println(rSet.getInt("ID"));
  108. System.out.println(rSet.getString("firstname"));
  109. System.out.println(rSet.getString("secondname"));
  110. System.out.println(rSet.getString("proffesion"));
  111. }
  112. }catch (Exception e) {
  113. e.printStackTrace();
  114. }finally {
  115. DBUtil.closeResSet(rSet);
  116. DBUtil.closePrepStatement(pStatement);
  117. DBUtil.closeConnection(connection);
  118. }
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement