Home > database >  JDBC Java, How to insert from an extended class
JDBC Java, How to insert from an extended class

Time:11-21

My information is read in via CSV for "banking information", A person CSV is

5
1,Tony,Stark,C,tonys,naslvj34-t934,[email protected];[email protected];[email protected] 2,Patrick,Hart,C,phart2,sdlwgl5034i52,[email protected];[email protected] 3,Tom,Baker,E,bake95,kweojtnefq567, 4,Kevin,Black,C,keb765,prjhohier99,[email protected] 5,Alex,Codd,E,alcodd,andlqjr78,[email protected];[email protected]

my Account CSV is,

5 1,313001,S,35881.12 2,313002,G,772400.34 3,313003,C,250002.15 4,313004,P,96310.66 5,313005,P,15624.15 1,313006,C,39950.99

In the Account CSV the 2nd token is My "Account type" So S = "Savings" G = "Gold Savings" C = "Checkings" and so on, When reading these in my CSV parser class an object was made of the types, so A Checking class was made and when the csv was tokenized when token[2] was equal to "C".

My Persons Class is set up as so, excluding my getters and setters

    private int personId;
    private String firstName;
    private String lastName;
    private String type;
    private String usrname;
    private String password;    
    private ArrayList<String> emails;
    
    
    public Person(int personId, String firstName, String lastName, String type, String usrname, String password,
            ArrayList<String> emails) {
        super();

And my Account class is set up as so.

    private Person accountHolder;
    private int accountNumber;
    private double currentBalance;
     
    
    public Account() {}
    
    public Account(Person accountHolder, int accountNumber, double currentBalance) {
        this.accountHolder = accountHolder;
        this.accountNumber = accountNumber;
        this.currentBalance = currentBalance;
    }   

this is one of my example account types(theres mutiple)

public class CheckingAccount extends Account {

    public CheckingAccount(Person p, int i, double d) {
        super(p,i, d);
    }

This is my Database adder

public static void DbAddAccount(Person personId, Account accountHolder){

         Connection conn = null;
            try {
               conn = DriverManager.getConnection(DatabaseInfo.URL,  DatabaseInfo.USERNAME, DatabaseInfo.PASSWORD);
            } catch (SQLException e) {
                System.out.println("Connection Failed");
                throw new RuntimeException(e);
            }
            
            String accountQuery = "insert into Account (accountId,accountNumber,currentBalance,type,personId) values (?,?,?,?,?);";
                
            PreparedStatement accountPs = null;
        
            ResultSet accountKeys = null;
            
            int accountId = 0;

                try {
                    //These are my prepare Statements for my queries
                    accountPs = conn.prepareStatement(accountQuery);
                    accountPs.setInt(1, accountId  1 );
                    accountPs.setInt(2, accountHolder.getAccountNumber());
                    accountPs.setDouble(3, accountHolder.getCurrentBalance());
                    accountPs.setString(4, ??????????????????????????????????);

    
                    accountPs.setInt(5, personId.getPersonId());
                                        
                    accountPs.executeUpdate();
                     accountPs.close(); 
                     conn.close();
                                
                } 

            catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    }   

JDBC Is not adding anything when running

CodePudding user response:

The ? is for parameterization in PreparedStatement. This parameterized query is to allow type-specific value when replacing the ? with their respective value. The series of ? (question marks) are wrong to code like that.

As I see you wanted to update the Account Type in the table, where you can do it by passing the correct account object/parameter to the DbAddAccount method

CodePudding user response:

I think your code has following issues:

  1. Account Object: Object is missing field to store "Account Type"
  2. Setting incorrect account type in DbAddAccount method At line accountPs.setString(4, ??????????????????????????????????); you are number of question marks (without enclosing in quote to make string) as account type which appears it is not correct.
  • series of ??? wont get converted to string without enclosed in quotes.
  • Even if you enclose in quotes, I think it wont be intended account number;

Solution for Issue 1; declare account_type in Account Object.

for issue 2; instead of using accountPs.setString(4, ??????????????????????????????????); use: accountPs.setString(4, account.getAccountType());

  • Related