Insert data in an object to a database.
- by paul
I am facing the following design/implementation dilemma.
I have a class Customer which is below with getters and setters. I would like to insert the value of the Customer into a "Customer" table of a database. But Customer has an address which is of type "Address". How do I go about inserting this field into the database?(I am using sqlite3). I thought of writing a separate table "Address(customerId,doorNo,city,state,country,pinCode)". But I am having second thoughts about generating the primary key(customerId) which should be same for both the "customer" and "Address" table. Sqlite3 faq states that I can do "Integer Primary Key" to use the field to generate an auto number. But if I do that in customer table, I would have to retrieve the same Id to be used in Address table. This kinda looks wrong to me :-?. There should be an elegant method to solve this. Any ideas would be much appreciated. Thanks in advance.
import java.io.*;
import java.sql.*;
class Customer {
private String id;
private String name;
private Address address;
private Connection connection;
private ResultSet resultSet;
private PreparedStatement preparedStatement;
public void insertToDatabase(){
}
}
class Address{
private String doorNumber;
private String streetName;
private String cityName;
private String districtName;
private String stateName;
private String countryName;
private long pinCode;
}