Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. LoanPrpcessing.jaba
  2.  
  3. package lab4;
  4.  
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. import com.mysql.jdbc.Connection;
  9.  
  10. public class LoanProcessing {
  11.  
  12. //"jdbc:mysql://www.papademas.net:3306/411labs?autoReconnect=true&useSSL=false"
  13.  
  14. private static String dbURL = "jdbc:mysql://localhost:3306/411labs?autoReconnect=true&useSSL=false";
  15. private static String username = "root";
  16. private static String password = "root";
  17.  
  18. public static Connection getConnection() throws SQLException {
  19.  
  20. Connection connection = null;
  21.  
  22. connection = (Connection) DriverManager.getConnection(dbURL, username, password);
  23. if(connection != null)
  24. System.out.println("connected");
  25.  
  26. return connection;
  27.  
  28. }
  29.  
  30.  
  31. }
  32.  
  33. Dao.Java
  34.  
  35. package lab4;
  36.  
  37. import java.sql.ResultSet;
  38. import java.sql.SQLException;
  39.  
  40. import com.mysql.jdbc.Connection;
  41. import com.mysql.jdbc.PreparedStatement;
  42. import com.mysql.jdbc.Statement;
  43.  
  44. public class Dao {
  45.  
  46. public static void createTable() throws SQLException {
  47.  
  48. Connection connection = LoanProcessing.getConnection();
  49.  
  50. Statement stmt = (Statement) connection.createStatement();
  51.  
  52. String sql = "CREATE TABLE IF NOT EXISTS fName_Lnam_tab " +
  53. "(id INTEGER not NULL, " +
  54. " income DOUBLE, " +
  55. " pep VARCHAR(255), " +
  56. " PRIMARY KEY ( id ))";
  57.  
  58. stmt.executeUpdate(sql);
  59. System.out.println("Created table in given database...");
  60.  
  61. }
  62.  
  63. public static void insertRecords(BankRecords[] records) throws SQLException {
  64.  
  65. int idCount = 1;
  66.  
  67. for(BankRecords bankRecord : records) {
  68.  
  69. String sql = "INSERT INTO fName_Lnam_tab (id, income, pep) VALUES (?, ?, ?)";
  70.  
  71. PreparedStatement statement = (PreparedStatement) LoanProcessing.getConnection().prepareStatement(sql);
  72. statement.setInt(1, idCount);
  73. statement.setDouble(2, bankRecord.getIncome());
  74. statement.setString(3, bankRecord.getPep());
  75.  
  76. int rowsInserted = statement.executeUpdate();
  77. if (rowsInserted > 0) {
  78. System.out.println("A new user was inserted successfully!");
  79. }
  80.  
  81. idCount++;
  82.  
  83. }
  84.  
  85.  
  86.  
  87. }
  88.  
  89. public static void retrieveRecords() throws SQLException {
  90.  
  91. String sql = "select id,income, pep from fName_Lnam_tab order by pep desc";
  92.  
  93. Statement statement = (Statement) LoanProcessing.getConnection().createStatement();
  94. ResultSet result = statement.executeQuery(sql);
  95.  
  96. int count = 0;
  97.  
  98. while (result.next()){
  99. int id = result.getInt("id");
  100. String income = result.getString("income");
  101. String pep = result.getString("pep");
  102.  
  103. String output = "User #%d: %s - %s - %s - %s";
  104. System.out.println(String.format(output, ++count, id, income, pep));
  105. }
  106.  
  107. }
  108.  
  109. }
  110.  
  111. 0 Comments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement