向数据库插入对象中的数据

时间:2022-12-04 16:24:41

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.

我面临着以下的设计/实现难题。我有一个类客户,下面是getters和setters。我想将客户的值插入到数据库的“客户”表中。但是客户有一个类型为“地址”的地址。如何将这个字段插入到数据库中?(我使用sqlite3)。我想写一个单独的表格“地址(客户id、门牌、城市、州、国家、密码)”。但是我正在考虑生成主键(customerId),它对于“客户”和“地址”表都应该是相同的。Sqlite3 faq指出,我可以用“整数主键”来使用该字段生成一个自动编号。但是如果我在customer表中那样做,我就必须检索地址表中使用的相同的Id。我觉得有点不对:-?应该有一个优雅的方法来解决这个问题。任何想法都将受到欢迎。提前谢谢。

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;
}

1 个解决方案

#1


4  

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

了解关系数据库设计、JDBC、SQLite JDBC和hibernate

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

有太多的事情需要回答,无法详细说明(无论如何,对我来说;)

#1


4  

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

了解关系数据库设计、JDBC、SQLite JDBC和hibernate

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

有太多的事情需要回答,无法详细说明(无论如何,对我来说;)