package CarParking; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Timestamp; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; import javax.swing.*; public class Add_in extends JFrame { public Add_in() { JFrame frame = new JFrame("添加入库车辆信息"); frame.setLayout(null); JLabel IDStr = new JLabel("车牌号:"); IDStr.setBounds(180, 200, 100, 25); frame.add(IDStr); JTextField carID = new JTextField(); carID.setBounds(250, 200, 150, 25); frame.add(carID); JLabel intimeStr = new JLabel("入库时间:"); intimeStr.setBounds(180, 250, 100, 25); frame.add(intimeStr); JButton nowTime = new JButton("获取当前时间"); nowTime.setBounds(250, 250, 150, 25); frame.add(nowTime); JButton buttonadd = new JButton("添加"); buttonadd.setBounds(200, 350, 70, 25); frame.add(buttonadd); JButton buttonreturn = new JButton("返回"); buttonreturn.setBounds(300, 350, 70, 25); frame.add(buttonreturn); frame.setBounds(400, 200, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); //返回按钮监听器 buttonreturn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); mainWindow ar = new mainWindow(); } }); //为添加按钮增加监听器 buttonadd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DB bd = new DB(); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); String sql = "insert into car (carid,carin) values (?,?)"; try { PreparedStatement ps = bd.getConn().prepareStatement(sql); ps.setString(1, carID.getText()); ps.setTimestamp(2,timeStamp); ps.executeUpdate(); ps.close(); } catch (SQLException ex) { System.out.println("添加失败!"); } finally { bd.close(); } } }); } // public static void main (String[]args){ // Add_in f = new Add_in(); // } }
package CarParking; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import java.lang.*; import java.sql.Timestamp; import java.util.Date; import javax.swing.*; public class Add_out extends JFrame { public Add_out() { JFrame frame = new JFrame("添加出库车辆信息"); frame.setLayout(null); JLabel IDStr = new JLabel("车牌号:"); IDStr.setBounds(180, 200, 100, 25); frame.add(IDStr); JTextField carID = new JTextField(); carID.setBounds(250, 200, 150, 25); frame.add(carID); JLabel intimeStr = new JLabel("出库时间:"); intimeStr.setBounds(180, 250, 100, 25); frame.add(intimeStr); // JTextField carOut = new JTextField(); // carID.setBounds(250, 250, 150, 25); // frame.add(carOut); JButton nowTime = new JButton("获取当前时间"); nowTime.setBounds(250, 250, 150, 25); frame.add(nowTime); JLabel jlpayment = new JLabel("费用:"); jlpayment.setBounds(180, 300, 100, 25); frame.add(jlpayment); //计算结果显示 JLabel jpayment= new JLabel(); frame.add(jpayment); jpayment.setBounds(280, 300, 100, 25); JButton calulate = new JButton("计算"); calulate.setBounds(340, 300, 60, 25); frame.add(calulate); JButton buttonadd = new JButton("添加"); buttonadd.setBounds(200, 350, 70, 25); frame.add(buttonadd); JButton buttonreturn = new JButton("返回"); buttonreturn.setBounds(300, 350, 70, 25); frame.add(buttonreturn); frame.setBounds(400, 200, 600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); //返回按钮监听器 buttonreturn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); mainWindow f = new mainWindow(); } }); //为获取当前时间添加监听器 nowTime.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DB bd = new DB(); Date date = new Date(); Timestamp timeStamp = new Timestamp(date.getTime()); String number = carID.getText(); // String sql = "insert into car (carout) values (?) where carid='"+number+"' and carout =null"; String sql="update car set carout='"+timeStamp+"' where carid='"+number+"' and carout is null"; try { PreparedStatement ps = bd.getConn().prepareStatement(sql); System.out.println("获取成功!"); ps.executeUpdate(); ps.close(); } catch (SQLException ex) { System.out.println("添加失败!!!"); } finally { bd.close(); } } }); //为计算按钮添加监听 calulate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DB db = new DB(); Double payment =0.0;