【问题标题】:how to Fix java.lang.ArrayIndexOutOfBoundsException in Java Hibernate application如何在 Java Hibernate 应用程序中修复 java.lang.ArrayIndexOutOfBoundsException
【发布时间】:2013-10-09 17:17:01
【问题描述】:
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 在 com.candidjava.AddStudent.main(AddStudent.java:15) 这个异常来了我不知道如何修复它请帮助...

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

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

    【讨论】:

    • 请在您的回答中包含内容
    【解决方案2】:

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

    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();
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我猜你没有传递所有的论点,使你的条件符合逻辑而不是或

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

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

      student1.cfg.xml
      

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

      不是休眠configuration 文件。

      在处理休眠时,从 hibernate.cfg.xml 文件而不是从 args 读取 properties

      Hibernate Configuration Official Docs

      【讨论】:

        【解决方案4】:

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

            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(""))) {
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-16
          相关资源
          最近更新 更多