Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please explain to me the solution of this program for final review. Thank you Su

ID: 3548287 • Letter: P

Question

Please explain to me the solution of this program for final review. Thank you


Suppose you have a hockey card collection and you create a hockey database with a cards table. You have many records already with fields for First, Last, POS, Team, 09-10Pts, and CareerPts. For each of the following, write an SQL command to save to a String called command and then write a java command to use that String to execute your command with a Statement object called stmt. For part b, save into a ResultSet object called rs. You do not have to print anything and you can assume command, stmt, and rs have already been declared and properly created. Here is what a sample record looks like:



First Last POS Team 09-10Pts CareerPts


Marian Gaborik RW Rangers 86 523




a. Create a card for Brandon Dubinsky of the Rangers. He is a C and had 44 points in 09-10 for 125 career points.

Explanation / Answer

Hi, Please find the program below that is constructed based on your problem. Below steps explain your above given problem. 1) Database is Mysql. 2) In the properties, give your usename and password. I hope that tables are already created. Here the table name is Cards and DB name is Hockey. 3) Connection established with the URL - "jdbc:mysql://localhost:3306/Hockey" 4) InsertAndViewRecords inserts the record for Brandon Dubinsky. 5) Then the records are retrieved using the executequery command and is printed in the format as given in the problem. First Last POS Team 09-10Pts CareerPts Marian Gaborik RW Rangers 86 523 PF below the progam. Hope it helps you. import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class HockeyCardCollection { public static Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", "yourDBusername"); connectionProps.put("password", "yourDBpassword"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hockey", connectionProps); System.out.println("Connected to database"); return conn; } public static void InsertAndViewRecords() throws SQLException { Connection con = getConnection(); Statement stmt = null; // Create a card for Brandon Dubinsky of the Rangers. He is a C and had // 44 points in 09-10 for 125 career points. String insertTableSQL = "INSERT INTO Cards" + "(First, Last, POS, Team, 09_10Pts, CareerPts) " + "VALUES" + "('Brandon','Dubinsky','C', 'Rangers',44,125)"; try { stmt = con.createStatement(); stmt.executeUpdate(insertTableSQL); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { stmt.close(); } } try { // Display Marian Gaborik RW Rangers 86 523 with other records stmt = con.createStatement(); String query = "select First, Last, POS, Team, 09_10Pts,CareerPts from Cards"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String First = rs.getString("First"); String Last = rs.getString("Last"); String POS = rs.getString("POS"); String Team = rs.getString("Team"); int P0910Pts = rs.getInt("09_10Pts"); int CareerPts = rs.getInt("CareerPts"); System.out.println("First Last POS Team 09-10Pts CareerPts"); System.out.println(First + " " + Last + " " + POS + " " + Team + " " + P0910Pts + " " + CareerPts); } } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { stmt.close(); } } } public static void main(String[] args) throws Exception { InsertAndViewRecords(); } } Thanks, Sri