大侠、高手进来瞧瞧!!!
User类代码:
package com.bjsxt.registration.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id ;
private String name ;
private String password ;
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
UserManager类代码:
package com.bjsxt.registration.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.util.HibernateUtil;
public class UserManager {
public boolean exists(User u)throws Exception{
System.out.println("aaaaa");
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s = sf.getCurrentSession();
s.beginTransaction();
Long count = (Long)s.createQuery("select count(*) from User u where u.name=:name").setString("name", u.getName()).uniqueResult();
s.getTransaction().commit();
if(count > 0)return true ;
return false ;
}
public boolean exists(User u)throws Exception{
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring" , "root" , "mysqladmin");
String sqlQuery = "select count(*)from user where name=?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1,u.getName());
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
conn.close();
if(count > 0){
return true;
}
return false;
}
}
HibernateUtil类代码:
package com.bjsxt.registration.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static SessionFactory sf ;
static{
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sf ;
}
}
UserManagerTest类代码:
package com.bjsxt.registration.service;
import junit.framework.Assert;
import org.junit.Test;
import com.bjsxt.registration.model.User;
public class UserManagerTest{
@Test
public void testExists() throws Exception
{
UserManager um = new UserManager();
User u = new User();
u.setName("b");
boolean exists = um.exists(u);
System.out.println(exists);
}
}
在测试类UserManagerTest中运行testExists方法:如果用UserManager类中红色代码:每次都为false。不管数据库中有没有“b”字段。而用蓝色代码运行,就会数据库有字段“b”为true,没有才会false。
我想问,怎么把红色代码改了,使它运行时的结果和蓝色代码的一样呢?
------解决方案--------------------
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s = sf.getCurrentSession();
Connection conn =s.connection();
String sqlQuery = "select count(*)from user where name=?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1,u.getName());
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
conn.close();
if(count > 0){
return true;