如何使用索引外键java存储数据

时间:2017-03-11 16:54:53

标签: java javafx

我正在java项目工作,我想在db中存储数据 在phpmyamin我在我的表发布id_createur中设置为引用membre表的外键

my foreign key configured like that

    public class PublicationService  implements IPublicationService{

private Connection connection;

public PublicationService() {
      connection = DataSource.getInstance().getConnection();
}


@Override
public void add(Publication pub) {
    try {
        String req = "insert into publication(description,id_createur) values (?,?)";
        PreparedStatement ps = connection.prepareStatement(req);
        ps.setString(1,pub.getDescription());
        ps.setInt(2,pub.getCreateur().getId());
        ps.executeUpdate();


    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

@Override
public void update(Publication t) {

}

@Override
public void delete(Integer id) {
}

@Override
public List<Publication> getAll() {
            List<Publication> pubs = new ArrayList<>();
            return pubs;

}

  }

这是我的界面IpublicationService,它扩展了IService

 public interface IPublicationService extends IService<Publication, Integer>
{

 }

这是我的Iservice界面

   public interface IService <T,R> {

void add(T t);
void update(T t);
void delete(R id);
List<T> getAll();



  }

最后这是我的模特刊物

  public class Publication {
private int id ;
private String description;
private int nb_jaime;
private Date date_publication;
private Membre createur;

public Publication(String description, Date date_publication) {
    this.description = description;
    this.date_publication = date_publication;
}

public Publication(String description, Date date_publication, Membre createur) {
    this.description = description;
    this.date_publication = date_publication;
    this.createur = createur;
}



public Publication(String description, Membre createur) {
    this.description = description;
    this.createur = createur;
}



public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getNb_jaime() {
    return nb_jaime;
}

public void setNb_jaime(int nb_jaime) {
    this.nb_jaime = nb_jaime;
}

public Date getDate_publication() {
    return date_publication;
}

public void setDate_publication(Date date_publication) {
    this.date_publication = date_publication;
}

public Membre getCreateur() {
    return createur;
}

public void setCreateur(Membre createur) {
    this.createur = createur;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 97 * hash + this.id;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Publication other = (Publication) obj;
    if (this.id != other.id) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Publication{" + "id=" + id + ", description=" + description + ", nb_jaime=" + nb_jaime + ", date_publication=" + date_publication + ", createur=" + createur + '}';
}




}

这是我的测试主要方法

   public class TestPublicationService {
public static void main(String[] args) {
    Publication p = new Publication("testjava",new Membre(1));
    IService ps = new PublicationService();
    ps.add(p);

}

 }

但我得错误无法添加外键约束

1 个答案:

答案 0 :(得分:0)

我干净并构建我的项目的属性,一切正常