@
文章目录
电影购票系统模拟真实购票流程,在线选座,充值购票,影院信息管理。登录用户分为:普通用户+管理员
数据库连接
BaseDao类,建立数据库连接
代码如下:
package daoimpl;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BaseDao {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/moviesystem?useUnicode=true&characterEncoding=UTF-8";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 与数据库建立连接
*
* @return
*/
public static Connection getconn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放所有资源
*
*/
public static void closeAll(ResultSet rs, PreparedStatement psts, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (psts != null) {
psts.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 此方法可以完成对数据库的增删改操作
*
* @param sql
* @param params
* @return
*/
public static boolean operUpdata(String sql, List params) {
int res = 0;//返回影响的行数
Connection conn = null;
PreparedStatement psts = null;//执行sql语句
ResultSet rs = null;
try {
conn = getconn();// 与数据库建立连接
psts = conn.prepareStatement(sql);// 装载sql语句
if (params != null) {// 假如有?号,在执行之前把问号替换掉
for(int i = 0; i < params.size(); i++) {
psts.setObject(i + 1, params.get(i));
}
}
res = psts.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(rs, psts, conn);
}
return res > 0 ? true : false;
}
/*
* 实现查的操作
*
*/
public static List operQuery(String sql, List p, Class cls)throws Exception {
Connection conn = null;
PreparedStatement pste = null;// 预处理语句
ResultSet rs = null;// 结果集
List list = new ArrayList();
conn = getconn();
try {
pste = conn.prepareStatement(sql);
if (p != null) {// 将条件值装入预处理语句
for (int i = 0; i < p.size(); i++) {
pste.setObject(i + 1, p.get(i));
}
}
rs = pste.executeQuery();// 执行
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
T entity = cls.newInstance();// 反射
for (int j = 0; j < rsmd.getColumnCount(); j++) {
// 从数据库中取得字段名
String col_name = rsmd.getColumnName(j + 1);
Object value = rs.getObject(col_name);
Field field = cls.getDeclaredField(col_name);
field.setAccessible(true);// 类中的成员变量为private,故必须进行此操作
field.set(entity, value);// 给实体类entity的field属性赋值
}
list.add(entity);// 加入list列表
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(rs, pste, conn);// 关闭连接,释放资源
}
return list;
}
}
主要页面
登录页面:LoginUI
package view;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import entity.User;
import service.UserService;
import serviceimpl.UserServiceImpl;
class Login implements ActionListener {
private JFrame jf = new JFrame("电影系统");
private Container con = jf.getContentPane();// 获得面板
private Toolkit toolkit = Toolkit.getDefaultToolkit();
private Dimension sc = toolkit.getScreenSize();// 获得屏幕尺寸
private JLabel title = new JLabel("欢迎使用电影购票系统");
private JLabel name1 = new JLabel("用户名");
private JLabel pass1 = new JLabel("密 码");
private JTextField textName = new JTextField();
private JPasswordField textPs = new JPasswordField();// 密码框
private JRadioButton choice1 = new JRadioButton("用户");
private JRadioButton choice2 = new JRadioButton("管理员");
private JLabel code1 = new JLabel("验证码");
private JTextField textCode = new JTextField();
private JLabel code2 = new JLabel();
private JButton btn_login = new JButton("登录");
private JButton btn_rgist = new JButton("注册");
// 按钮
private Font font = new Font("楷体", 1, 28);
private Font font1 = new Font("楷体", 0, 20);
private Font font2 = new Font("楷体", 0, 18);
// 字体,样式(粗体,斜体),大小
private ButtonGroup buttongroup = new ButtonGroup();
private ImageIcon loginbg = new ImageIcon("image/loginbg.jpg");
public Login() {
con.setLayout(null);
jf.setSize(1000, 618);
jf.setLocation((sc.width - 1000) / 2, (sc.height - 618) / 2);
jf.setResizable(false);// 窗口大小不可变
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.setVisible(true);
JLabel maxlabel = new JLabel(loginbg);
title.setBounds(375, 10, 370, 30);
title.setFont(font);
//title.setForeground(Color.black);
title.setForeground(Color.white);
name1.setBounds(140, 140, 85, 30);// 账号的位置大小
name1.setFont(font1);// 字体
name1.setForeground(Color.red);// name1字的颜色
pass1.setBounds(140, 190, 85, 30);// 密码的位置大小
pass1.setForeground(Color.red);
pass1.setFont(font1);
textName.setBounds(200, 143, 140, 25);
textName.setBorder(null);// 边框
textName.setFont(font1);
textPs.setBounds(200, 193, 140, 25);
textPs.setBorder(null);
textPs.setEchoChar('*');// 可以将密码显示为* ;默认为·
textPs.setFont(font1);
choice1.setBounds(140, 290, 120, 25);
choice1.setSelected(true);// 默认普通用户登录
choice2.setBounds(260, 290, 80, 25);
code1.setBounds(140, 240, 60, 25);
code1.setFont(font1);
code1.setForeground(Color.black);
textCode.setBounds(200, 240, 95, 25);
textCode.setBorder(null);
textCode.setFont(font1);
code2.setBounds(300, 240, 70, 25);
code2.setFont(font1);
code2.setText(code());
code2.setForeground(Color.black);
code1.setVisible(false);
code2.setVisible(false);
textCode.setVisible(false);
btn_login.setBounds(140, 340, 90, 25);//140, 340, 90, 25
btn_login.setFont(font2);
btn_login.addActionListener(this);
jf.getRootPane().setDefaultButton(btn_login);//回车默认是登录按钮
btn_rgist.setBounds(250, 340, 90, 25);//250, 340, 90, 25
btn_rgist.setFont(font2);
btn_rgist.addActionListener(this);
JLabel bg = new JLabel(loginbg);
maxlabel.add(title);
maxlabel.add(name1);
maxlabel.add(pass1);
maxlabel.add(textName);
maxlabel.add(textPs);
maxlabel.add(choice1);
maxlabel.add(choice2);
buttongroup.add(choice1);
buttongroup.add(choice2);
maxlabel.add(code1);
maxlabel.add(code2);
maxlabel.add(textCode);
maxlabel.add(btn_login);
maxlabel.add(btn_rgist);
maxlabel.setBounds(0, 0, 999, 617);
con.add(maxlabel);
}
private String code() {
int m = 0;
for (int i = 0; i < 4; i++) {
m *= 10;
m += (int) (Math.random() * 9 + 1);
}
return ((Integer) m).toString();
}
public void cleanUserInfo() {
this.textName.setText("");
this.textPs.setText("");
this.textCode.setText("");
}
public static void winMessage(String str) {// 提示窗口,有多个地方调用
JOptionPane.showMessageDialog(null, str, "提示",
JOptionPane.INFORMATION_MESSAGE);
}<