JDBC+Mysql+Swing模拟公交信息查询系统

JDBC+Mysql+Swing创建公交信息查询系统

JDBC+Mysql+Swing创建公交信息查询系统

开发JDBC接口的数据库应用程序
设计公交线路与客流统计管理界面,实现如下功能,包括:
(1)添、改、删公交线路信息
(2)添、改、删某条公交线路的车站信息;
(3)查询某条公交线路的车站信息;查询某条公交线路的客流统计信息;查询某个时段的客流统计信息;

源码

源码下载

1、 开发工具和版本,各种插件

  • 开发工具为EclipseIDE(4.12.0)
  • 用到的插件:windowsbuilder
  • Jdk版本是 javaSE-12 Mysql的驱动包为8.0.19版本

2、 开发的各个源文件说明,文件作用

3、数据库系统和版本,jdbc驱动jar包,需要如何配置和建表和插入数据语句

数据库采用的是MySQL8.0版本
数据库管理工具是navicat premium 12
Mysql的驱动包为8.0.19版本
Jar包在项目中构建path添加即可
数据库中有两张表

建表和插入语句会打包放在文件中

4、程序入口在哪,如何运行等。

程序入口为main.Java,运行即可进入登录界面

在这里插入图片描述

效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
**
**

Mysql

基于某地区公交巴士的乘车刷卡数据(表transinfo),进行统计
CREATE TABLE TransInfo(
CardNo decimal(15, 0) NULL, – 乘车卡号码
BoardingTime datetime NULL,-- 上车时间
BusLine char(20) NULL,-- 公交线路
BoardingStation smallint NULL,-- 上车站编号
OffStation smallint NULL,-- 下车站编号
TransAmount decimal(6, 2) NULL – 乘车费用
);

除此之外还有个user表
**

JAVA代码

**

import com.jdbcsy.view.logOnFrm;

public class Main {

	/**
	 * 程序入口
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new logOnFrm().setVisible(true);
	}

}



package com.jdbcsy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.StringUtil;

/**
 * 公交信息Dao类
 * 
 * @author sy
 *
 */
public class TransinfoDao {

	/**
	 * 公交线路添加
	 * 
	 * @param con
	 * @param transinfo
	 * @return
	 * @throws Exception
	 */
	public int add(Connection con, Transinfo transinfo) throws Exception {
		String sql = "insert into transinfo values(?,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setLong(1, transinfo.getCardNo());
		pstmt.setString(2, transinfo.getBoardingTime());
		pstmt.setString(3, transinfo.getBusLine());
		pstmt.setInt(4, transinfo.getBoardingStation());
		pstmt.setInt(5, transinfo.getOffStation());
		pstmt.setDouble(6, transinfo.getTransAmout());

		return pstmt.executeUpdate();
	}

