Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

urgent help needed for Java Code: 20-1. Write a program that will create a yankees table for a baseball part a Access database file. It

urgent help needed for Java Code:

20-1. Write a program that will create a yankees table for a baseball part a Access database file. It will have fields for the name of the player, the position of the player, and the player's number. Then insert the following records into the table (a list of yankees from a few years ago). Use SQL commands for the create and insert. Then use a ResultSet to list all records and all fields. If you use Access, set up UCanAccess or the ODBC JDBC bridge and create an Access baseball database file, then the lab exercise will create the yankees table. Bernie Williams CF 51 Derek Jeter SS 2 Mariano Rivera P 42 Jason Giambi 1B 25 Mike Mussina P 35 Alfonso Soriano 2B 12 You then realize Giambi would be better as a DH instead of 1B, so use an SQL command to update the table for the record with Jason Giambi as the Name in the java program.

20-2. Create 2 queries for the yankees table from 20-1 from the baseball Access database or Derby database. It will be set up similar to 20-1, then add the query and ResultSet. List the results from the ResultSet on the screen. You can combine both into one java program. PLEASE USE THE Access databse (To USE ACCESS DATABASE YOU NEED If the ODBC Data Source driver is not available or not supported on your system, but you still want to use Java to connect to an Access database, UCanAccess can also be used. UCanAccess can be downloaded from:: https://sourceforge.net/projects/ucanaccess/) . Give the proper query statement and display the results.

a. The first will be to list the players name, number, and position (in that order) for all players whose number is less than 20 (of course most of the low numbers have been retired because of all the great players in the history of the Yankees). Sort the list by position.

package dbms20; import java.sql.*; // Connection, Statement, DriverManager

import javax.swing.*; public class ex201 { public static void main(String args[]) { String url = "jdbc:ucanaccess://C:/Users/sara/yankeesdb.accdb"; // connection for UCanAccess (adjust path)//change to the acess // String url = "jdbc:odbc:bookstore";//chaqnge to the acess file name is yankess // odbc protocol for Access, bookstore data source Connection con; // session with database String createstring; // for SQL command createstring = "create table players " + "(playerName varchar(30), " + "Postion varchar(4), " + "playernumber int)"; /* create customers table with integer field called ID, with Character Text field called FirstName or size 20, etc. */ Statement stmt; // for executing SQL statement // continued // JDBC-ODBC Bridge driver check is removed try { con = DriverManager.getConnection(url, "", ""); /* trys to establish a connection to the given database URL and selects driver from registered JDBC drivers. On local system, so no login name or password needed */ stmt = con.createStatement(); // create a new Statement object for this Connection stmt.executeUpdate(createstring); // executes the SQL statement JOptionPane.showMessageDialog(null, "players table created", "SQL Statement Confirmation", JOptionPane.INFORMATION_MESSAGE ); /* open message dialog box confirming SQL statement execution */

stmt.executeUpdate("insert into players " + "values( 'Bernie Williams', 'CF', 51)");

stmt.executeUpdate("insert into players " + "values( 'Derek Jeter', 'SS', 2)"); stmt.executeUpdate("insert into players " + "values( 'Mariano Rivera', 'P', 42)"); stmt.executeUpdate("insert into players " + "values( 'Jason Giambi', '1B', 25)"); stmt.executeUpdate("insert into players " + "values( 'Mike Mussina', 'P', 35)");

stmt.executeUpdate("insert into players " + "values( 'Alfonso Soriano', '2B', 12)");

stmt.executeUpdate("update players " + "set Postion = 'DH' " + "where playerName = 'Jason Giambi' ");

String query = "select * from players"; ResultSet rs = stmt.executeQuery(query);

System.out.println("playersName\tpostion\tplayernumber"); //prints title and column headings to output screen while (rs.next()) //advances the current row until no more { String Name = rs.getString("playerName"); /* get the string first name field value from the current record of the ResultSet */ String postion = rs.getString("postion"); int number = rs.getInt("playernumber"); System.out.println(Name + " " + postion + " " + number); //print the data to the output screen }

// returns a ResultSet that contains the data produced stmt.close(); con.close(); // close and release resources System.exit(0); // end program because of dialog box } catch(SQLException ex) // if can't execute SQL statement { System.out.println("SQLException"); System.out.println(ex.getMessage()); ex.printStackTrace(); } } }

b. The second will be to ask the user what position they want to see listed, then list the name and number for all players at that position. Attach your .java program using Access database.

package dbms20;

import java.sql.Connection; import java.sql.*;

import java.sql.DriverManager; import java.sql.ResultSet; //import java.sql.ResultSetMetaData; import java.sql.Statement; import java.sql.PreparedStatement; import java.util.Scanner;

public class ex202b {

public static void main(String[] args) throws Exception { String url = "jdbc:ucanaccess://C:/Users/sara/yankeesdb.accdb"; Connection con; con = DriverManager.getConnection(url, "", ""); Statement st = con.createStatement(); //String sc1; //createstring = "create table players " + // "(playerName varchar(30), " + // "Postion varchar(4), " + // "playernumber int)"; //create customers table with integer field called ID, with Character Text field called FirstName or size 20, etc. */ Statement stmt; // for executing SQL statement // continued // JDBC-ODBC Bridge driver check is removed try { st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT playerName,playernumber, Postion FROM players WHERE playernumber < 20 ORDER BY Postion asc"); while (rs.next()) { //Print one row System.out.println(rs.getString(1) + " "+rs.getInt(2)+" "+rs.getString(3)); } st = con.createStatement(); ResultSet rs1 = st.executeQuery("SELECT playerName,playernumber, Postion FROM players WHERE playernumber < 20 ORDER BY Postion asc"); System.out.print("Please Enter The Position For Player Listing"); @SuppressWarnings("resource") Scanner sc= new Scanner(System.in); String position=sc.nextLine(); PreparedStatement ps = con.prepareStatement("SELECT * FROM yankees where position = ?"); ps.setString(1,position); while (rs1.next()) { //Print one row System.out.println(rs1.getString(1) + " "+rs1.getInt(2)); } } catch (SQLException ex) { System.out.println("SQLException"); System.out.println(ex.getMessage()); ex.printStackTrace(); } st.close(); con.close(); } }

can you please help me with this code it doesn't allow "ask the user what position they want to see listed, then list the name and number for all players at that position." (i am working this file on Eclipse)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago