1,使用Swing完成用户登录程序。
2,使用Swing完成用户注册程序。
package com.frame;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BaseDao {
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
private List params=new ArrayList();//表示SQL的参数
public void setParams(List params) {
this.params = params;
}
public void setPreparedStatement (){
for (int i = 0; i < params.size(); i++) {
try {
pstmt.setObject(i+1, params.get(i));
} catch (Exception e) {
e.printStackTrace();
}
}
}
//连接数据库
public Connection getConnection(){
try {
InputStream in=this.getClass().getResourceAsStream("/db.properties");//在包下获取资源
Properties prop=new Properties();
prop.load(in);//按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
Class.forName(prop.getProperty("driver"));//返回与带有给定字符串名的类或接口相关联的 Class 对象。
return DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("user"), prop.getProperty("password"));
//试图建立到给定数据库 URL 的连接。
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
//增删改
public int update(String sql){
conn=getConnection();
try {
pstmt=conn.prepareStatement(sql);
this.setPreparedStatement();
return pstmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return 0;
}finally{
close();
}
}
//利用反射改造查询方法
public List query(String sql,Class cls){
List list=new ArrayList();
conn=getConnection();
try {
pstmt=conn.prepareStatement(sql);
this.setPreparedStatement();
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
Object obj=cls.newInstance();
Field[]fs=cls.getDeclaredFields();
for (Field f : fs) {
Method m=cls.getDeclaredMethod("set"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1),f.getType());
m.invoke(obj, rs.getObject(f.getName()));
}
list.add(obj);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}finally{
this.close();
}
return list;
}
//关闭资源
public void close(){
try {
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.frame;
public class GuiUser {
private String name;
private String password;
public GuiUser() {
super();
}
public GuiUser(String name, String password) {
super();
this.name = name;
this.password = password;
}
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;
}
}
/*
* login.java
*
* Created on __DATE__, __TIME__
*/
package com.frame;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author __USER__
*/
public class Login extends javax.swing.JFrame {
/** Creates new form login */
public Login() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
"/img/001.jpg"))); // NOI18N
jLabel4.setText("\u7528\u6237\u540d\uff1a");
jLabel5.setText("\u5bc6 \u7801\uff1a");
jButton1.setText("\u6ce8\u518c");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("\u767b\u5f55");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource(
"/com/sun/java/swing/plaf/motif/icons/Question.gif"))); // NOI18N
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource(
"/com/sun/java/swing/plaf/motif/icons/Warn.gif"))); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(84, 84, 84)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addGap(75, 75,
75)
.addComponent(
jButton1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
jButton2))
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(
layout.createSequentialGroup()
.addComponent(
jLabel6)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(
layout.createSequentialGroup()
.addComponent(
jLabel7)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jLabel5)
.addComponent(
jLabel4))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(
jPasswordField1,
0,
0,
Short.MAX_VALUE)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
146,
javax.swing.GroupLayout.PREFERRED_SIZE))))));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
110,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(
jLabel2)
.addGap(26, 26,
26)
.addComponent(
jLabel3))
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26,
26,
26)
.addComponent(
jPasswordField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(49,
49,
49))
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
2,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel4)
.addComponent(
jLabel6))
.addGap(26,
26,
26)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jLabel5)
.addComponent(
jLabel7))
.addGap(47,
47,
47)))
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jButton1)
.addComponent(
jButton2))))
.addContainerGap(117, Short.MAX_VALUE)));
pack();
}//
//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//注册
String name = jTextField1.getText();
String password = new String(jPasswordField1.getPassword());
BaseDao dao = new BaseDao();
List list = new ArrayList();
String sql = "select * from guiuser where name=?";
list.add(name);
dao.setParams(list);
List l = dao.query(sql, GuiUser.class);
if(name.length()==0||password.length()==0){
//replace(" ", "") 去掉所有空格的方法
JOptionPane.showMessageDialog(null, "用户名或密码不能为空");
this.setVisible(false);
this.dispose();//释放资源
}else if (l.size() > 0) {
JOptionPane.showMessageDialog(null, "该用户已注册!");
}else{
String input="insert into guiuser values(?,?)";
List list1=new ArrayList();
list1.add(name);
list1.add(password);
dao.setParams(list1);
dao.update(input);
JOptionPane.showMessageDialog(null, "注册成功!!");
}
this.setVisible(false);
this.dispose();//释放资源
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//登录
String name = jTextField1.getText();
String password = new String(jPasswordField1.getPassword());
BaseDao dao = new BaseDao();
List list = new ArrayList();
String sql = "select * from guiuser where name=? and password=?";
list.add(name);
list.add(password);
dao.setParams(list);
List l = dao.query(sql, GuiUser.class);
if (l.size() > 0) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else
JOptionPane.showMessageDialog(null, "登录失败!");
this.setVisible(false);
this.dispose();//释放资源
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
连接数据库用db文件
//2019,1,11 20:03