	/**
	 * 查询公交信息类别的集合
	 * 
	 * @param con
	 * @param transinfo
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con, Transinfo transinfo) throws Exception {
		StringBuffer sb = new StringBuffer("select * from transinfo");
		if (transinfo.getCardNo() > 0) {
			sb.append(" and CardNo = " + transinfo.getCardNo());
		}
		if (StringUtil.isNotEmpty(transinfo.getBoardingTime())) {
			sb.append(" and BoardingTime like '%" + transinfo.getBoardingTime() + "%'");
		}
		if (StringUtil.isNotEmpty(transinfo.getBusLine())) {
			sb.append(" and BusLine = '" + transinfo.getBusLine() + "'");
		}
		if (transinfo.getBoardingStation() > 0) {
			sb.append(" and BoardingStation = '" + transinfo.getBoardingStation() + "'");
		}
		if (transinfo.getOffStation() > 0) {
			sb.append(" and OffStation = '" + transinfo.getOffStation() + "'");
		}
		if (transinfo.getTransAmout() > 0) {
			sb.append(" and TransAmount = " + transinfo.getTransAmout());
		}

		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));

		return pstmt.executeQuery();
	}

	/**
	 * 删除公交线路信息
	 * 
	 * @param con
	 * @param cardNo
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con, Long cardNo) throws Exception {
		String sql = "delete from transinfo where CardNo =? ";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setLong(1, cardNo);
		return pstmt.executeUpdate();
	}

	/**
	 * 修改公交线路信息
	 * 
	 * @param con
	 * @param transinfo
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con, Transinfo transinfo) throws Exception {
//		String sql = "update transinfo where CardNo = ? , BoardingTime = ? , BusLine = ? , BoardingStation =? , OffStation = ? , TransAmount = ?";
		String sql = "update transinfo set BusLine=?,BoardingStation=?,OffStation=? where CardNo =?";

//		System.out.println(transinfo.getCardNo());
//		String a = transinfo.getBusLine();
//		int b = transinfo.getBoardingStation();
//		int c = transinfo.getOffStation();
//		long d = transinfo.getCardNo();

		PreparedStatement pstmt = con.prepareStatement(sql);
//		pstmt.setInt(1, transinfo.getCardNo());
//		pstmt.setString(2, transinfo.getBoardingTime());
//		pstmt.setString(3, transinfo.getBusLine());
//		pstmt.setInt(4, transinfo.getBoardingStation());
//		pstmt.setInt(5, transinfo.getOffStation());
//		pstmt.setDouble(6, transinfo.getTransAmout());
		pstmt.setString(1, transinfo.getBusLine());
		pstmt.setInt(2, transinfo.getBoardingStation());
		pstmt.setInt(3, transinfo.getOffStation());
		pstmt.setLong(4, transinfo.getCardNo());

		return pstmt.executeUpdate();
	}

	/**
	 * 查询某条公交线路的车站信息
	 * 
	 * @param con
	 * @param transinfo
	 * @return
	 */
	public ResultSet listStation(Connection con, Transinfo transinfo ,int flag) throws Exception {
		StringBuffer sb = new StringBuffer("SELECT DISTINCT c.BusLine busLine ,c.station Station "
				+ "FROM( SELECT a.BusLine, a.BoardingStation station FROM transinfo a UNION ALL "
				+ "( SELECT  b.BusLine, b.OffStation FROM transinfo b ) ) c ");
		if (StringUtil.isNotEmpty(transinfo.getBusLine())) {
			sb.append("WHERE busLine = '" + transinfo.getBusLine() + "'");
			sb.append("ORDER BY station	");
		} else if (flag == 1) {
			sb.append(" ORDER BY busLine	");
		} else{
			sb.append("GROUP BY BusLine ORDER BY Station	");
		} 

		PreparedStatement pstmt = con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	/**
	 * 查询某条公交线路的客流统计信息
	 * @param con
	 * @param transinfo
	 * @return
	 * @throws Exception
	 */
	public ResultSet listPassengerAmount(Connection con, Transinfo transinfo) throws Exception{
		StringBuffer sb = new StringBuffer("SELECT BusLine, COUNT( CardNo ) as passengersAmount FROM transinfo ");
		if (StringUtil.isNotEmpty(transinfo.getBusLine())) {
			sb.append("WHERE busLine = '" + transinfo.getBusLine() + "'");
			sb.append(" GROUP BY BusLine");
		} else{
			sb.append("GROUP BY BusLine");
		} 
		PreparedStatement pstmt = con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	
	/**
	 * 查询某个时段的客流统计信息
	 * @param con
	 * @param transinfo
	 * @return
	 * @throws Exception
	 */
	public ResultSet listTimePassengerAmount(Connection con, Transinfo transinfo) throws Exception{
		StringBuffer sb = new StringBuffer("SELECT COUNT(CardNo) as amount FROM transinfo WHERE BoardingTime BETWEEN  ");
		if (StringUtil.isNotEmpty(transinfo.getStartTime())  &&  StringUtil.isNotEmpty(transinfo.getEndTime()) == true){
			sb.append("'"+transinfo.getStartTime()+"'"+"AND '"+transinfo.getEndTime()+"'");
		} else{
			
		} 
		
		PreparedStatement pstmt = con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}

}

package com.jdbcsy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.jdbcsy.model.User;
import com.mysql.cj.xdevapi.PreparableStatement;

/**
 * 用户Dao类
 * @author sy
 *
 */
public class UserDao {
	
	/**
	 * 登录验证
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con,User user) throws Exception{
		User resultUser = null;
		String sql = "select * from t_user where username = ? and password = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassword());
		ResultSet rs = pstmt.executeQuery();
		if(rs.next()) {
			//如果查到的话就会只有一条记录
			resultUser = new User();
			resultUser.setId(rs.getInt("id"));
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassword(rs.getString("password"));
		}
		
		return resultUser;
	}
}

package com.jdbcsy.model;

import java.math.BigInteger;

/**
 * 公交信息类别实体
 * 
 * @author sy
 *
 */
public class Transinfo {

	private long CardNo; // 乘车卡号码
	private String BoardingTime;// 上车时间
	private String BusLine; // 公交线路
	private int BoardingStation; // 上车站编号
	private int OffStation; // 下车站标号
	private double TransAmount; // 乘车费用

	private String startTime;// 查询某个时段的客流统计信息时的起始时间
	private String endTime; // 查询某个时段的客流统计信息时的截止时间

	public Transinfo() {
		super();
	}

	public Transinfo(String busLine) {
		super();
		BusLine = busLine;
	}

	public Transinfo(String startTime, String endTime) {
		super();
		this.startTime = startTime;
		this.endTime = endTime;
	}

	public Transinfo(String busLine, int boardingStation, int offStation) {
		super();
		BusLine = busLine;
		BoardingStation = boardingStation;
		OffStation = offStation;
	}

	public Transinfo(Long cardNo, String busLine, int boardingStation, int offStation) {
		super();
		CardNo = cardNo;
		BusLine = busLine;
		BoardingStation = boardingStation;
		OffStation = offStation;
	}

	public Transinfo(Long cardNo, String boardingTime, String busLine, int boardingStation, int offStation,
			double transAmount) {
		super();
		CardNo = cardNo;
		BoardingTime = boardingTime;
		BusLine = busLine;
		BoardingStation = boardingStation;
		OffStation = offStation;
		TransAmount = transAmount;
	}

	public Long getCardNo() {
		return CardNo;
	}

	public void setCardNo(Long cardNo) {
		CardNo = cardNo;
	}

	public String getBoardingTime() {
		return BoardingTime;
	}

	public void setBoardingTime(String boardingTime) {
		BoardingTime = boardingTime;
	}

	public String getBusLine() {
		return BusLine;
	}

	public void setBusLine(String busLine) {
		BusLine = busLine;
	}

	public int getBoardingStation() {
		return BoardingStation;
	}

	public void setBoardingStation(int boardingStation) {
		BoardingStation = boardingStation;
	}

	public int getOffStation() {
		return OffStation;
	}

	public void setOffStation(int offStation) {
		OffStation = offStation;
	}

	public double getTransAmout() {
		return TransAmount;
	}

	public void setTrasAmout(double transAmount) {
		TransAmount = transAmount;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	
}

package com.jdbcsy.model;

/**
 * 用户实体
 * @author sy
 *
 */
public class User {

	private int id; 
	private String userName; //用户名
	private String password; //密码
	
	
	
	public User() {
		super();
		// TODO 自动生成的构造函数存根
	}
	
	
	
	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}



	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	
}

package com.jdbcsy.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 数据库工具类
 * @author sy
 *
 */
public class Dbutil {
	
	private String dbUrl = "jdbc:mysql://127.0.0.1:3306/jdbcbus?serverTimezone=UTC";
	private String dbUserName = "root"; 
	private String dbPassword = "songYU";
	private String jdbcName = "com.mysql.cj.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
		return con;
	}
	
	/**
	 * 关闭数据库连接
	 * @param con
	 * @throws Exception
	 */
	public void closeCon(Connection con) throws Exception{
		if(con != null) {
			con.close();
		}
	}
	
	public static void main(String[] args) {
		Dbutil dbutil = new Dbutil();
		try {
			dbutil.getCon();
			System.out.println("数据库连接成功!");
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println("数据库连接失败!");
		}
	}
}

package com.jdbcsy.util;
/**
 * 字符串工具类
 * @author sy
 *
 */
public class StringUtil {
	
	/**
	 * 判断字符串是否是空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if(str == null || "".equals(str.trim())) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 判断是否不是空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		if(str!=null&&!"".equals(str.trim())) {
			return true;
		}else {
			return false;
		}
	}
}

package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class AboutUsJDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextArea textArea;
	private JLabel lblNewLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AboutUsJDialog dialog = new AboutUsJDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AboutUsJDialog() {
		setTitle("\u5173\u4E8E");
		setBounds(100, 100, 442, 437);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(Color.RED);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			textArea = new JTextArea();
			textArea.setBackground(Color.RED);
			textArea.setLineWrap(true);
			textArea.setEditable(false);
			textArea.setText("自己写的关于内容");
		}
		{
			lblNewLabel = new JLabel("");
			lblNewLabel.setIcon(new ImageIcon(AboutUsJDialog.class.getResource("/images/\u5173\u4E8E1.png")));
		}
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(142)
							.addComponent(lblNewLabel))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(48)
							.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 331, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(39, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(9)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
					.addContainerGap())
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.RED);
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			GroupLayout gl_buttonPane = new GroupLayout(buttonPane);
			gl_buttonPane.setHorizontalGroup(
				gl_buttonPane.createParallelGroup(Alignment.TRAILING)
					.addGap(0, 428, Short.MAX_VALUE)
			);
			gl_buttonPane.setVerticalGroup(
				gl_buttonPane.createParallelGroup(Alignment.LEADING)
					.addGap(0, 33, Short.MAX_VALUE)
			);
			buttonPane.setLayout(gl_buttonPane);
		}
		
		setLocationRelativeTo(null); //设置对话框相对屏幕居中
	}

}

package com.jdbcsy.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import com.jdbcsy.dao.UserDao;
import com.jdbcsy.model.User;
import com.jdbcsy.util.Dbutil;
import com.jdbcsy.util.StringUtil;

public class logOnFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTxt;
	private JPasswordField passwordTxt;
	
	private Dbutil dbutil = new Dbutil();
	private UserDao uerDao = new UserDao();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					logOnFrm frame = new logOnFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public logOnFrm() {
		setResizable(false);
		setTitle("\u516C\u4EA4\u7EBF\u8DEF\u8BBE\u8BA1\u4E0E\u5BA2\u6D41\u7EDF\u8BA1\u7BA1\u7406\u7CFB\u7EDF\u767B\u5F55");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 712, 468);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel label = new JLabel("\u516C\u4EA4\u7EBF\u8DEF\u8BBE\u8BA1\u4E0E\u5BA2\u6D41\u7EDF\u8BA1\u7BA1\u7406\u7CFB\u7EDF");
		label.setFont(new Font("微软雅黑 Light", Font.BOLD, 28));
		label.setIcon(new ImageIcon(logOnFrm.class.getResource("/images/\u516C\u4EA4\u8F66.png")));
		
		JLabel label_1 = new JLabel("\u5BC6   \u7801\uFF1A");
		label_1.setIcon(new ImageIcon(logOnFrm.class.getResource("/images/\u5BC6\u7801.png")));
		label_1.setFont(new Font("微软雅黑 Light", Font.BOLD, 17));
		
		JLabel label_2 = new JLabel("\u7528\u6237\u540D\uFF1A");
		label_2.setIcon(new ImageIcon(logOnFrm.class.getResource("/images/\u7528\u6237\u540D1.png")));
		label_2.setFont(new Font("微软雅黑 Light", Font.BOLD, 17));
		
		JButton button = new JButton("\u767B\u5F55");
		button.setFont(new Font("微软雅黑 Light", Font.BOLD, 17));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		
		JButton button_1 = new JButton("\u91CD\u7F6E");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		button_1.setFont(new Font("微软雅黑 Light", Font.BOLD, 17));
		
		userNameTxt = new JTextField();
		userNameTxt.setColumns(10);
		
		passwordTxt = new JPasswordField();
		
		JLabel label_3 = new JLabel("\u6570\u636E\u5E93\u539F\u7406\u4E0E\u6570\u636E\u4ED3\u5E93\u6280\u672F    \u5B8B\u745C 18251013");
		label_3.setFont(new Font("微软雅黑 Light", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(60, Short.MAX_VALUE)
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
					.addGap(53))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(174)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label_2)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(userNameTxt))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(button)
								.addComponent(label_1))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(130)
									.addComponent(button_1))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(passwordTxt)))))
					.addContainerGap(217, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(201)
					.addComponent(label_3)
					.addContainerGap(443, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(29)
					.addComponent(label)
					.addGap(41)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2)
						.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(58)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
					.addComponent(label_3))
		);
		contentPane.setLayout(gl_contentPane);
		
		this.setLocationRelativeTo(null);//设置jframe居中
	}
	
	/**
	 * 登录按钮功能
	 * @param e
	 */
	private void loginActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		String userName = this.userNameTxt.getText();
		String password = new String(this.passwordTxt.getPassword());  //getpassword得到的是char类型 需要转换
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空!");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空!");
			return;
		}
		User user = new User(userName,password);
		Connection con = null;
		try {
			con = dbutil.getCon();
			User currentUser = uerDao.login(con, user);
			if(currentUser != null) {
//				JOptionPane.showMessageDialog(null, "登陆成功!");
				dispose();
				new MainFrm().setVisible(true);
			}else {
				JOptionPane.showMessageDialog(null, "用户名密码错误!");
			}
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}finally {
			try {
				dbutil.closeCon(con);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 重置按钮功能
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		this.userNameTxt.setText("");
		this.passwordTxt.setText("");
		
	}
}

package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.jdbcsy.dao.TransinfoDao;
import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.Dbutil;
import com.jdbcsy.util.StringUtil;
import com.mysql.cj.protocol.Resultset;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigInteger;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class MainFrm extends JFrame {

	private Dbutil dbUtil = new Dbutil();
	private TransinfoDao transinfoDao = new TransinfoDao();

	private JPanel contentPane;
	private JDesktopPane table = null;
	private JTable transinfoTable;
	private JTextField CardNoTxt;
	private JTextField BoardingTimeTxt;
	private JTextField BusLineTxt;
	private JTextField BoardingStationTxt;
	private JTextField OffStationTxt;
	private JTextField TransAmountTxt;

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

	/**
	 * Create the frame.
	 */
	public MainFrm() {
		setTitle("\u516C\u4EA4\u7EBF\u8DEF\u4E0E\u5BA2\u6D41\u7EDF\u8BA1\u7BA1\u7406\u754C\u9762");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1081, 762);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menu = new JMenu("\u529F\u80FD\u4E00");
		menu.setFont(new Font("微软雅黑 Light", Font.BOLD, 24));
		menuBar.add(menu);

		JMenuItem menuItem = new JMenuItem("\u67E5\u8BE2\u516C\u4EA4\u7EBF\u8DEF\u8F66\u7AD9\u4FE1\u606F");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SelectStationDialog().setVisible(true);
			}
		});
