Student.class:
package cn.cstp.org.action2;
//POJO类
public class Student {
//写入若干属性
private String id;
private String sname;
private String sno;
private String sex;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSno() {
return sno;
} public void setSno(String sno) {
this.sno = sno;
}
}
StudentOperate.java:
package cn.cstp.org.action2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class StudentOperate {
//在Hibernate中,所有的操作都是通过Session完成
//此Session不同于JSP的Session
private Session session=null;
//在构造方法之中实例化session对象
public StudentOperate(){
// 找到Hibernate配置
Configuration config=new Configuration().configure();
//从配置中取出SessionFactory
SessionFactory factory=config.buildSessionFactory();
//从SessionFactory中取出一个Session
this.session=factory.openSession();
} //所有操作都是由session进行的
//向数据库中增加数据
public void insert(Student student){
//开始事务
Transaction tran=this.session.beginTransaction(); //执行语句
this.session.save(student); //提交事务
tran.commit();
}
}
StudentTest.java:
package cn.cstp.org.action2;public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//生成POJO类实例化对象
Student stu=new Student();
stu.setId("2");
stu.setSname("3");
stu.setSno("3");
stu.setSex("3");
stu.setEmail("3"); //实例化 StudentOperate 对象
StudentOperate op=new StudentOperate();
op.insert(stu);
}
}
例子是网上的,按着来做但最后还是没显示出SQL语句,进入MYSQL后,查看过数据,还是没修改,请高手帮帮忙!!!
运行后,控制台结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
package cn.cstp.org.action2;
//POJO类
public class Student {
//写入若干属性
private String id;
private String sname;
private String sno;
private String sex;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSno() {
return sno;
} public void setSno(String sno) {
this.sno = sno;
}
}
StudentOperate.java:
package cn.cstp.org.action2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
public class StudentOperate {
//在Hibernate中,所有的操作都是通过Session完成
//此Session不同于JSP的Session
private Session session=null;
//在构造方法之中实例化session对象
public StudentOperate(){
// 找到Hibernate配置
Configuration config=new Configuration().configure();
//从配置中取出SessionFactory
SessionFactory factory=config.buildSessionFactory();
//从SessionFactory中取出一个Session
this.session=factory.openSession();
} //所有操作都是由session进行的
//向数据库中增加数据
public void insert(Student student){
//开始事务
Transaction tran=this.session.beginTransaction(); //执行语句
this.session.save(student); //提交事务
tran.commit();
}
}
StudentTest.java:
package cn.cstp.org.action2;public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//生成POJO类实例化对象
Student stu=new Student();
stu.setId("2");
stu.setSname("3");
stu.setSno("3");
stu.setSex("3");
stu.setEmail("3"); //实例化 StudentOperate 对象
StudentOperate op=new StudentOperate();
op.insert(stu);
}
}
例子是网上的,按着来做但最后还是没显示出SQL语句,进入MYSQL后,查看过数据,还是没修改,请高手帮帮忙!!!
运行后,控制台结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
想修改要使用update()一般用saveOrUpdate()让hibernate自己去判断是执行insert还是update
<property name="show_sql">true</property>
显示sql语句 在配置文件里面写上这
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">abdd</property>
<property name="connection.password">abc</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="cn/cstp/org/action2/Student.hbm.xml" /> </session-factory></hibernate-configuration>
jdbc:mysql://localhost:3306/
</property>
数据库名呢???????
jdbc:mysql://localhost:3306/
</property>
数据库名呢???????没数据名