0
package com.candidjava;

import java.sql.*;
import java.io.*;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class AddStudent {
    private static SessionFactory sessionFactory1;

    public static void main(String args[]) throws Exception {
        if (args[0] != null || args[1] != null || args[2] != null) {// begin if
                                                                    // A
            String name = args[0];
            String name1 = args[0];
            String degree = args[1];
            String phone = args[2];
            System.out.println("Name: " + name);
            System.out.println("Degree: " + degree);
            System.out.println("Phone: " + phone);

            if ((name.equals("") || degree.equals("") || phone.equals(""))) {
                System.out.println("All informations are Required");
            } else {

                try {// begin try

                    sessionFactory1 = new Configuration().configure(
                            "com\\xml\\student1.cfg.xml").buildSessionFactory();
                } catch (Exception e) {
                    System.out.println("mathan");
                    System.out.println(e.getMessage());
                    System.err
                            .println("Initial SessionFactory creation failed."
                                    + e);

                }

                Session s1 = sessionFactory1.openSession();
                Transaction tx1 = s1.beginTransaction();
                Student1 stu1 = new Student1();
                stu1.setName(name1);
                s1.save(stu1);
                tx1.commit();
                System.out.println("Added to mysql Database");
                if (s1 != null)
                    s1.close();
            }
        }
    }
}

这是我的代码我不知道在哪里做错了但是当我运行这个应用程序时出现异常我正在创建 Hibernate 示例应用程序

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 at com.candidjava.AddStudent.main(AddStudent.java:15) 这个异常来了我不知道如何修复它请帮助...

4

4 回答 4

2

问题之一可能是您需要在以下情况下使用 &&:

    if (args[0] != null || args[1] != null || args[2] != null) {// begin if

将其更改为:

    if (args[0] != null && args[1] != null && args[2] != null) {

以下检查也有相同的逻辑问题:

        if ((name.equals("") || degree.equals("") || phone.equals(""))) {

应该 :

        if ((name.equals("") && degree.equals("") && phone.equals(""))) {
于 2013-10-03T06:03:31.340 回答
1

我猜你没有通过所有的论据使你的条件符合逻辑而不是或

 if (args[0] != null && args[1] != null && args[2] != null) { 

而且,您在配置方面走错了路configure

student1.cfg.xml

我相信上面的文件是Studentpojo的映射xml文件。

不是休眠configuration文件。

在处理休眠时,propertieshibernate.cfg.xml文件中读取而不是从 args 中读取。

Hibernate 配置官方文档

于 2013-10-03T06:04:14.590 回答
0

您可以轻松地一次进行验证

package com.candidjava;

导入java.sql.*;

导入java.io.*;

导入 org.hibernate.SessionFactory;

导入 org.hibernate.Transaction;

导入 org.hibernate.HibernateException;

导入 org.hibernate.Session;

导入 org.hibernate.cfg.Configuration;

公共类 AddStudent {

private static SessionFactory sessionFactory1;

public static void main(String args[]) throws Exception {
     if ((name.equals("") && degree.equals("") && phone.equals(""))) {
            System.out.println("All informations are Required");
        } else {

        String name = args[0];
        String name1 = args[0];
        String degree = args[1];
        String phone = args[2];
        System.out.println("Name: " + name);
        System.out.println("Degree: " + degree);
        System.out.println("Phone: " + phone);   

            try {// begin try

                sessionFactory1 = new Configuration().configure(
                        "com\\xml\\student1.cfg.xml").buildSessionFactory();
            } catch (Exception e) {
                System.out.println("mathan");
                System.out.println(e.getMessage());
                System.err
                        .println("Initial SessionFactory creation failed."
                                + e);

            }

            Session s1 = sessionFactory1.openSession();
            Transaction tx1 = s1.beginTransaction();
            Student1 stu1 = new Student1();
            stu1.setName(name1);
            s1.save(stu1);
            tx1.commit();
            System.out.println("Added to mysql Database");
            if (s1 != null)
                s1.close();
        }
    }
}
于 2013-10-03T10:15:53.720 回答
0

本教程已在 candidjava 网站上重写,试试这个新的hibernate 注释示例

于 2017-07-23T19:36:03.567 回答