开发者

Insert data in an object to a database

开发者 https://www.devze.com 2022-12-22 11:12 出处:网络
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

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. T开发者_StackOverflowhanks 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;
}


have a read about Relational Database design, JDBC, SQLite JDBC and hibernate

there's way too many things to answer to go in to detail on this (for me anyway ;))

0

精彩评论

暂无评论...
验证码 换一张
取 消