2013-10-03 69 views
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(); 
      } 
     } 
    } 
} 

修復java.lang.ArrayIndexOutOfBoundsException這是我的代碼我不知道我做的錯誤,但,當我運行該應用程序,然後異常來我創建Hibernate的示例應用程序如何用Java Hibernate應用程序

在異常線程「主要」 java.lang.ArrayIndexOutOfBoundsException:0 在com.candidjava.AddStudent.main(AddStudent.java:15) 該異常來了,我不知道如何解決它,請幫助...

回答

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

我想你不是爸ssing所有的論據是有邏輯的,而不是或

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

而且,你是在錯誤的軌道上配置configure

student1.cfg.xml 

我認爲上述文件是Student POJO映射XML文件的條件。

不是休眠configuration文件。

在處理休眠時,請從hibernate.cfg.xml文件中讀取properties而非args。

Hibernate Configuration Official Docs

0

您可以輕鬆完成驗證一次

package com.candidjava; 

進口的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。組態;

公共類傳遞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(); 
     } 
    } 
} 
相關問題