java swing 通过hibernate连接数据库的客户端登录

来源:互联网 发布:stringbuffer转成数组 编辑:程序博客网 时间:2024/06/05 18:32

//客户端程序

package Hibernate;



import java.awt.BorderLayout;


public class LogIn extends JFrame {


private JPanel contentPane;
private JTextField Name;
private JTextField Password;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LogIn frame = new LogIn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public LogIn() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(22, 10, 384, 242);
contentPane.add(panel);
panel.setLayout(null);

JLabel NameLabel = new JLabel("登录名:");
NameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
NameLabel.setBounds(90, 97, 54, 15);
panel.add(NameLabel);

JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(162, 10, 73, 64);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\fengzhili-5\\Desktop\\preferences_system_login_64px_541072_easyicon.net.png"));
panel.add(lblNewLabel);

JLabel PasswordLabel = new JLabel("密   码:");
PasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
PasswordLabel.setBounds(90, 141, 54, 15);
panel.add(PasswordLabel);

Name = new JTextField();
Name.setBounds(166, 94, 89, 21);
panel.add(Name);
Name.setColumns(10);

Password = new JTextField();
Password.setBounds(166, 138, 89, 21);
panel.add(Password);
Password.setColumns(10);

JButton LogIn = new JButton("登录");
LogIn.setFont(new Font("微软雅黑", Font.PLAIN, 12));
LogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User c=new User();

//c.setPassword(Password.getText());
Service a = new Service();
if(a.findUserByName(Name.getText())){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}
});
LogIn.setBounds(89, 187, 73, 23);
panel.add(LogIn);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
JButton Cancel = new JButton("取消");
Cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
Cancel.setBounds(175, 187, 80, 23);
panel.add(Cancel);
}

}

//service 

package Hibernate;


import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;




public class Service {
 Configuration cfg = new Configuration().configure();  
      SessionFactory factory = cfg.buildSessionFactory();  
      Session session = null;
private Serializable name;
public void saveUser(User user) {
session = factory.openSession();  
         //开启事务  
         session.beginTransaction();  
/*User c = new User();
c.setName("bibi");*/
session.save(user);  
         //提交事务  
         session.getTransaction().commit();  
}


public void updateUser(User user) {
session = factory.openSession();  
        //开启事务  
        session.beginTransaction(); 
/*User c = new User();
c.setId(3);
c.setName("mama");*/
session.update(user);
//session.save(c);  
        //提交事务  
        session.getTransaction().commit();  
}


public User findUserById(int id) {
session = factory.openSession();  
        //开启事务  
        session.beginTransaction(); 
//Integer id = 1;
User c = (User) session.get(User.class, id);
System.out.println(c.getId() + c.getName());
session.getTransaction().commit(); 
return c;
 
}
public User delete(String ii) {
session = factory.openSession();  
        //开启事务  
User user=new User();
user.setName(ii);
user.setId(2);
user.setPassword("123456");
        session.beginTransaction(); 
//Integer id = 1;
        session.delete(user);
        System.out.println("删除成功");
// User c = (User) session.get(User.class, user.getName());//name
// System.out.println(c.getId() + c.getName());
session.getTransaction().commit(); 
return null;
 
}


public boolean findUserByName(String name) {


session = factory.openSession();  
        //开启事务  
// user.setName(name);
        session.beginTransaction(); 
//Integer id = 1;
        Query query= session.createQuery("from User where name=? ");
        query.setString(0, name);
        List<User> user=query.list();
        System.out.println("查询成功");
// User c = (User) session.get(User.class, user.getName());//name
// System.out.println(c.getId() + c.getName());
session.getTransaction().commit(); 
if(user.size()!=0){
return true;
}
return false;
 

}
}

//hibernate配置

<?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>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/personal
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>


<!-- 配置数据库的方言,通过方言,让计算机知道连接的是哪种数据库 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!--在控制台上输出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!--格式化sqlk语句 -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 加载映射文件 -->
<!-- <mapping resource="cn/itcast/primer/customer.hbm.xml"/>  -->
<!-- <mapping resource="/cn/itcast/primer/data/User.hbm.xml" /> -->
<mapping resource="User.hbm.xml" />


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

//实体类

package Hibernate;


public class User {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
/*public User(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}*/
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

//结果


0 0