//		menuItem.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				MainFrm mainfrm = new MainFrm();
//				mainfrm.setVisible(true);
//				table.add(mainfrm);
//			}
//		});
		menuItem.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		menu.add(menuItem);

		JMenuItem mntmNewMenuItem = new JMenuItem("\u67E5\u8BE2\u516C\u4EA4\u7EBF\u8DEF\u5BA2\u6D41\u4FE1\u606F");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SelectPassengersAmountDialog().setVisible(true);
			}
		});
		mntmNewMenuItem.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		menu.add(mntmNewMenuItem);

		JMenuItem menuItem_1 = new JMenuItem("\u5B89\u5168\u9000\u51FA");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int result = JOptionPane.showConfirmDialog(null, "是否退出系统?");
//				System.out.println(result); //测试得出   是:0 ; 否:1;  取消:2
				if (result == 0) {
					dispose();
				}
			}
		});
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u67E5\u8BE2\u67D0\u65F6\u6BB5\u5BA2\u6D41\u4FE1\u606F");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SelectTimePassengerDialog().setVisible(true);
			}
		});
		mntmNewMenuItem_2.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		menu.add(mntmNewMenuItem_2);
		menuItem_1.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		menu.add(menuItem_1);
		
		JMenu mnNewMenu = new JMenu("\u529F\u80FD\u4E8C");
		mnNewMenu.setFont(new Font("微软雅黑 Light", Font.BOLD, 24));
		menuBar.add(mnNewMenu);
		
		JMenuItem menuItem_2 = new JMenuItem("\u8C03\u7528\u5B58\u50A8\u8FC7\u7A0B\u6309\u7EBF\u8DEF\u67E5\u8BE2\u5BA2\u6D41\u4FE1\u606F");
		menuItem_2.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		mnNewMenu.add(menuItem_2);

		JMenu menu_1 = new JMenu("\u5173\u4E8E");
		menu_1.setFont(new Font("微软雅黑 Light", Font.BOLD, 24));
		menuBar.add(menu_1);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u5173\u4E8E\u7A0B\u5E8F");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				aboutUs_innerFrame aboutinnerFrm = new aboutUs_innerFrame();
