package junit4;
import java.util.Date;
import hibernate.Students;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory =config.buildSessionFactory(serviceRegistry);
//打开会话
session = sessionFactory.openSession();
//打开事物
transaction = session.beginTransaction();
System.out.println(1);
}
@After
public void destory(){
transaction.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSaveStudents(){
//生成学生对象
Students s =new Students(1,"张三丰","男",new java.sql.Date(new Date().getTime()),"武当山");
session.save(s);//保存对象进入数据库
}
}
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-001.students (name, gender, birthday, adress, sid) values ('张三丰', '男', ' at line 1
添加回答
举报
0/150
提交
取消