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) 这个异常出现了,我不知道如何修复它,请帮忙......
最佳答案
问题之一可能是您需要在以下情况下使用 && :
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(""))) {
关于java - 如何修复 Java Hibernate 应用程序中的 java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151885/