Hibernate:log4j:WARN找不到appender

时间:2013-12-04 12:59:40

标签: java mysql hibernate

完整的错误是..................

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.HibernateException: JDBC Driver         
class not found: com.mysql.jdbc.Driver
Exception in thread "main" java.lang.ExceptionInInitializerError
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:26)
at hibernate.TestMain.updateUser(TestMain.java:123)
at hibernate.TestMain.main(TestMain.java:35)
Caused by: org.hibernate.HibernateException: JDBC Driver class not found:   
com.mysql.jdbc.Driver
at  
org.hibernate.connection.DriverManagerConnectionProvider.
configure(DriverManagerConnectionProvider.java:66)
at org.hibernate.connection.ConnectionProviderFactory.
newConnectionProvider(ConnectionProviderFactory.java:124)
at org.hibernate.connection.ConnectionProviderFactory.
newConnectionProvider(ConnectionProviderFactory.java:56)
at  

org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:397)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:21)
... 2 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
at  

org.hibernate.connection.DriverManagerConnectionProvider.
configure(DriverManagerConnectionProvider.java:61)
... 9 more

我是新来的冬眠请任何人帮助我......... 我的Users.hbm.xml文件是.....

<?xml version="1.0" encoding="UTF-8"?>

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

<hibernate-mapping>

<class entity-name="Users" name="hibernate.Users" table="users">

<meta attribute="description">This class is used to save the info about users</meta>

<id column="UserId" name="UserId" type="long"/>

<property column="FName" name="FName" type="string"/>

<property column="LName"  name="LName" type="string"/>

<property column="UserTypeId" name="UserTypeId" type="long"/>

<property column="UserName" name="UserName" type="string"/>

<property column="Email" name="Email" type="string"/>

<property column="Pwd" name="Pwd" type="string"/>

<property column="Note" name="Note" type="string"/>

<property column="IsActive" name="IsActive" type="boolean"/>

</class>

</hibernate-mapping>

hibernate.cfg.xml文件是........

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD   
3.0//EN"  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3307/userdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="hibernate/Users.hbm.xml"/>

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

我的Users.java pgm是..........

package hibernate;

// Generated 4 Dec, 2013 5:13:45 PM by Hibernate Tools 3.4.0.CR1

/**
 * Users generated by hbm2java
 */
public class Users implements java.io.Serializable {

private long UserId;
private String FName;
private String LName;
private long UserTypeId;
private String UserName;
private String Email;
private String Pwd;
private String Note;
private boolean IsActive;

public Users() {
}

public Users(long UserId) {
    this.UserId = UserId;
}

public Users(long UserId, String FName, String LName, long UserTypeId,
        String UserName, String Email, String Pwd, String Note,
        boolean IsActive) {
    this.UserId = UserId;
    this.FName = FName;
    this.LName = LName;
    this.UserTypeId = UserTypeId;
    this.UserName = UserName;
    this.Email = Email;
    this.Pwd = Pwd;
    this.Note = Note;
    this.IsActive = IsActive;
}

public long getUserId() {
    return this.UserId;
}

public void setUserId(long UserId) {
    this.UserId = UserId;
}

public String getFName() {
    return this.FName;
}

public void setFName(String FName) {
    this.FName = FName;
}

public String getLName() {
    return this.LName;
}

public void setLName(String LName) {
    this.LName = LName;
}

public long getUserTypeId() {
    return this.UserTypeId;
}

public void setUserTypeId(long UserTypeId) {
    this.UserTypeId = UserTypeId;
}

public String getUserName() {
    return this.UserName;
}

public void setUserName(String UserName) {
    this.UserName = UserName;
}

public String getEmail() {
    return this.Email;
}

public void setEmail(String Email) {
    this.Email = Email;
}

public String getPwd() {
    return this.Pwd;
}

public void setPwd(String Pwd) {
    this.Pwd = Pwd;
}

public String getNote() {
    return this.Note;
}

public void setNote(String Note) {
    this.Note = Note;
}

public boolean isIsActive() {
    return this.IsActive;
}

public void setIsActive(boolean IsActive) {
    this.IsActive = IsActive;
}

    }

HibernateUtil.java pgm是........

package hibernate;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {



    try {



        sessionFactory = new Configuration().configure()

        .buildSessionFactory();

    } catch (Throwable ex) {

        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {

    return sessionFactory;

  }

   }

我的TestMain程序是......

package hibernate;

import hibernate.Users;

import hibernate.HibernateUtil;



import java.util.List;



import org.hibernate.Session;

import org.hibernate.Transaction;



 public class TestMain {



 /**

 * @param args

 */

public static void main(String[] args) {

    TestMain obj = new TestMain();

    //obj.saveRecord();

    obj.updateUser(12);

    obj.deleteUser(13);

    obj.getList();

}



public void saveRecord()

{

    Users u = new Users(0, "Jitendra", "Zaa", 1, "jitendra.zaa",   
 "jitendra.zaa@shivasoft.in", "test", "this is note", true);

    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = null;

    try

    {

        transaction = session.beginTransaction();

        session.save(u);

       transaction.commit();

        System.out.println("Data Saved");

    }catch(Exception e)

    {

        e.printStackTrace();

    }finally{session.close();}



}

public void deleteUser(long UserId)

{

    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = null;

    try

    {

        transaction = session.beginTransaction();

        Users u = (Users)session.get(Users.class,UserId);

        session.delete(u);

        transaction.commit();

        System.out.println("Data Deleted");

    }

    catch(Exception e)

    {

        e.printStackTrace();

    }

    finally{

       session.close();

   }

}

public void updateUser(long UserId)

{

    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = null;

    try

    {

        transaction = session.beginTransaction();

        Users u = (Users)session.get(Users.class,UserId);

        u.setFName("ShivaSoft");

        transaction.commit();

        System.out.println("Data Updated");

    }

    catch(Exception e)

    {

        e.printStackTrace();

    }

    finally{

        session.close();

    }

}



public void getList()

{

    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = null;

    try

    {

       transaction = session.beginTransaction();

        List<Users> uList = session.createQuery("from Users").list();

        for(Users u : uList)

        {

            System.out.println("First Name - "+u.getFName());

        }

    }

    catch(Exception e)

    {

        e.printStackTrace();

    }

    finally{

        session.close();

    }
}

}

请帮助我.......

1 个答案:

答案 0 :(得分:1)

未找到类:com.mysql.jdbc.Driver

1)。请首先确认您的lib文件夹中有mysql.connector.java.jar文件     或者这是jar成功添加你的项目