//				aboutinnerFrm.setVisible(true);
//				table.add(aboutinnerFrm);
				new AboutUsJDialog().setVisible(true);
			}
		});
		mntmNewMenuItem_1.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		menu_1.add(mntmNewMenuItem_1);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JScrollPane scrollPane = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton button = new JButton("\u6DFB\u52A0");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new transinfoAddDialog().setVisible(true);
			}
		});
		button.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));
		
		JButton RefreshButton = new JButton("\u5237\u65B0");
		RefreshButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RefreshActionPerformed(e);
			}
		});
		RefreshButton.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 822, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_contentPane.createSequentialGroup()
								.addComponent(RefreshButton)
								.addGap(18)
								.addComponent(button))
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 998, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(29, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(29)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(button)
								.addComponent(RefreshButton))
							.addPreferredGap(ComponentPlacement.UNRELATED)))
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(65))
		);

		JLabel lblCardno = new JLabel("CardNo:");

		CardNoTxt = new JTextField();
		CardNoTxt.setEditable(false);
		CardNoTxt.setColumns(10);

		JLabel lblBoardingtime = new JLabel("BoardingTime:");

		BoardingTimeTxt = new JTextField();
		BoardingTimeTxt.setEditable(false);
		BoardingTimeTxt.setColumns(10);

		JLabel lblBusline = new JLabel("BusLine:");

		BusLineTxt = new JTextField();
		BusLineTxt.setColumns(10);

		JLabel lblBoardingstation = new JLabel("BoardingStation:");

		BoardingStationTxt = new JTextField();
		BoardingStationTxt.setColumns(10);

		JLabel lblOffstation = new JLabel("OffStation:");

		OffStationTxt = new JTextField();
		OffStationTxt.setColumns(10);

		JLabel lblTransamount = new JLabel("TransAmount:");

		TransAmountTxt = new JTextField();
		TransAmountTxt.setEditable(false);
		TransAmountTxt.setColumns(10);

		JButton UpdateButton = new JButton("\u4FEE\u6539");
		UpdateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TransinfoUpdateActionEvent(e);
			}
		});
		UpdateButton.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));

		JButton DeleteButton = new JButton("\u5220\u9664");
		DeleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TransinfoDeleteActionEvent(e);
			}
		});
		DeleteButton.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel
						.createParallelGroup(Alignment.LEADING).addComponent(lblBoardingtime).addComponent(lblCardno))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(CardNoTxt, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
						.addComponent(BoardingTimeTxt, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(Alignment.TRAILING,
								gl_panel.createSequentialGroup().addComponent(lblBoardingstation)
										.addPreferredGap(ComponentPlacement.RELATED).addComponent(BoardingStationTxt,
												GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup().addComponent(lblBusline)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(BusLineTxt, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)))
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup().addGap(18).addComponent(lblTransamount))
						.addGroup(Alignment.TRAILING,
								gl_panel.createSequentialGroup().addGap(24).addComponent(lblOffstation)))
				.addGap(4)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false).addComponent(TransAmountTxt)
						.addComponent(OffStationTxt, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
				.addPreferredGap(ComponentPlacement.RELATED, 49, Short.MAX_VALUE).addGroup(gl_panel
						.createParallelGroup(Alignment.TRAILING).addComponent(UpdateButton).addComponent(DeleteButton))
				.addContainerGap()));
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup().addContainerGap()
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblCardno)
										.addComponent(CardNoTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(OffStationTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblOffstation).addComponent(UpdateButton)
										.addComponent(BusLineTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(lblBusline))
								.addGap(18)
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblBoardingtime)
										.addComponent(BoardingTimeTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblBoardingstation)
										.addComponent(BoardingStationTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblTransamount)
										.addComponent(TransAmountTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(DeleteButton))
								.addContainerGap(12, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);

		transinfoTable = new JTable();
		transinfoTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				transinfoTableMousePressed(e);
			}
		});
		transinfoTable.setFont(new Font("微软雅黑 Light", Font.PLAIN, 15));
		transinfoTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "CardNo(\u4E58\u8F66\u5361\u53F7\u7801)", "BoardingTime(\u4E0A\u8F66\u65F6\u95F4)",
						"BusLine(\u516C\u4EA4\u7EBF\u8DEF)", "BoardingStation(\u4E0A\u8F66\u7AD9\u7F16\u53F7)",
						"OffStation(\u4E0B\u8F66\u7AD9\u7F16\u53F7)", "TransAmout(\u4E58\u8F66\u8D39\u7528)" }));
		transinfoTable.getColumnModel().getColumn(0).setPreferredWidth(149);
		transinfoTable.getColumnModel().getColumn(1).setPreferredWidth(168);
		transinfoTable.getColumnModel().getColumn(2).setPreferredWidth(141);
		transinfoTable.getColumnModel().getColumn(3).setPreferredWidth(152);
		transinfoTable.getColumnModel().getColumn(4).setPreferredWidth(151);
		transinfoTable.getColumnModel().getColumn(5).setPreferredWidth(164);
		scrollPane.setViewportView(transinfoTable);
		contentPane.setLayout(gl_contentPane);

		this.fillTable(new Transinfo());

