从对象列表中获取值

时间:2017-01-16 04:38:48

标签: java mysql

我有一个联系人课程,其中包含人名,电子邮件和电话号码。然后我有一个地址簿类,它有一个id,名字和联系人列表。所以我想将地址簿添加到mysql数据库。我只想通过从adressbook类的联系人列表中检索人名,电子邮件和电话号码来确保我这样做。到目前为止,这是我的代码:

AddressBook类:

public class AddressBook{
    private int id;
    private String name; 
    private List<Contact> contacts;
    // getters and setters
}

联系班级:

public class Contact
{
    private String firstName, lastName, email, phoneNum;
    // getters and setters
}

数据库类:

public boolean insert(AddressBook addressBook)
    {
        boolean success = false;
        int executedValue = 0;

        try
        {
            Connection conn = DBConnection.getConnection();
            String query = "INSERT INTO addressbook (id, firstname, lastname, email, phonenum)"
                    + "VALUEs (?,?,?,?,?)";
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setInt(1, addressBook.getId());

            List<Contact> contacts = addressBook.getContacts();

            for(Contact contact : contacts)
            {
                 stmt.setString(2, contact.getFirstName());
                 stmt.setString(3, contact.getLastName());
                 stmt.setString(4, contact.getEmail());
                 stmt.setString(5, contact.getPhoneNum());
            }

            executedValue = stmt.executeUpdate();
            success = executedValue > 0;   
        } 
        catch (Exception e)
        {
            System.out.println(e);
        }
        return success;
    }

1 个答案:

答案 0 :(得分:0)

为什么你有两个班,但只有一个班?如果AddressBook表没有?{/ p>,addressbook类为什么会有一个名称?

简而言之,您的数据库是错误的。为了拥有一个包含许多联系人的通讯簿,您需要一个addressbook表,其中包含与contact表的一对多关联。

没有进入有关自动生成的密钥/标识符的整个讨论,我假设您的addressbook列有一个有效的id列作为主键,而您contact不具有自己的id,但是是addressbook的子表,例如主键为id, firstname, lastname,其中id是地址簿ID。这是一个糟糕的设计,但也许你想要那样。

如果是这样,您的代码可能是:

// Insert AddressBook
String sql1 = "INSERT INTO addressbook (id, name) VALUES (?,?)"
try (PreparedStatement stmt = conn.prepareStatement(sql1)) {
    stmt.setInt(1, addressBook.getId());
    stmt.setString(2, addressBook.getName());
    stmt.executeUpdate();
}

// Insert Contacts
String sql2 = "INSERT INTO contact" +
             " (id, firstname, lastname, email, phonenum)" +
             " VALUES (?,?,?,?,?)";
try (PreparedStatement stmt = conn.prepareStatement(sql2)) {
    stmt.setInt(1, addressBook.getId());
    for (Contact contact : addressBook.getContacts()) {
        stmt.setString(2, contact.getFirstName());
        stmt.setString(3, contact.getLastName());
        stmt.setString(4, contact.getEmail());
        stmt.setString(5, contact.getPhoneNum());
        stmt.executeUpdate();
    }
}

注意executeUpdate() {em> for循环中的内容。

为了提高性能,最好使用批量插入:

// Insert Contacts
String sql2 = "INSERT INTO contact" +
             " (id, firstname, lastname, email, phonenum)" +
             " VALUES (?,?,?,?,?)";
try (PreparedStatement stmt = conn.prepareStatement(sql2)) {
    stmt.setInt(1, addressBook.getId());
    for (Contact contact : addressBook.getContacts()) {
        stmt.setString(2, contact.getFirstName());
        stmt.setString(3, contact.getLastName());
        stmt.setString(4, contact.getEmail());
        stmt.setString(5, contact.getPhoneNum());
        stmt.addBatch();
    }
    stmt.executeBatch();
}

插入语句无法以静默方式失败,即返回0更新计数,因此检查更新计数毫无意义。它们当然可能因重复键,值溢出,错误的SQL等而失败,但这一切都会导致SQLException