大一结束了,这学期的课程设计,老师要求写一个火车站火车站大屏幕火车信息显示系统,写了一个多星期,做了一个小程序,大体实现老师的要求了,就是有一些简陋和BUG。
题目要求如下
管理员负责设置经过济南西站的各趟列车的信息,当有列车晚点时,要及时进行设置晚点时间。
系统运行时屏幕循环显示即将到站的列车信息。
屏幕显示的列车信息及滚动显示方式请去火车站实地调研。
话不多说,直接上代码!
//sql类
import java.sql.*;
public class sqll {
private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=列车信息";
private static final String USER="sa";
private static final String PASSWORD="111111";
private static Connection con=null;
private static Statement stmt =null;
private static ResultSet rs =null;
String a,b,c,d,e,f;
public String aa(){
return a;
}
public String bb(){
return b;
}
public String cc(){
return c;
}
public String dd(){
return d;
}
public String ee(){
return e;
}
public String ff(){
return f;
}
public void first() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println("数据库连接成功");
}
catch(Exception e) {
System.out.println("数据库连接失败\n" + e.toString());
}
}
public void chaxun(String name) throws SQLException{
stmt =con.createStatement();
rs = stmt.executeQuery("select * from 列车 where 车次="+"'"+name+"'");
// while(rs.next()){
// System.out.println(rs.getString("车次")+","+rs.getString("终点站")+","+rs.getTime("到点")+","+rs.getTime("开点")+","+rs.getString("站台")+","+rs.getString("状态"));
// }
con.close();
stmt.close();
rs.close();
}
public void xinjian(String a,String b,String c,String d,String e,String f) throws SQLException{
stmt =con.createStatement();
stmt.executeUpdate("insert into 列车 values "+"('"+a+"','"+b+"','"+c+"','"+d+"','"+e+"','"+f+"')");
stmt.close();
con.close();
}
public void shanchu(String a) throws SQLException{
stmt =con.createStatement();
stmt.executeUpdate("delete from 列车 where 车次= '"+a+"'");
stmt.close();
con.close();
}
public void xiugai1(String name)throws SQLException{
stmt =con.createStatement();
rs = stmt.executeQuery("select * from 列车 where 车次='"+name+"'");
while(rs.next()){
a=rs.getString("终点站");b=rs.getString("到点");c=rs.getString("开点");d=rs.getString("站台");e=rs.getString("状态");f=rs.getString("车次");
}
con.close();
stmt.close();
rs.close();
}
public void xiugai2(String a,String b,String c,String d,String e,String f) throws SQLException{
stmt =con.createStatement();
stmt.executeUpdate("update 列车 set 终点站='"+b+"',开点='"+c+"',到点='"+d+"',站台='"+e+"',状态='"+f+"' where 车次='"+a+"'");
stmt.close();
con.close();
}
}
//列车管理窗口
import java.awt.BorderLayout;
public class guanli extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_9;
private JTable table;
private JTextField textField_8;
private JTextField textField_10;
private JTextField textField_11;
private JTextField textField_12;
private JTextField textField_13;
private JTextField textField_14;
/**
* Launch the application.
*/
public static void gl() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
guanli frame = new guanli();
frame.guan();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public void guan() {
setTitle("\u5217\u8F66\u4FE1\u606F\u7BA1\u7406\u9762\u677F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1061, 667);
contentPane = new JPanel();
contentPane.setBackground(new Color(224, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u65B0\u5EFA\u5217\u8F66\u4FE1\u606F\uFF1A");
label.setFont(new Font("黑体", Font.PLAIN, 24));
label.setBounds(14, 13, 178, 37);
contentPane.add(label);
JLabel label_1 = new JLabel("\u8F66\u6B21");
label_1.setFont(new Font("宋体", Font.PLAIN, 20));
label_1.setBounds(14, 63, 49, 18);
contentPane.add(label_1);
textField = new JTextField();
textField.setBounds(85, 62, 206, 24);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_2 = new JLabel("\u7EC8\u70B9\u7AD9");
label_2.setFont(new Font("宋体", Font.PLAIN, 20));
label_2.setBounds(14, 125, 72, 18);
contentPane.add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(85, 124, 206, 24);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel label_3 = new JLabel("\u5230\u70B9");
label_3.setFont(new Font("宋体", Font.PLAIN, 20));
label_3.setBounds(14, 192, 72, 18);
contentPane.add(label_3);
textField_2 = new JTextField();
textField_2.setBounds(85, 191, 206, 24);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel label_4 = new JLabel("\u5F00\u70B9");
label_4.setFont(new Font("宋体", Font.PLAIN, 20));
label_4.setBounds(14, 264, 72, 18);
contentPane.add(label_4);
textField_3 = new JTextField();
textField_3.setBounds(85, 258, 206, 24);
contentPane.add(textField_3);
textField_3.setColumns(10);
JLabel label_5 = new JLabel("\u7AD9\u53F0");
label_5.setFont(new Font("宋体", Font.PLAIN, 20));
label_5.setBounds(14, 338, 72, 18);
contentPane.add(label_5);
textField_4 = new JTextField();
textField_4.setBounds(85, 337, 206, 24);
contentPane.add(textField_4);
textField_4.setColumns(10);
JLabel label_6 = new JLabel("\u72B6\u6001");
label_6.setFont(new Font("宋体", Font.PLAIN, 20));
label_6.setBounds(14, 413, 72, 18);
contentPane.add(label_6);
final JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u5019\u8F66", "\u6B63\u5728\u68C0\u7968", "\u505C\u6B62\u68C0\u7968", "\u665A\u70B9"}));
comboBox.setBounds(85, 412, 206, 24);
contentPane.add(comboBox);
JButton button = new JButton("\u6DFB\u52A0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
// System.out.println(dat);
Date date = null;
Date datt = null;
try {
date = df.parse((String)textField_3.getText());
datt = df.parse((String)textField_2.getText());
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println(obj[q][2]);
long tim=(datt.getTime()-date.getTime());
if(tim>0){textField_5.setText("异常!列车时间错误");}
else{
String a=textField.getText();
String b=textField_1.getText();
String c=textField_2.getText();
String d=textField_3.getText();
String e=textField_4.getText();
String f= comboBox.getSelectedItem().toString();
sqll x=new sqll();
x.first();
try {
x.xinjian(a, b, c, d, e, f);
textField_5.setText("列车添加成功!");
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
textField_5.setText("列车添加失败!");
}}
}
});
button.setFont(new Font("楷体", Font.PLAIN, 29));
button.setBounds(50, 482, 200, 50);
contentPane.add(button);
textField_5 = new JTextField();
textField_5.setFont(new Font("宋体", Font.PLAIN, 20));
textField_5.setBackground(new Color(224, 255, 255));
textField_5.setBounds(14, 574, 277, 24);
contentPane.add(textField_5);
textField_5.setColumns(10);
JLabel label_7 = new JLabel("\u67E5\u8BE2\u5217\u8F66\u4FE1\u606F\uFF1A");
label_7.setFont(new Font("黑体", Font.PLAIN, 24));
label_7.setBounds(371, 19, 183, 24);
contentPane.add(label_7);
JLabel label_8 = new JLabel("\u88AB\u67E5\u8BE2\u5217\u8F66\u8F66\u6B21");
label_8.setFont(new Font("宋体", Font.PLAIN, 20));
label_8.setBounds(381, 62, 151, 19);
contentPane.add(label_8);
textField_6 = new JTextField();
textField_6.setBounds(569, 62, 246, 24);
contentPane.add(textField_6);
textField_6.setColumns(10);
JButton btnNewButton = new JButton("\u67E5\u8BE2");
final String[][] str1=new String[10][6];
btnNewButton.addActionListener(new ActionListener() {
int q=0;
public void actionPerformed(ActionEvent arg0) {
sqll a=new sqll();
a.first();
try {
a.chaxun(textField_6.getText());
a.first();
a.xiugai1(textField_6.getText());
str1[q][0]=a.ff();
str1[q][1]=a.aa();
str1[q][2]=a.bb();
str1[q][3]=a.cc();
str1[q][4]=a.dd();
str1[q][5]=a.ee();
if(a.ff()!=null)
q++;
if(q==10){q=0;}
// 测试
// for(int d=0;d<6;d++){
// System.out.print(str1[0][d]);
// }
table.setModel(new DefaultTableModel(
str1 ,new String[] {"\u8F66\u6B21", "\u7EC8\u70B9\u7AD9", "\u5F00\u70B9", "\u5230\u70B9", "\u7AD9\u53F0", "\u72B6\u6001"}
));
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
btnNewButton.setBounds(876, 61, 113, 27);
contentPane.add(btnNewButton);
JLabel label_9 = new JLabel("\u4FEE\u6539\u5217\u8F66\u4FE1\u606F\uFF1A");
label_9.setFont(new Font("黑体", Font.PLAIN, 24));
label_9.setBounds(328, 227, 168, 24);
contentPane.add(label_9);
JLabel label_10 = new JLabel("\u88AB\u4FEE\u6539\u5217\u8F66\u8F66\u6B21");
label_10.setFont(new Font("宋体", Font.PLAIN, 20));
label_10.setBounds(328, 261, 156, 24);
contentPane.add(label_10);
final JComboBox comboBox_1 = new JComboBox();
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"\u5019\u8F66", "\u6B63\u5728\u68C0\u7968", "\u505C\u6B62\u68C0\u7968", "\u665A\u70B9"}));
comboBox_1.setBounds(546, 448, 252, 24);
contentPane.add(comboBox_1);
textField_7 = new JTextField();
textField_7.setBounds(546, 264, 252, 24);
contentPane.add(textField_7);
textField_7.setColumns(10);
JButton button_1 = new JButton("\u67E5\u627E");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sqll a=new sqll();
a.first();
try {
a.xiugai1(textField_7.getText());
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
textField_8.setText(a.aa());
textField_10.setText(a.bb());
textField_11.setText(a.cc());
textField_12.setText(a.dd());
String xx=a.ff();
if(xx.equals("候车")){comboBox_1.setSelectedIndex(0); }
if(xx.equals("正在检票")){comboBox_1.setSelectedIndex(1); }
if(xx.equals("停止检票")){comboBox_1.setSelectedIndex(2); }
if(xx.equals("晚点")){comboBox_1.setSelectedIndex(3); }
}
});
button_1.setBounds(870, 263, 134, 24);
contentPane.add(button_1);
JLabel label_11 = new JLabel("\u4FEE\u6539\u5217\u8F66\u72B6\u6001");
label_11.setFont(new Font("宋体", Font.PLAIN, 20));
label_11.setBounds(328, 449, 151, 18);
contentPane.add(label_11);
JButton btnNewButton_1 = new JButton("\u4FEE\u6539");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String q1=textField_7.getText();
String q2=textField_8.getText();
String q3=textField_10.getText();
String q4=textField_11.getText();
String q5=textField_12.getText();
String q6 = comboBox_1.getSelectedItem().toString();
sqll a=new sqll();
a.first();
try {
a.xiugai2(q1, q2, q3, q4,q5, q6);
textField_13.setText("修改成功!");
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 24));
btnNewButton_1.setBounds(876, 338, 128, 129);
contentPane.add(btnNewButton_1);
JLabel label_14 = new JLabel("\u5220\u9664\u5217\u8F66\u4FE1\u606F\uFF1A");
label_14.setBackground(new Color(0, 255, 255));
label_14.setFont(new Font("黑体", Font.PLAIN, 24));
label_14.setBounds(366, 495, 176, 27);
contentPane.add(label_14);
JLabel label_15 = new JLabel("\u88AB\u5220\u9664\u5217\u8F66\u8F66\u6B21");
label_15.setFont(new Font("宋体", Font.PLAIN, 20));
label_15.setBounds(366, 539, 156, 18);
contentPane.add(label_15);
textField_9 = new JTextField();
textField_9.setBounds(569, 533, 246, 24);
contentPane.add(textField_9);
textField_9.setColumns(10);
JButton btnNewButton_2 = new JButton("\u5220\u9664");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String a=textField_9.getText();
sqll x=new sqll();
x.first();
try {
x.shanchu(a);
textField_14.setText("删除成功!");
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
btnNewButton_2.setBounds(876, 530, 113, 27);
contentPane.add(btnNewButton_2);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.GREEN);
desktopPane.setBounds(315, 0, 3, 620);
contentPane.add(desktopPane);
JDesktopPane desktopPane_1 = new JDesktopPane();
desktopPane_1.setBackground(Color.GREEN);
desktopPane_1.setBounds(315, 214, 714, 3);
contentPane.add(desktopPane_1);
JDesktopPane desktopPane_2 = new JDesktopPane();
desktopPane_2.setBackground(Color.GREEN);
desktopPane_2.setBounds(315, 482, 728, 3);
contentPane.add(desktopPane_2);
JButton button_2 = new JButton("\u8BBE\u7F6E\u80CC\u666F\u989C\u8272");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color color = JColorChooser.showDialog(contentPane, "选取颜色", null);
if (color == null) {
return;
}
contentPane.setBackground(color);
}
});
button_2.setForeground(Color.BLACK);
button_2.setBackground(Color.PINK);
button_2.setBounds(593, 580, 163, 40);
contentPane.add(button_2);
JDesktopPane desktopPane_3 = new JDesktopPane();
desktopPane_3.setBackground(Color.GREEN);
desktopPane_3.setBounds(317, 567, 726, 3);
contentPane.add(desktopPane_3);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(366, 110, 645, 94);
contentPane.add(scrollPane);
table = new JTable();
String[] str={"\u8F66\u6B21", "\u7EC8\u70B9\u7AD9", "\u5230\u70B9", "\u5F00\u70B9", "\u7AD9\u53F0", "\u72B6\u6001"};
table.setModel(new DefaultTableModel(
str1,str
));
scrollPane.setViewportView(table);
JLabel label_12 = new JLabel("\u4FEE\u6539\u5217\u8F66\u7EC8\u70B9\u7AD9");
label_12.setFont(new Font("宋体", Font.PLAIN, 20));
label_12.setBounds(328, 284, 200, 50);
contentPane.add(label_12);
textField_8 = new JTextField();
textField_8.setBounds(546, 301, 252, 21);
contentPane.add(textField_8);
textField_8.setColumns(10);
JLabel label_13 = new JLabel("\u4FEE\u6539\u5217\u8F66\u5230\u70B9");
label_13.setFont(new Font("宋体", Font.PLAIN, 20));
label_13.setBounds(328, 322, 200, 50);
contentPane.add(label_13);
textField_10 = new JTextField();
textField_10.setBounds(546, 339, 252, 21);
contentPane.add(textField_10);
textField_10.setColumns(10);
JLabel label_16 = new JLabel("\u4FEE\u6539\u5217\u8F66\u5F00\u70B9");
label_16.setFont(new Font("宋体", Font.PLAIN, 20));
label_16.setBounds(328, 359, 200, 50);
contentPane.add(label_16);
textField_11 = new JTextField();
textField_11.setBounds(546, 376, 252, 21);
contentPane.add(textField_11);
textField_11.setColumns(10);
JLabel label_17 = new JLabel("\u4FEE\u6539\u5217\u8F66\u7AD9\u53F0");
label_17.setFont(new Font("宋体", Font.PLAIN, 20));
label_17.setBounds(328, 397, 200, 50);
contentPane.add(label_17);
textField_12 = new JTextField();
textField_12.setBounds(546, 414, 252, 21);
contentPane.add(textField_12);
textField_12.setColumns(10);
textField_13 = new JTextField();
textField_13.setBackground(new Color(224, 255, 255));
textField_13.setFont(new Font("宋体", Font.PLAIN, 20));
textField_13.setBounds(546, 233, 252, 21);
contentPane.add(textField_13);
textField_13.setColumns(10);
textField_14 = new JTextField();
textField_14.setFont(new Font("宋体", Font.PLAIN, 20));
textField_14.setBackground(new Color(224, 255, 255));
textField_14.setBounds(569, 502, 246, 21);
contentPane.add(textField_14);
textField_14.setColumns(10);
}
}
//屏幕刷新方法
import java.sql.SQLException;
import java.util.Timer;
public class shuaxin {
public shuaxin() throws ClassNotFoundException, SQLException {
Timer time = new Timer();
time.schedule(new xianshi(), 0, 3000);
}
}
//屏幕显示类
import java.sql.*;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.util.TimerTask;
public class xianshi extends TimerTask{
private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=列车信息";
private static final String USER="sa";
private static final String PASSWORD="111111";
private static Connection con=null;
private static Statement stmt =null,stmt1=null;
private static ResultSet rs =null;
JFrame xs;
int q=0,row=0;
Object[][] obj=new Object[10][6];
public xianshi() throws ClassNotFoundException, SQLException{
xs=new JFrame("列车信息显示");
xs.setBounds(1200,500,600,223);
xs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
xs.setVisible(true);//设置窗口可见
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println("数据库连接成功");
}
public void run(){
String[] title={"车次","终点站","到点","开点","站台","状态"};
try {
stmt =con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt1 =con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("with a as(select *,row_number() over(order by getdate()) as row from 列车 ) select * from a where row >+"+row+" and row <+"+row+11);
if(rs.next()){
rs.previous();
while(rs.next()){
obj[q][0]=rs.getString("车次");obj[q][1]=rs.getString("终点站");obj[q][2]=rs.getString("到点");obj[q][3]=rs.getString("开点");obj[q][4]=rs.getString("站台");obj[q][5]=rs.getString("状态");
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式
String dat=df.format(new Date());
// System.out.println(dat);
Date date = null;
Date datt = null;
try {
date = df.parse(dat);
datt = df.parse((String) obj[q][2]);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(obj[q][2]);
long tim=(datt.getTime()-date.getTime());
// System.out.println(tim);
if(obj[q][5].equals("晚点")){}else{
if(tim<=600000&&tim>=0){stmt1.executeUpdate("update 列车 set 状态='正在检票' where 车次='"+obj[q][0]+"'");}
else if(tim==0){stmt1.executeUpdate("update 列车 set 状态='停止检票' where 车次='"+obj[q][0]+"'");}
else if(tim>600000){stmt1.executeUpdate("update 列车 set 状态='候车' where 车次='"+obj[q][0]+"'");}
else if(tim<0){stmt1.executeUpdate("delete from 列车 where 车次= '"+obj[q][0]+"'");}}
q++;
}
row+=10;
}else{row=0;}
q=0;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
JTable table = new JTable(obj, title);
JScrollPane JSP= new JScrollPane(table);
xs.add(JSP);
xs.validate();
}
}
//测试类
import java.sql.SQLException;
public class zhu {
public static void main(String[] args) throws ClassNotFoundException, SQLException{
guanli a=new guanli();
a.gl();
new shuaxin();
}
}
显示成果
两个窗口均已连接数据库
程序已设置自动获取系统时间更改列车状态以及删除列车
关于程序的bug,欢迎评论留言指出!