Question
using Java, I need help doing the following: I already have the other files in eclipse I just need to create the following files and
using Java, I need help doing the following: I already have the other files in eclipse I just need to create the following files and need help in how to create them.
For this lab you will continue using your current project folder created for labs 2 & 3 and create 3 newsource files. Files are as follows:
File DbConnect: to allow an object to connect / close a database connection.
File Dao: where Dao stands for Data Access Object.This will allow for database connectivity and CRUD (Create Read Update Delete) like operations. Ref: for CRUD->Tutorialspoint.
File LoanProcessing:acts as a driver file (i.e., includes main function) to call your database CRUD methods and create some resulting output.
To start working with a database you will need a JDBC driver to allow for any connectivity within your app.
To include a driver for your MySQL database,you need to createa folder called libswithin your project. The MySQL driver for JDBC connections packages in the form of a jar file which you can download here:
the DBConnect.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
publicclass DBConnect {
// Code database URL
staticfinal String DB_URL = "jdbc:mysql://www.papademas.net/411labs?autoReconnect=true&useSSL=false";
// Database credentials
staticfinal String USER = "db411", PASS = "411";
public Connection connect() throws SQLException {
return DriverManager.getConnection(DB_URL, USER, PASS);
}
}
Remaining helper code below will not include imports for brevity, so please make sure to add them in(Source > Organize Imports)!
Dao.java
-Include the following class fields, constructor
//Declare DB objects
DBConnectconn = null;
Statement stmt = null;
// constructor
public Dao() { //create db object instance
conn = new DBConnect();
}
-Include a method to create a database table called createTable. createTable merely
creates a table when the method is called. Include the fields pid, id, income and pep
when building your table setup. A PRIMARY KEY which ensures record uniqueness is included for your build for the pid field which is shown below.
[ Note when creating a table, it is IMPERATIVE to include the following name:
yourFirstinitial_First4LettersOfYourLastName_tab]
// CREATE TABLE METHOD
publicvoid createTable() {
try {
// Open a connection
System.out.println("Connecting to a selected database to create Table...");
System.out.println("Connected database successfully...");
// Execute create query
System.out.println("Creating table in given database...");
stmt = conn.connect().createStatement();
String sql = "CREATE TABLE yourTableName_tab " +
"(pid INTEGER not NULL, " +
" id VARCHAR(10), " +
" income numeric(8,2), " +
" pep VARCHAR(3), " +
" PRIMARY KEY ( pid ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
conn.connect().close();//close db connection
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
}
}
Notice carefully that comments are put in code as well the display of relevant information to the console. For future methods continue this way (note-see snapshot at the last page).
-Include a method to insert records called insertRecords().
// INSERT INTO METHOD
publicvoid insertRecords(BankRecords[] robjs) {
try {
// Execute a query
System.out.println("Inserting records into the table...");
stmt = conn.connect().createStatement();
// Include all object data to the database table
for (inti = 0; i // finish string assignment to insert all object data // (pid, id, income, pep) intoyour database table String sql = ""; stmt.executeUpdate(sql); } conn.connect().close(); } catch (SQLException se) { se.printStackTrace(); } } Finish coding the above sql string where commented with an insert statement (example insert statement follows): sql = "INSERT INTO yourTableName_tab(field 1,field 2, field n) " + "VALUES (' "+value 1+" ', ' "+value 2+" ', ' "+value n+"' )"; Note for brevity purposes, future starter code will EXCLUDE try / catch blocks. Add in your own try / catch blocks were applicable. -Include a method to retrieve records for display called retrieveRecords(). public ResultSet retrieveRecords() { ResultSet rs = null; stmt = conn.connect().createStatement(); String sql = "SELECT * from yourTableName_tab"; rs = stmt.executeQuery(sql); conn.connect().close(); returnrs; } Methods breakdown insertRecords(BankRecords [] arrayName) will allow for thearray of BankRecord objects, to be passed to your method which will allow for the insertionof allthe id,income and pepdata from your BankRecords array (or whatever you named it)into your database table when called. retrieveRecords() will return a ResultSet object used for creating output. The result set contains record data including yourid, income and peptable fields. *Code tweak: Make sure to sort the pep field in descending order to allow for premium loan candidates to appear first in the record set for reporting purposes (i.e., those with data values of YES). The resultset query string to build can be something like: String sql = "select id,income, pep from yourTableName_tab order by pep desc"; As a quick note: make sure to alwayscloseoutof your connections and any statements when through with any processing! Make sure to include error trapping using SQLException handling for all your database operations and connection logic. Again, include messages to the console when your methods trigger. Ex. Table created, Inserting records into database, etc. A super great resource to assist you with all your JDBC-CRUD operations for your methods can be found at this site: http://www.tutorialspoint.com/jdbc/, as well as the Chapter 23 PowerPoint from Gaddis. Remember though to phrasecoding the best you can using your own object names, method naming and variable names, including coding syntax and even comments, if referencing any material from tutorialspoint so your lab work is unique.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started