//		this.setExtendedState(JFrame.MAXIMIZED_BOTH);//设置JFRAME最大化
	}

	private void RefreshActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		this.resetValue();
		this.fillTable(new Transinfo());
		JOptionPane.showMessageDialog(null, "刷新成功");
	}

	/**
	 * 删除事件处理
	 * 
	 * @param e
	 */
	private void TransinfoDeleteActionEvent(ActionEvent e) {
		// TODO 自动生成的方法存根
		String carNum = CardNoTxt.getText();
		if (StringUtil.isEmpty(carNum)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的记录");
			return;
		}
		int n = JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
		if (n == 0) {
			Connection con = null;
			try {
				con = dbUtil.getCon();
				int deleteNum = transinfoDao.delete(con, Long.parseLong(carNum));
				if (deleteNum == 1) {
					JOptionPane.showMessageDialog(null, "删除成功");
					this.resetValue();
					this.fillTable(new Transinfo());
				} else {
					JOptionPane.showMessageDialog(null, "删除失败");

				}
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "删除失败");
			} finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
			}

		}

	}

	/**
	 * 修改事件处理
	 * 
	 * @param e
	 */
	private void TransinfoUpdateActionEvent(ActionEvent e) {
		// TODO 自动生成的方法存根
		String carNum = CardNoTxt.getText();
		String busLine = BusLineTxt.getText();
		String boardingStation = BoardingStationTxt.getText();
		String offStation = OffStationTxt.getText();
		if (StringUtil.isEmpty(busLine) || StringUtil.isEmpty(boardingStation) || StringUtil.isEmpty(offStation)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录");
			return;
		}
//		Transinfo transinfo = new Transinfo(Integer.parseInt(carNum),busLine,Integer.parseInt(boardingStation), Integer.parseInt(offStation));
//		Transinfo transinfo = new Transinfo(new BigInteger(carNum),busLine,Integer.parseInt(boardingStation), Integer.parseInt(offStation));
		Transinfo transinfo = new Transinfo(Long.parseLong(carNum), busLine, Integer.parseInt(boardingStation),
				Integer.parseInt(offStation));

		Connection con = null;
		try {
			con = dbUtil.getCon();
			int modifyNum = transinfoDao.update(con, transinfo);
			if (modifyNum == 1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.resetValue();
				this.fillTable(new Transinfo());
			} else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 表格行点击事件处理
	 * 
	 * @param e
	 */
	private void transinfoTableMousePressed(MouseEvent evt) {
		// TODO 自动生成的方法存根
		int row = transinfoTable.getSelectedRow();
		CardNoTxt.setText((String) transinfoTable.getValueAt(row, 0));
		BoardingTimeTxt.setText((String) transinfoTable.getValueAt(row, 1));
		BusLineTxt.setText((String) transinfoTable.getValueAt(row, 2));
		BoardingStationTxt.setText((String) transinfoTable.getValueAt(row, 3));
		OffStationTxt.setText((String) transinfoTable.getValueAt(row, 4));
		TransAmountTxt.setText((String) transinfoTable.getValueAt(row, 5));

	}

	/**
	 * 初始化表格
	 * 
	 * @param transinfo
	 */
	private void fillTable(Transinfo transinfo) {
		DefaultTableModel dtm = (DefaultTableModel) transinfoTable.getModel();
		dtm.setRowCount(0);// 设置成0行 清空表格
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.list(con, transinfo);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getString("CardNo"));
				v.add(rs.getString("BoardingTime"));
				v.add(rs.getString("BusLine"));
				v.add(rs.getString("BoardingStation"));
				v.add(rs.getString("OffStation"));
				v.add(rs.getString("TransAmount"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.CardNoTxt.setText("");
		this.BoardingTimeTxt.setText("");
		this.BusLineTxt.setText("");
		this.BoardingStationTxt.setText("");
		this.OffStationTxt.setText("");
		this.TransAmountTxt.setText("");

	}
}

package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.jdbcsy.dao.TransinfoDao;
import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.Dbutil;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SelectPassengersAmountDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable PassengerAmounttable;
	JComboBox BusLineComboBox ;
	
	private Dbutil dbUtil = new Dbutil();
	private TransinfoDao transinfoDao = new TransinfoDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SelectPassengersAmountDialog dialog = new SelectPassengersAmountDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SelectPassengersAmountDialog() {
		setResizable(false);
		setTitle("\u67E5\u8BE2\u516C\u4EA4\u7EBF\u8DEF\u8F66\u7AD9\u4FE1\u606F");
		setBounds(100, 100, 406, 479);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton button = new JButton("\u67E5\u8BE2");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectStationActionPerformed(e);
			}
		});
		button.setFont(new Font("微软雅黑 Light", Font.PLAIN, 17));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(43)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(button)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(scrollPane, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
							.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)))
					.addContainerGap(226, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
					.addGap(45)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
					.addGap(32)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
					.addComponent(button)
					.addGap(22))
		);
		
		JLabel label = new JLabel("\u516C\u4EA4\u7EBF\u8DEF\uFF1A");
		label.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		BusLineComboBox = new JComboBox();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(29)
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(BusLineComboBox, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(53, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(BusLineComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(11, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		{
			PassengerAmounttable = new JTable();
			PassengerAmounttable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"BusLine", "PassengerAmount"
				}
			));
			scrollPane.setViewportView(PassengerAmounttable);
		}
		contentPanel.setLayout(gl_contentPanel);
		
		setLocationRelativeTo(null); //设置对话框相对屏幕居中
		
		this.fillBusLine("search");
		this.fillTable(new Transinfo());
	}
	
	/**
	 * 公交线路查询事件处理
	 * @param e
	 */
	private void selectStationActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		String busline = (String)this.BusLineComboBox.getSelectedItem();
		Transinfo transinfo = new Transinfo(busline);
		this.fillTable(transinfo);
		
	}

	/**
	 * 初始化下拉框
	 * @param type
	 */
	private void fillBusLine(String type) {
		Connection con = null;
		Transinfo transinfo = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.listPassengerAmount(con, new Transinfo());
			if("search".equals(type)) {
				transinfo = new Transinfo();
				transinfo.setBusLine("请选择...");
				this.BusLineComboBox.addItem(transinfo.getBusLine());
				
			}
			while(rs.next()) {
				transinfo = new Transinfo();
				transinfo.setBusLine(rs.getString("busLine"));
//				transinfo.setBoardingStation(rs.getInt("Station"));
				if("search".equals(type)) {
					this.BusLineComboBox.addItem(transinfo.getBusLine());
				}else if("modify".equals(type)){
					
				}
			}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 初始化表格数据
	 * @param transinfo
	 */
	private void fillTable(Transinfo transinfo) {

		DefaultTableModel dtm = (DefaultTableModel) PassengerAmounttable.getModel();
		dtm.setRowCount(0);// 设置成0行 清空表格
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.listPassengerAmount(con, transinfo);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getString("BusLine"));
				v.add(rs.getInt("passengersAmount"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	
	}
}

package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.jdbcsy.dao.TransinfoDao;
import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.Dbutil;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SelectStationDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable BusStationtable;
	JComboBox BusLineComboBox ;
	
	private Dbutil dbUtil = new Dbutil();
	private TransinfoDao transinfoDao = new TransinfoDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SelectStationDialog dialog = new SelectStationDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SelectStationDialog() {
		setResizable(false);
		setTitle("\u67E5\u8BE2\u516C\u4EA4\u7EBF\u8DEF\u8F66\u7AD9\u4FE1\u606F");
		setBounds(100, 100, 406, 479);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton button = new JButton("\u67E5\u8BE2");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectStationActionPerformed(e);
			}
		});
		button.setFont(new Font("微软雅黑 Light", Font.PLAIN, 17));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(43)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(button)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(scrollPane, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
							.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)))
					.addContainerGap(226, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
					.addGap(45)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
					.addGap(32)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
					.addComponent(button)
					.addGap(22))
		);
		
		JLabel label = new JLabel("\u516C\u4EA4\u7EBF\u8DEF\uFF1A");
		label.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		BusLineComboBox = new JComboBox();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(29)
					.addComponent(label)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(BusLineComboBox, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(53, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(BusLineComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(11, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		{
			BusStationtable = new JTable();
			BusStationtable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"BusLine","BusStation"
				}
			));
			scrollPane.setViewportView(BusStationtable);
		}
		contentPanel.setLayout(gl_contentPanel);
		
		setLocationRelativeTo(null); //设置对话框相对屏幕居中
		
		this.fillBusLine("search");
		this.fillTable(new Transinfo());
	}
	
	/**
	 * 公交线路查询事件处理
	 * @param e
	 */
	private void selectStationActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		String busline = (String)this.BusLineComboBox.getSelectedItem();
		Transinfo transinfo = new Transinfo(busline);
		this.fillTable(transinfo);
		
	}

	/**
	 * 初始化下拉框
	 * @param type
	 */
	private void fillBusLine(String type) {
		Connection con = null;
		Transinfo transinfo = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.listStation(con, new Transinfo(),0);
			if("search".equals(type)) {
				transinfo = new Transinfo();
				transinfo.setBusLine("请选择...");
				this.BusLineComboBox.addItem(transinfo.getBusLine());
				
			}
			while(rs.next()) {
				transinfo = new Transinfo();
				transinfo.setBusLine(rs.getString("busLine"));
				transinfo.setBoardingStation(rs.getInt("Station"));
				if("search".equals(type)) {
					this.BusLineComboBox.addItem(transinfo.getBusLine());
				}else if("modify".equals(type)){
					
				}
			}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 初始化表格数据
	 * @param transinfo
	 */
	private void fillTable(Transinfo transinfo) {

		DefaultTableModel dtm = (DefaultTableModel) BusStationtable.getModel();
		dtm.setRowCount(0);// 设置成0行 清空表格
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.listStation(con, transinfo,1);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getString("busLine"));
				v.add(rs.getInt("Station"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	
	}
}
package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.jdbcsy.dao.TransinfoDao;
import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.Dbutil;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class SelectTimePassengerDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable PassengerAmounttable;
	
	private Dbutil dbUtil = new Dbutil();
	private TransinfoDao transinfoDao = new TransinfoDao();
	private JTextField StartTimeTxt;
	private JTextField EndTimeTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SelectTimePassengerDialog dialog = new SelectTimePassengerDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SelectTimePassengerDialog() {
		setResizable(false);
		setTitle("\u67E5\u8BE2\u67D0\u4E2A\u65F6\u6BB5\u7684\u5BA2\u6D41\u7EDF\u8BA1\u4FE1\u606F");
		setBounds(100, 100, 406, 479);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton button = new JButton("\u67E5\u8BE2");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectTimePassengerActionPerformed(e);
			}
		});
		button.setFont(new Font("微软雅黑 Light", Font.PLAIN, 17));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(43)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(scrollPane, Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
						.addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
						.addComponent(button, Alignment.TRAILING))
					.addContainerGap(53, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addGap(45)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
					.addGap(58)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
					.addComponent(button)
					.addGap(22))
		);
		
		JLabel label = new JLabel("\u8D77\u59CB\u65F6\u95F4");
		label.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		StartTimeTxt = new JTextField();
		StartTimeTxt.setColumns(10);
		
		JLabel label_1 = new JLabel("\u622A\u6B62\u65F6\u95F4");
		label_1.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		EndTimeTxt = new JTextField();
		EndTimeTxt.setColumns(10);
		
		JLabel label_2 = new JLabel("\u586B\u5199\u683C\u5F0F\uFF1A2016-04-02 07:42:00");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(29)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(label)
								.addComponent(label_1))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(EndTimeTxt)
								.addComponent(StartTimeTxt, GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(73)
							.addComponent(label_2)))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(StartTimeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(EndTimeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(13)
					.addComponent(label_2))
		);
		panel.setLayout(gl_panel);
		{
			PassengerAmounttable = new JTable();
			PassengerAmounttable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"PassengerAmount"
				}
			));
			PassengerAmounttable.getColumnModel().getColumn(0).setPreferredWidth(217);
			scrollPane.setViewportView(PassengerAmounttable);
		}
		contentPanel.setLayout(gl_contentPanel);
		
		setLocationRelativeTo(null); //设置对话框相对屏幕居中
		

	}
	


	/**
	 * 客流统计信息查询事件处理
	 * @param e
	 */
	private void selectTimePassengerActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		String startime = StartTimeTxt.getText();
		String endtime = EndTimeTxt.getText();
		Transinfo transinfo = new Transinfo(startime, endtime);
		this.fillTable(transinfo);
	}

	/**
	 * 初始化表格数据
	 * @param transinfo
	 */
	private void fillTable(Transinfo transinfo) {

		DefaultTableModel dtm = (DefaultTableModel) PassengerAmounttable.getModel();
		dtm.setRowCount(0);// 设置成0行 清空表格
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = transinfoDao.listTimePassengerAmount(con, transinfo);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getInt("amount"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

	
	}
}

