import java.sql.*; import static java.lang.Class.forName; import static java.sql.DriverManager.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Objects; import java.util.Random; //数据库的连接 class Collcetion{ Connection conn = null; public Collcetion() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException ex) { throw new RuntimeException(ex); } System.out.println("加载驱动成功!"); String dbURL = "jdbc:sqlserver://localhost;DatabaseName=User1001;encrypt=true;trustServerCertificate=true"; String user = "sa"; String password = "123456";//在引号中修改为自己设置的登陆密码 try { conn = getConnection(dbURL, user, password); } catch (SQLException ex) { throw new RuntimeException(ex); } System.out.println("连接数据库成功!"); try (Statement statement = conn.createStatement()) { } catch (SQLException e) { throw new RuntimeException(e); } } } class Conction extends Collcetion{ ArrayList<String> list1=new ArrayList<>(); ArrayList<String> list2=new ArrayList<>(); ArrayList <String> list3=new ArrayList<>(); ArrayList <String> list4=new ArrayList<>(); String string1=""; String string2="";String string3=""; public void Collcetion(String s1, String s2){ try (Statement stmt =conn.createStatement() ){ ResultSet rs=stmt.executeQuery("select * from usertable2 where username='"+s1+"'"+"and password="+s2); if(!rs.isBeforeFirst())//获取行号 { new showwarn("请输入正确的用户名或者密码"); } else { while(rs.next()) { String tet= rs.getString("username"); list2.add(tet); System.out.println(tet); } for(int i=0;i<list2.size();i++) { if(list2.get(i).contains(s1)); { string2=list2.get(i); } } new show1(string2,"登录成功"); } } catch (SQLException ex) { throw new RuntimeException(ex); } } public void Collcetion(String t1,String t2,String t3,String t4,String t5) { String sql = "INSERT INTO usertable2(username,potsize,potid,pwater,password) VALUES (?,?,?,?,?)"; try { PreparedStatement stat = conn.prepareStatement(sql);//进行预编译 stat.setString(1,t1); stat.setString(2,t2); stat.setString(3,t3); stat.setString(4,t4); stat.setString(5,t5); stat.executeUpdate(); } catch (SQLException ex) { throw new RuntimeException(ex); } String sql2="select * from usertable2"; PreparedStatement stmt = null; PreparedStatement stmt2 = null; try { stmt = conn.prepareStatement(sql); stmt2=conn.prepareStatement(sql2); } catch (SQLException ex) { throw new RuntimeException(ex); } try (ResultSet res = stmt2.executeQuery()) { while (res.next()) { String title = res.getString("username"); list1.add(title); } for(int i=0;i<list1.size();i++) { if(list1.get(i).contains(t1)) { string1=list1.get(i); } } new show1(string1,"注册成功"); } catch (SQLException ex) { throw new RuntimeException(ex); } try { stmt.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } try { conn.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } } public void Collcetion(String c1,String c2,String c3)//模块3 { try (Statement stmt3 =conn.createStatement() ){ ResultSet rs=stmt3.executeQuery("select * from usertable2 where username='"+c1+"'"+"and password="+c2); if(!rs.isBeforeFirst())//获取行号 { new showwarn("请输入正确的用户名或者密码"); } else { String sql4 = "INSERT INTO usertable4(PNM,PASS,STIM) VALUES (?,?,?)"; try { PreparedStatement stat = conn.prepareStatement(sql4);//进行预编译 stat.setString(1,c1); stat.setString(2,c2); stat.setString(3,c3); stat.executeUpdate(); } catch (SQLException ex) { throw new RuntimeException(ex); } String sql2="select * from usertable4"; PreparedStatement stmt = null; PreparedStatement stmt2 = null; try { stmt = conn.prepareStatement(sql4); stmt2=conn.prepareStatement(sql2); } catch (SQLException ex) { throw new RuntimeException(ex); } try (ResultSet res = stmt2.executeQuery()) { while (res.next()) { String title = res.getString("PNM"); list3.add(title); } for(int i=0;i<list3.size();i++) { if(list3.get(i).contains(c1)) { string3=list3.get(i); } } new show1(string3,"申请成功"); } catch (SQLException ex) { throw new RuntimeException(ex); } try { stmt.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } try { conn.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } } } catch (SQLException ex) { throw new RuntimeException(ex); } } public void Collcetion(String k1,String k2,int m){// 修改模块 try (Statement stmt3 =conn.createStatement() ){ ResultSet rs=stmt3.executeQuery("select * from usertable4 where PNM='"+k1+"'"+"and PASS="+k2); if(!rs.isBeforeFirst())//获取行号 { new showwarn("请输入正确的用户名或者密码"); } else { String sql2="select DATEDIFF(day,STIM,'2023-06-16') as age from usertable4 "; PreparedStatement stmt2 = null; try { stmt2=conn.prepareStatement(sql2); } catch (SQLException ex) { throw new RuntimeException(ex); } try (ResultSet res = stmt2.executeQuery()) { while (res.next()) { String title = res.getString("age"); list4.add(title); } System.out.println(list4.get(list4.size()-1)); new show1("共计停靠时间"+list4.get(list4.size()-1)+"天","离港成功"); } String sql4 = "delete from usertable4 where PNM=?"; try { PreparedStatement stat = conn.prepareStatement(sql4);//进行预编译 stat.setString(1,k1); stat.executeUpdate(); } catch (SQLException ex) { throw new RuntimeException(ex); } try { stmt2.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } try { conn.close(); } catch (SQLException ex) { throw new RuntimeException(ex); } } } catch (SQLException ex) { throw new RuntimeException(ex); } } } class showwarn extends JFrame{ public showwarn(String g){ super("WARN"); JLabel name=new JLabel(g); name.setBounds(30,80,500,100); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),30)); JPanel jp=new JPanel(); String path="D://E//B2.jpg"; ImageIcon background=new ImageIcon(path); JLabel pack=new JLabel(background); pack.setBounds(0,0,this.getWidth(),this.getHeight()); JPanel imagePan=(JPanel) this.getContentPane(); imagePan.setOpaque(false); this.getLayeredPane().add(pack,new Integer(Integer.MIN_VALUE)); jp.add(pack); jp.setOpaque(false); this.setVisible(true); this.setResizable(false); jp.add(name); this.setSize(500,500); this.setLocation(500,300); this.add(name); this.add(jp); jp.setOpaque(false); this.setVisible(true); } } class show1 extends JFrame{//提示模块 public show1(String s,String to){ super("注册"); JLabel tatil=new JLabel(to); JLabel name=new JLabel("用户名: "+s); tatil.setBounds(30,40,500,100); name.setBounds(30,80,500,100); tatil.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); JPanel jp=new JPanel(); this.setResizable(false); this.setSize(500,300); this.setLocation(400,400); this.add(name); this.add(tatil);this.add(jp); jp.setOpaque(false); this.setVisible(true); } } class GUIshow1 extends JFrame{ public GUIshow1() { super("新用户注册"); JFrame jf=new JFrame(); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel name=new JLabel("用户名:"); JLabel Sex=new JLabel("船型号:"); JLabel ID=new JLabel("船编号:"); JLabel password=new JLabel("设置密码:"); JLabel pwater=new JLabel("排水量:"); JLabel vfcd=new JLabel("验证码"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); JTextField data3=new JTextField(); JTextField data4=new JTextField(); JTextField data5=new JTextField(); JTextField data6=new JTextField(); JPanel jp=new JPanel(); String path="D://E//A1.jpg"; ImageIcon background=new ImageIcon(path); JLabel pack=new JLabel(background); pack.setBounds(0,0,this.getWidth(),this.getHeight()); JPanel imagePan=(JPanel) this.getContentPane(); imagePan.setOpaque(false); this.getLayeredPane().add(pack,new Integer(Integer.MIN_VALUE)); jp.add(pack); this.setResizable(false); jp.add(name); this.setSize(450,450); this.setLocation(400,300); this.add(name); this.add(jp); jp.setOpaque(false); this.setVisible(true); Random random=new Random(); int n=random.nextInt(100000,999999); String yz=String.valueOf(n); JTextField data7=new JTextField(yz); data1.setBounds(200,95,150,30); data2.setBounds(200,145,150,30); data3.setBounds(200,195,150,30); data4.setBounds(200,245,150,30); data5.setBounds(200,295,150,30); data6.setBounds(200,345,75,30); data7.setBounds(300,345,75,30); name.setBounds(100,60,100,100); Sex.setBounds(100,110,100,100); ID.setBounds(100,160,100,100); pwater.setBounds(100,210,100,100); password.setBounds(100,260,100,100); vfcd.setBounds(100,310,100,100); dete.setBounds(20,400,100,30); can.setBounds(350,400,100,30); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); Sex.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); ID.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); pwater.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); password.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); vfcd.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); this.setResizable(false); this.setSize(500,500); this.setLocation(100,100); this.add(name); this.add(pwater); this.add(ID); this.add(Sex); this.add(dete); this.add(vfcd);this.add(password); this.add(data2); this.add(data1);this.add(can); this.add(data5);this.add(data4); this.add(data3); this.add(data6);this.add(data7); this.add(jp); data1.setOpaque(false);data3.setOpaque(false);data4.setOpaque(false); data5.setOpaque(false);data6.setOpaque(false);data2.setOpaque(false); data7.setOpaque(false);jp.setOpaque(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String Name=data1.getText(); String Size=data2.getText(); String Id=data3.getText(); String Pwatert=data4.getText(); String PassWord=data5.getText(); String yz=data6.getText(); if(yz.contains(data7.getText())) { Conction fg = new Conction(); fg.Collcetion(Name,Size,Id,Pwatert,PassWord); } else { new showwarn("请输入正确的验证码"); } } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } class GUIshow2 extends JFrame{ public GUIshow2(){ super("用户登录"); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel name=new JLabel("用户名:"); JLabel password=new JLabel("密码:"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); data1.setBounds(200,115,150,30); data2.setBounds(200,165,150,30); name.setBounds(100,80,100,100); password.setBounds(100,130,100,100); dete.setBounds(20,400,100,30); can.setBounds(350,400,100,30); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); password.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); JPanel jp=new JPanel(); String path="D://E//12.jpg"; ImageIcon background=new ImageIcon(path); JLabel pack=new JLabel(background); pack.setBounds(0,0,this.getWidth(),this.getHeight()); JPanel imagePan=(JPanel) this.getContentPane(); imagePan.setOpaque(false); this.getLayeredPane().add(pack,new Integer(Integer.MIN_VALUE)); jp.add(pack); this.setResizable(false); this.setSize(450,450); this.setLocation(400,300); this.add(jp); jp.setOpaque(false); this.setVisible(true); this.setResizable(false);this.setSize(500,500);this.setLocation(100,100); this.add(name); this.add(password); this.add(dete); this.add(can);this.add(data1); this.add(data2);this.add(jp); data1.setOpaque(false);data2.setOpaque(false); jp.setOpaque(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String Name = data1.getText(); String Password=data2.getText(); Conction fg = new Conction(); fg.Collcetion(Name,Password); } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } class GUIshow3 extends JFrame{ public GUIshow3(){ super("停靠管理"); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel name=new JLabel("用户名:"); JLabel password=new JLabel("登录密码:"); JLabel st=new JLabel("输入停靠港口时间:"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); JTextField data3=new JTextField(); data1.setBounds(260,115,150,30); data2.setBounds(260,165,150,30); data3.setBounds(260,215,150,30); name.setBounds(50,80,100,100); password.setBounds(50,130,100,100); st.setBounds(50,180,200,100); dete.setBounds(50,400,100,30); can.setBounds(350,400,100,30); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); password.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); st.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); JPanel jp=new JPanel(); String path="D://E//A3.jpg"; ImageIcon background=new ImageIcon(path); JLabel pack=new JLabel(background); pack.setBounds(0,0,this.getWidth(),this.getHeight()); JPanel imagePan=(JPanel) this.getContentPane(); imagePan.setOpaque(false); this.getLayeredPane().add(pack,new Integer(Integer.MIN_VALUE)); jp.add(pack); this.setResizable(false); this.setSize(500,500); this.setLocation(400,300); this.add(name); this.add(password);this.add(st);this.add(dete);this.add(can); this.add(data1); this.add(data2);this.add(data3);this.add(jp); jp.setOpaque(false); this.add(jp); jp.setOpaque(false); data1.setOpaque(false); data2.setOpaque(false); data3.setOpaque(false); this.setVisible(true); this.setResizable(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String g1=data1.getText(); String g2=data2.getText(); String g3=data3.getText(); Conction fg = new Conction(); fg.Collcetion(g1,g2,g3); } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } class GUIshow4 extends JFrame{ public GUIshow4(){ super("停靠管理"); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel name=new JLabel("用户名:"); JLabel password=new JLabel("登录密码:"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); data1.setBounds(260,115,150,30); data2.setBounds(260,165,150,30); name.setBounds(50,80,100,100); password.setBounds(50,130,100,100); dete.setBounds(50,400,100,30); can.setBounds(350,400,100,30); name.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); password.setFont(new Font(name.getFont().getName(),name.getFont().getStyle(),20)); JPanel jp=new JPanel(); String path="D://E//A4.jpg"; ImageIcon background=new ImageIcon(path); JLabel pack=new JLabel(background); pack.setBounds(0,0,this.getWidth(),this.getHeight()); JPanel imagePan=(JPanel) this.getContentPane(); imagePan.setOpaque(false); this.getLayeredPane().add(pack,new Integer(Integer.MIN_VALUE)); jp.add(pack); this.setResizable(false); this.setSize(500,500); this.setLocation(400,300); this.add(name); this.add(password);this.add(dete);this.add(can); this.add(data1); this.add(data2);this.add(jp); jp.setOpaque(false); this.add(jp); jp.setOpaque(false); data1.setOpaque(false); data2.setOpaque(false); this.setVisible(true); this.setResizable(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String g1=data1.getText(); String g2=data2.getText(); Conction fg = new Conction(); fg.Collcetion(g1,g2,5); } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } class GUIshow5 extends JFrame{ public GUIshow5(){ super("海港救助"); JFrame jf=new JFrame(); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel size=new JLabel("请选择事故原因"); JLabel time=new JLabel("输入具体位置经度纬度:"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); data1.setBounds(200,115,150,30); data2.setBounds(200,165,150,30); size.setBounds(100,80,100,100); time.setBounds(100,130,100,100); dete.setBounds(20,400,100,30); can.setBounds(350,400,100,30); size.setFont(new Font(size.getFont().getName(),size.getFont().getStyle(),20)); time.setFont(new Font(time.getFont().getName(),time.getFont().getStyle(),20)); JPanel jp=new JPanel(); this.setResizable(false); this.setSize(500,500); this.setLocation(100,100); this.add(size);this.add(time); this.add(dete);this.add(can); this.add(data1); this.add(data2); this.add(jp); jp.setOpaque(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } class GUIshow6 extends JFrame{ public GUIshow6(){ super("管理员登录"); JFrame jf=new JFrame(); JButton dete=new JButton("确定"); JButton can=new JButton("取消"); JLabel size=new JLabel("管理员ID:"); JLabel time=new JLabel("密码:"); JTextField data1=new JTextField(); JTextField data2=new JTextField(); data1.setBounds(200,115,150,30); data2.setBounds(200,165,150,30); size.setBounds(100,80,100,100); time.setBounds(100,130,100,100); dete.setBounds(20,400,100,30); can.setBounds(350,400,100,30); size.setFont(new Font(size.getFont().getName(),size.getFont().getStyle(),20)); time.setFont(new Font(time.getFont().getName(),time.getFont().getStyle(),20)); JPanel jp=new JPanel(); this.setResizable(false); this.setSize(500,500); this.setLocation(100,100); this.add(size); this.add(time); this.add(dete); this.add(can); this.add(data1); this.add(data2); this.add(jp); jp.setOpaque(false); this.setVisible(true); dete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); can.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); } } public class Main extends JFrame implements ActionListener{ public Main() { super("志文的小demo"); //设置大小 setSize(1000, 700); //设置位置 setLocation(200, 50); //背景图片的路径。(相对路径或者绝对路径。本例图片放于"java项目名"的文件下) String path = "D://E//10.jpg"; // 背景图片 ImageIcon background = new ImageIcon(path); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 把背景图片显示在一个标签里面 JLabel label1=new JLabel("欢迎进入港口渔船停泊管理系统"); JLabel label2=new JLabel(df.format(System.currentTimeMillis())); JButton bt1=new JButton("用户登录"); JButton regbt=new JButton("新用户注册"); JButton adm=new JButton("管理员登录"); JButton tstop=new JButton("离港申请"); JButton stop=new JButton("入港申请"); JButton help=new JButton("实时可停靠位置查询"); JLabel label = new JLabel(background); JPanel jp=new JPanel(); // 把标签的大小位置设置为图片刚好填充整个面板 label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明 JPanel imagePanel = (JPanel) this.getContentPane(); label1.setBounds(150,25,800,200); label2.setBounds(800,5,200,75); label1.setFont(new Font(label1.getFont().getName(),label1.getFont().getStyle(),50)); label2.setFont(new Font(label1.getFont().getName(),label1.getFont().getStyle(),20)); // label1.setBorder(new LineBorder(Color.WHITE)); adm.setBounds(425,600,150,40); help.setBounds(425,550,150,40); tstop.setBounds(425,500,150,40); stop.setBounds(425,450,150,40); bt1.setBounds(425,400,150,40); regbt.setBounds(425,350,150,40); regbt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new GUIshow1(); } }); bt1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {new GUIshow2(); } }); stop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new GUIshow3(); } }); tstop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new GUIshow4(); } }); adm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new GUIshow6(); } }); help.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {new GUIshow5(); } }); add(regbt); add(bt1); add(stop); add(adm); add(help); add(tstop); add(label1); add((label2)); add(jp); jp.setOpaque(false); imagePanel.setOpaque(false); // 把背景图片添加到分层窗格的最底层作为背景 this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); //设置可见 setVisible(true); //点关闭按钮时退出 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Main(); } @Override public void actionPerformed(ActionEvent e) { } }