对于Oracle 11G Express中的每个值,表重复打印多次

时间:2017-04-20 19:37:38

标签: java oracle11g

enter image description here

我正在开发Hibernate应用程序,我必须通过" hbm2ddl.auto"来创建表。在Oracle 11g Express数据库中的属性,现在我要求用户输入。用户给出以下输入,这些输入应存储在相应的创建列中。 ID-SID 名称 - SNAME 接触scontact 电子邮件semail 马克 - smarks 地址saddress

现在我有四个模块,包括主驱动程序类和两个hibernate xml文件

hibernate.cfg.xml如下所示

       <!DOCTYPE hibernate-configuration PUBLIC
           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
      <session-factory>
      <property name="connection.driver_class">
          oracle.jdbc.OracleDriver</property>
        <property 
        name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">system</property>
        <property name="connection.password">manager</property>
        <property name="connection.pool_size">10</property>

        <property 
         name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>         
         <mapping resource="resources/student.hbm.xml"/>

       </session-factory>
       </hibernate-configuration>

student.hbm.xml如下所示

      <!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
    <class name="beans.Student" table="studentinfo" schema="system">
        <id name="Student_ID" column="sid"/>
        <property name="Student_Name" column="sname"/>
        <property name="Student_Contact" column="scontact"/>
        <property name="Student_Address" column="saddress"/>
        <property name="Student_Marks" column="smarks"/>
        <property name="Student_Email" column="semail"/>

     </class>


</hibernate-mapping>

Client.java下面显示的驱动程序(主要)类

import beans.Student;
import beans.UserInput;

public class Client {

    public static void main(String[] args) {

        try {
            Configuration cfg = new Configuration();
            cfg.configure("resources/hibernate.cfg.xml");
            SessionFactory sf = cfg.buildSessionFactory();
            Student obj1 = UserInput.input(new Student());

            Session s = sf.openSession();
            s.save(obj1);
            s.beginTransaction().commit();
            s.evict(obj1);
        } catch (Exception e) {
        }
    }
}

User Input是此UserInput.java类的句柄

package beans;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UserInput {

    static BufferedReader br;
    static Validation v = new Validation();

    public static Student input(Student obj1) throws IOException {
        obj1 = new Student();
        br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter Roll No.:");
        int id = Integer.parseInt(br.readLine());
        obj1.setStudent_ID(id);

        System.out.println("Enter Name:");
        String name = br.readLine();
        obj1.setStudent_Name(name);

        System.out.println("Enter Marks:");
        int marks = Integer.parseInt(br.readLine());
        obj1.setStudent_Marks(marks);

        System.out.println("Enter Contact:");
        String contact = br.readLine();
        if (v.validateContact(contact)) {
            obj1.setStudent_Contact(contact);
        }

        System.out.println("Enter Email:");
        String email = br.readLine();
        if (v.validateEmail(email)) {
            obj1.setStudent_Email(email);
        }

        System.out.println("Enter Address:");
        String address = br.readLine();
        obj1.setStudent_Address(address);

        return obj1;
    }
}

我还没有在这里展示另外两个类,一个是POJO类(student.java),另一个是Validation.java类,用于验证手机号码和电子邮件地址。

注意:所有课程都正常且完美。

现在终于我在这里复制并粘贴了控制台日志。我很抱歉,因为有些内容无法格式化。

   Enter Roll No.:
   111
   Enter Name:
   anas
   Enter Marks:
   475
   Enter Contact:
   9981866224
   Enter Email:
   anas@gmail.com
   Enter Address:
    lilly talkies bhopal
   Hibernate: 
   insert 
       into
    system.studentinfo
    (sname, scontact, saddress, smarks, semail, sid) 
values
    (?, ?, ?, ?, ?, ?)

这是我见过的非常不寻常的事。

请大家,如果你遇到同样的问题,请帮助我解决这个问题,我有很多关于这方面的问题,如:

1.这是什么类型的错误?

2.它来自原产地?

3.课程负责吗?还是DB本身?

我在这里提供每一个可能的内容,请帮助我并建议我最好的解决方案。

1 个答案:

答案 0 :(得分:0)

您以递归方式调用方法输入

因此,只要您的联系电子邮件无效,您就会递归调用输入方法。

所以你得到了多个学生无效信息的实例。

如果您不想保留无效的联系方式或电子邮件详细信息,请按以下方式处理值(只需忽略它们)

System.out.println("Enter Contact:");
String contact=br.readLine();
if(v.validateContact(contact))//Method is present in Validation.java
{
    obj1.setStudent_Contact(contact);
}

System.out.println("Enter Email:");
String email=br.readLine();
if(v.validateEmail(email)) //Method is present in Validation.java
{
    obj1.setStudent_Email(email);
}
System.out.println("Enter Address:");
String address=br.readLine();
obj1.setStudent_Address(address);

return obj1;

或者在下面的循环中询问有效信息

System.out.println("Enter Contact:");
String contact=br.readLine();
while(!v.validateContact(contact)){
    contact=br.readLine();
}
obj1.setStudent_Contact(contact);