//view
package com.jdbcsy.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jdbcsy.dao.TransinfoDao;
import com.jdbcsy.model.Transinfo;
import com.jdbcsy.util.Dbutil;
import com.jdbcsy.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class transinfoAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField CardNotextField;
	private JTextField BoardingTimetextField;
	private JTextField BusLinetextField;
	private JTextField BoardingStationtextField;
	private JTextField OffStationtextField;
	private JTextField TransAmounttextField;
	
	private Dbutil dbUtil = new Dbutil();
	private TransinfoDao transinfoDao = new TransinfoDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			transinfoAddDialog dialog = new transinfoAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public transinfoAddDialog() {
		setTitle("\u8868\u5355\u6DFB\u52A0");
		setResizable(false);
		setBounds(100, 100, 438, 383);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("CardNo:");
		lblNewLabel.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		JLabel lblBoardingtime = new JLabel("BoardingTime:");
		lblBoardingtime.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		JLabel lblBusline = new JLabel("BusLine:");
		lblBusline.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		JLabel lblBoardingstation = new JLabel("BoardingStation:");
		lblBoardingstation.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		JLabel lblOffstation = new JLabel("OffStation:");
		lblOffstation.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		JLabel lblTransamount = new JLabel("TransAmount:");
		lblTransamount.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
		
		CardNotextField = new JTextField();
		CardNotextField.setColumns(10);
		
		BoardingTimetextField = new JTextField();
		BoardingTimetextField.setColumns(10);
		
		BusLinetextField = new JTextField();
		BusLinetextField.setColumns(10);
		
		BoardingStationtextField = new JTextField();
		BoardingStationtextField.setColumns(10);
		
		OffStationtextField = new JTextField();
		OffStationtextField.setColumns(10);
		
		TransAmounttextField = new JTextField();
		TransAmounttextField.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblBoardingtime)
								.addComponent(lblBusline)
								.addComponent(lblNewLabel))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(CardNotextField, 115, 115, 115)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
									.addComponent(BusLinetextField)
									.addComponent(BoardingTimetextField, GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblBoardingstation)
								.addComponent(lblOffstation)
								.addComponent(lblTransamount))
							.addPreferredGap(ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(TransAmounttextField)
								.addComponent(OffStationtextField)
								.addComponent(BoardingStationtextField, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))))
					.addContainerGap(160, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(40)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(CardNotextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBoardingtime)
						.addComponent(BoardingTimetextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBusline)
						.addComponent(BusLinetextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBoardingstation)
						.addComponent(BoardingStationtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOffstation)
						.addComponent(OffStationtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblTransamount)
						.addComponent(TransAmounttextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(156, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton AddButton = new JButton("\u6DFB\u52A0");
				AddButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						transinfoAddActionPerformed(e);
					}
				});
				AddButton.setFont(new Font("微软雅黑 Light", Font.PLAIN, 20));
				AddButton.setActionCommand("添加");
				buttonPane.add(AddButton);
				getRootPane().setDefaultButton(AddButton);
			}
			{
				JButton ResetButton = new JButton("\u91CD\u7F6E");
				ResetButton.setFont(new Font("微软雅黑 Light", Font.PLAIN, 20));
				ResetButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						resetValueActionPerformed(e);
					}
				});
				ResetButton.setActionCommand("重置");
				buttonPane.add(ResetButton);
			}
		}
		setLocationRelativeTo(null); //设置对话框相对屏幕居中
	}
	
	/**
	 * 添加事件处理
	 * @param e
	 */
	private void transinfoAddActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		String carNum = this.CardNotextField.getText();
		String boardingTime = this.BoardingTimetextField.getText();
		String busLine = this.BusLinetextField.getText();
		String boardingStation = this.BoardingStationtextField.getText();
		String offStation = this.OffStationtextField.getText();
		String transAmount = this.TransAmounttextField.getText();
		if(StringUtil.isEmpty(carNum)||StringUtil.isEmpty(boardingTime)||StringUtil.isEmpty(busLine)
				||StringUtil.isEmpty(boardingStation)||StringUtil.isEmpty(offStation)||StringUtil.isEmpty(transAmount) == true) {
			JOptionPane.showMessageDialog(null, "不能为空");
			return;
		}
		Transinfo transinfo = new Transinfo(Long.parseLong(carNum), boardingTime, busLine, Integer.parseInt(boardingStation), Integer.parseInt(offStation), Double.parseDouble(transAmount));
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int n = transinfoDao.add(con, transinfo);
			if(n == 1) {
				JOptionPane.showMessageDialog(null, "添加成功");
				resetValue();
			}else {
				JOptionPane.showMessageDialog(null, "添加失败");

			}
			
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "添加失败");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		
	}

	/**
	 * 重置事件处理
	 * @param evt
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		this.resetValue();
	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.CardNotextField.setText("");
		this.BoardingTimetextField.setText("");
		this.BusLinetextField.setText("");
		this.BoardingStationtextField.setText("");
		this.OffStationtextField.setText("");
		this.TransAmounttextField.setText("");
	}
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的ATM柜员机模拟程序的Java代码示例,包括连接MySQL数据库和GUI界面的实现。这里使用了Java Swing框架实现GUI界面,以及JDBC驱动连接MySQL数据库。 代码中的数据库名为"atm",包含两个表:"account"和"transaction"。"account"表包含账户信息,包括账户号码、密码、余额等字段,"transaction"表包含交易记录,包括交易时间、账户号码、交易类型、交易金额等字段。 注意:以下代码仅供参考,具体实现需要根据实际需求进行修改和完善。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class ATM extends JFrame implements ActionListener { // GUI components private JLabel accountLabel, passwordLabel, balanceLabel; private JTextField accountField; private JPasswordField passwordField; private JButton loginButton, depositButton, withdrawButton, transferButton; private JPanel mainPanel, buttonPanel; // database connection private Connection conn; private Statement stmt; // current user account info private String currentUser; private double currentBalance; public ATM() { // initialize GUI components accountLabel = new JLabel("Account:"); passwordLabel = new JLabel("Password:"); balanceLabel = new JLabel("Balance: $0.00"); accountField = new JTextField(10); passwordField = new JPasswordField(10); loginButton = new JButton("Login"); depositButton = new JButton("Deposit"); withdrawButton = new JButton("Withdraw"); transferButton = new JButton("Transfer"); mainPanel = new JPanel(new GridLayout(3, 2)); buttonPanel = new JPanel(new GridLayout(1, 4)); // add GUI components to main panel mainPanel.add(accountLabel); mainPanel.add(accountField); mainPanel.add(passwordLabel); mainPanel.add(passwordField); mainPanel.add(balanceLabel); mainPanel.add(new JLabel()); // placeholder // add GUI components to button panel buttonPanel.add(loginButton); buttonPanel.add(depositButton); buttonPanel.add(withdrawButton); buttonPanel.add(transferButton); // add action listeners to buttons loginButton.addActionListener(this); depositButton.addActionListener(this); withdrawButton.addActionListener(this); transferButton.addActionListener(this); // add panels to frame add(mainPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); // set frame properties setTitle("ATM"); setSize(300, 150); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // connect to database try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/atm", "root", ""); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to connect to database."); } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { // login button pressed String account = accountField.getText(); String password = new String(passwordField.getPassword()); try { // check if account and password are valid ResultSet rs = stmt.executeQuery("SELECT * FROM account WHERE account_no = '" + account + "' AND password = '" + password + "'"); if (rs.next()) { // login successful currentUser = account; currentBalance = rs.getDouble("balance"); balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance)); } else { // login failed JOptionPane.showMessageDialog(this, "Invalid account or password."); } rs.close(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to retrieve account information."); } } else if (e.getSource() == depositButton) { // deposit button pressed if (currentUser == null) { // not logged in JOptionPane.showMessageDialog(this, "Please log in first."); return; } String input = JOptionPane.showInputDialog(this, "Enter deposit amount:"); if (input == null || input.isEmpty()) { // cancel button pressed return; } try { double amount = Double.parseDouble(input); if (amount <= 0) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } else { // update account balance and transaction record currentBalance += amount; stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'"); stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Deposit', " + amount + ")"); balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance)); JOptionPane.showMessageDialog(this, "Deposit successful."); } } catch (NumberFormatException ex) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to update account information."); } } else if (e.getSource() == withdrawButton) { // withdraw button pressed if (currentUser == null) { // not logged in JOptionPane.showMessageDialog(this, "Please log in first."); return; } String input = JOptionPane.showInputDialog(this, "Enter withdrawal amount:"); if (input == null || input.isEmpty()) { // cancel button pressed return; } try { double amount = Double.parseDouble(input); if (amount <= 0 || amount > currentBalance) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } else { // update account balance and transaction record currentBalance -= amount; stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'"); stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Withdrawal', " + amount + ")"); balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance)); JOptionPane.showMessageDialog(this, "Withdrawal successful."); } } catch (NumberFormatException ex) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to update account information."); } } else if (e.getSource() == transferButton) { // transfer button pressed if (currentUser == null) { // not logged in JOptionPane.showMessageDialog(this, "Please log in first."); return; } String input = JOptionPane.showInputDialog(this, "Enter transfer amount:"); if (input == null || input.isEmpty()) { // cancel button pressed return; } try { double amount = Double.parseDouble(input); if (amount <= 0 || amount > currentBalance) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } else { String targetAccount = JOptionPane.showInputDialog(this, "Enter target account number:"); if (targetAccount == null || targetAccount.isEmpty()) { // cancel button pressed return; } ResultSet rs = stmt.executeQuery("SELECT * FROM account WHERE account_no = '" + targetAccount + "'"); if (rs.next()) { // target account exists double targetBalance = rs.getDouble("balance"); targetBalance += amount; currentBalance -= amount; stmt.executeUpdate("UPDATE account SET balance = " + currentBalance + " WHERE account_no = '" + currentUser + "'"); stmt.executeUpdate("UPDATE account SET balance = " + targetBalance + " WHERE account_no = '" + targetAccount + "'"); stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + currentUser + "', 'Transfer', " + amount + ")"); stmt.executeUpdate("INSERT INTO transaction (account_no, type, amount) VALUES ('" + targetAccount + "', 'Transfer', " + amount + ")"); balanceLabel.setText("Balance: $" + String.format("%.2f", currentBalance)); JOptionPane.showMessageDialog(this, "Transfer successful."); } else { // target account not found JOptionPane.showMessageDialog(this, "Target account not found."); } rs.close(); } } catch (NumberFormatException ex) { // invalid input JOptionPane.showMessageDialog(this, "Invalid amount."); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to update account information."); } } } public static void main(String[] args) { new ATM(); } } ``` 希望以上代码对您有所帮助。如有任何疑问或需求,请随时与我交流。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值