Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database DB4O插入不存在的酿酒厂_Database_Insert_Db4o - Fatal编程技术网

Database DB4O插入不存在的酿酒厂

Database DB4O插入不存在的酿酒厂,database,insert,db4o,Database,Insert,Db4o,我正在用DB4O做一个测试。我正在尝试插入数据。 我的问题是: 当我插入winery和D.O.的名称时,我必须仅在数据库中不存在时插入 我怎么做呢 这是我的插入代码: Scanner teclado=new Scanner(System.in); try{ ObjectContainer db= Db4oEmbedded.openFile("DB4O.yap"); System.out.print("Name of Wine: ");

我正在用DB4O做一个测试。我正在尝试插入数据。 我的问题是:

当我插入winery和D.O.的名称时,我必须仅在数据库中不存在时插入

我怎么做呢

这是我的插入代码:

Scanner teclado=new Scanner(System.in);

        try{

        ObjectContainer db= Db4oEmbedded.openFile("DB4O.yap");
        System.out.print("Name of Wine: ");
        String nomVino = teclado.next();
        System.out.print("Name of Cellar: ");
        String nomBodega = teclado.next();
        System.out.print("D.O: ");
        String DO = teclado.next();

        System.out.print("Type of Wine: ");
        String tipoVino = teclado.next();
        System.out.print("Grad: ");
        float gradosAlch = teclado.nextFloat();
        System.out.print("Year of wine: ");
        int fecha = teclado.nextInt();

        teclado.close();

            Vinos v = new Vinos(nomVino,new Bodega(nomBodega,DO),tipoVino,gradosAlch,fecha);
            db.store(v); //guardar objetos
            db.commit(); // valida los datos
            //odb.rollback(); // deshace los datos
            System.out.print("Vino Introducido\n");
            db.close(); //cerrar la bd  
        }

     catch(Exception e){System.out.println (e);}

    }

提前谢谢你

你到底有什么问题(你的问题不清楚)

您是否希望避免重复Bodega()对象,即当您存储一个新的Vinos对象时,如果Bodega()对象与bodegaNameä一起存在,您不希望存储另一个Bodega()对象

如果您正试图实现这一点,则需要在数据库中搜索Bodega对象,然后再将其与Vinos对象一起使用;比如:

Scanner teclado=new Scanner(System.in);

try{
    ObjectContainer db= Db4oEmbedded.openFile("DB4O.yap");
    System.out.print("Name of Wine: ");
    String nomVino = teclado.next();
    System.out.print("Name of Cellar: ");
    String nomBodega = teclado.next();
    System.out.print("D.O: ");
    String DO = teclado.next();

    System.out.print("Type of Wine: ");
    String tipoVino = teclado.next();
    System.out.print("Grad: ");
    float gradosAlch = teclado.nextFloat();
    System.out.print("Year of wine: ");
    int fecha = teclado.nextInt();

    teclado.close();

    ObjectSet<Bodega> foundBodegas = db.query(new Predicate<Bodega>() {
                                        @Override public  boolean match(Bodega candidate) {
                                             return candidate.getName().equals(nomeBodega);
                                        }});

    Bodega bodega = null;

    if (foundBodegas.size() == 1) {
        bodega = foundBodegas.next();
    }
    else {
        bodega = new Bodega(nomBodega, DO);
    }

    Vinos v = new Vinos(nomVino,  bodega  ,tipoVino,gradosAlch,fecha);

    db.store(v); //guardar objetos
    db.commit(); // valida los datos
    //odb.rollback(); // deshace los datos
    System.out.print("Vino Introducido\n");
    db.close(); //cerrar la bd  
}
catch(Exception e)
{
    System.out.println (e);
}
Scanner-teclado=新扫描仪(System.in);
试一试{
ObjectContainer db=Db4oEmbedded.openFile(“DB4O.yap”);
系统输出打印(“葡萄酒名称:”);
字符串nomVino=teclado.next();
系统输出打印(“酒窖名称:”);
字符串nomBodega=teclado.next();
系统输出打印(“D.O:”);
字符串DO=teclado.next();
系统输出打印(“葡萄酒类型:”);
字符串tipoVino=teclado.next();
系统输出打印(“梯度:”);
float gradosAlch=teclado.nextFloat();
系统输出打印(“葡萄酒年份:”);
int fecha=teclado.nextInt();
teclado.close();
ObjectSet foundBodegas=db.query(新谓词(){
@覆盖公共布尔匹配(Bodega候选){
返回candidate.getName().equals(nomeBodega);
}});
波德加波德加=零;
如果(foundBodegas.size()==1){
bodega=foundBodegas.next();
}
否则{
博迪加=新博迪加(nomBodega,DO);
}
Vinos v=新葡萄酒(nomVino、bodega、tipoVino、gradosAlch、fecha);
db.store(v);//瓜达尔对象
db.commit();//valida los datos
//odb.rollback();//deshace los datos
System.out.print(“Vino-Introcido\n”);
db.close();//cerrar la bd
}
捕获(例外e)
{
System.out.println(e);
}

您可以找到更多信息,谢谢,这正是我需要的!