Question: Please, I really need help I can't get my code to work properly! I keep getting duplicates in my tables and I don't know how

Please, I really need help I can't get my code to work properly! I keep getting duplicates in my tables and I don't know how to make the code work when the values in the table are names or dates. here is my code so far:

package dataEntry;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

public class DataEntry {

public static void main(String[] args) {

Connection con = null;

Statement st = null;

ResultSet rs = null;

try {

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost:3306/hate?useSSL=false";

String username = "root";

String password = "davestrider";

Class.forName(driver);

con = DriverManager.getConnection(url,username,password);

System.out.println("Connected");

String sql = " INSERT INTO Earnings(tournament,players,prize_money,position) VALUES(?,?,?,?) ";

/**

* Insert Queries for all

* Players -> INSERT INTO Players(player_id,tag,real_name,nationality,birthday,game_race) VALUES(?,?,?,?,?,?) "

* Teams -> INSERT INTO Teams(team_id,name,founded,disbanded) VALUES(?,?,?,?) "

* Members -> INSERT INTO Members(player,team,start_date,end_date) VALUES(?,?,?,?) "

*/

try {

BufferedReader bReader = new BufferedReader(new FileReader("./src/earnings.csv"));

/**

* These lines changes un comment and execute the each insertion operation sepearetely

*/

//BufferedReader bReader = new BufferedReader(new FileReader("matches.csv"));

//BufferedReader bReader = new BufferedReader(new FileReader("members.csv"));

//BufferedReader bReader = new BufferedReader(new FileReader("teams.csv"));

//BufferedReader bReader = new BufferedReader(new FileReader("players.csv"));

//BufferedReader bReader = new BufferedReader(new FileReader("tournaments.csv"));

String line = "";

while ((line = bReader.readLine()) != null) {

try {

if (line != null) {

String[] array = line.split(",+");

for (String result : array) {

System.out.println(result);

// Create preparedStatement here and set them

// and excute them

/**

* sql will be changes based on your table to insert

*/

PreparedStatement ps = con.prepareStatement(sql);

/**

* Prepared statement set value will be changes based on the datatype , take care this

*

*/

ps.setInt(1, Integer.parseInt(array[0]));

ps.setInt(2, Integer.parseInt(array[1]));

ps.setInt(3, Integer.parseInt(array[2]));

ps.setInt(4, Integer.parseInt(array[3]));

ps.executeUpdate();

ps.close();

// Assuming that your line from file after split

// will folllow that sequence

}

}

} finally {

if (bReader == null) {

bReader.close();

}

}

}

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

Here is the actual assignment:

Submit the dataEntry.java file along with the tables created inside MySQL (as a .csv file) in a single .zip file.

Install JDBC and Eclipse.

(INSERT data into the database) Use the attached schema to implement the corresponding tables.

Foreign keys are omitted, but you are expected to identify and implement them correctly.

Populate the database using the uploaded data files.

Write a java program to solve this - name of the file should be: dataEntry.java. The methods in the java file will accept data file names as input (file names can be hardcoded) and will be able to read and parse data before inserting them into the corresponding tables. **Note: when executing the same statement repeatedly, you should use the JDBC PreparedStatement construct (Links to an external site.)Links to an external site. for greater efficiency as compared to the more generic Statement class.

Here are the data files for the assignment. You can download it from my google drive (just copy and paste the link):

https://drive.google.com/drive/folders/0BycHCqxSw_g9dVVra3VpR1VTcHc?usp=sharing

Plavers plaver id tagreal name nationality birthday game race Teams team id name founded disbanded Members player team start date end date Tournaments tournament id name region major Matches match id date tournament playerA playerB scoreA scoreB offline Earnings tournamentplayer prize money position

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!