<b id="yt35u"></b>
  • <b id="yt35u"></b>

  • <tt id="yt35u"><form id="yt35u"></form></tt>
    <button id="yt35u"></button>

    <button id="yt35u"></button>

    <strike id="yt35u"><dfn id="yt35u"></dfn></strike>

    <i id="yt35u"></i>
    1. <tt id="yt35u"></tt>
      <b id="yt35u"><sub id="yt35u"><tr id="yt35u"></tr></sub></b>

      教育路上

      全國站>Java>課堂作業>關于java的InetAddress連接
      學員需求

      關于java的InetAddress連接

      摘要:關于java的InetAddress連接。以下是我們為大家整理的,相信大家閱讀完后肯定有了自己的選擇吧。

      2021-07-11 21:31張文韜

      發布時間:
      2021-07-11 21:31
      信息來源:
      張文韜
      瀏覽次數:
      2730
      關于java的InetAddress連接

      package ZY;

      import java.sql.Statement;

      import javax.naming.spi.DirStateFactory.Result;

      import java.net.Inet4Address;
      import java.net.InetAddress;
      import java.net.UnknownHostException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;

      public class Connecting {// 連接類

      public static boolean Yanzheng(String username, String password) {// 此方法驗證賬號和密碼
      // 輸入用戶名和密碼進行驗證!
      // 返回布爾類型
      boolean flag = false;
      try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection cons = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "123456");
      Statement st = cons.createStatement();
      ResultSet count = st.executeQuery("select * from user where user='" + username + "' and pass='" + password + "';");
      flag = count.next();
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.out.println("未知驗證錯誤");
      }
      // System.out.println(flag);
      return flag;//返回值false

      }
      public static void addInfoData(String username, String password) {// 向數據庫添加數據
      try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection cons = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "123456");
      Statement st = cons.createStatement();

      // int count=st.executeUpdate("insert into info
      // values(null,"+username+","+password+")");
      int count = st.executeUpdate("insert into user values('" + username + "','" + password + "')");
      System.out.println(count);
      } catch (Exception e) {
      // TODO Auto-generated catch block
      // System.out.println("未知錯誤");
      }

      }


      }

      =========================================================================================================================================================

      package ZY;

      import java.awt.Color;

      import java.awt.Container;
      import java.awt.Cursor;
      import java.awt.FlowLayout;
      import java.awt.Font;
      import java.awt.Image;
      import java.awt.Panel;
      import java.awt.TextField;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.ImageIcon;
      import javax.swing.JButton;
      import javax.swing.JCheckBox;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      import javax.swing.JPasswordField;
      import javax.swing.JRootPane;
      import javax.swing.JTextField;


      public class EnterWindow extends JFrame {

      public static boolean yan = false;
      public static RegisterWindow rw;
      private Panel mp = new Panel();
      private JLabel user;//JLabel標簽
      private JLabel pass;//JLabel標簽
      private TextField tf1;//文本框
      private JPasswordField tf2;//密碼框
      //定義按鈕
      private JButton StartButton;
      private JButton registerButton;

      public EnterWindow() {
      this.setTitle("登錄界面");
      mp.setBounds(200, 200, 380, 280);//定義起始位置和大小
      this.setBounds(200, 200, 380, 280);
      this.setResizable(false);//固定窗口大小不可拖動
      this.add(mp);
      mp.setBackground(Color.white);
      mp.setLayout(null);
      init();
      //窗體的監聽事件
      this.addWindowListener(new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent e) {
      //監聽關閉
      // TODO Auto-generated method stub
      }

      });
      }

      public void init() {
      //相關組件--------------------
      user = new JLabel("用戶:");
      pass = new JLabel("密碼:");
      user.setBounds(80, 45, 32, 16);
      pass.setBounds(80, 85, 32, 16);

      tf1 = new TextField();
      tf2 = new JPasswordField();
      tf1.setBounds(120, 40, 200, 25);
      tf2.setBounds(120, 80, 200, 25);
      StartButton = new JButton("登錄");
      registerButton = new JButton("注冊");
      StartButton.setBounds(140, 120, 80, 30);
      registerButton.setBounds(240, 120, 80, 30);

      mp.add(StartButton);
      mp.add(registerButton);
      mp.add(user);
      mp.add(tf1);
      mp.add(pass);
      mp.add(tf2);

      StartButton.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent e) {

      String username = new String(tf1.getText());
      String password = new String(tf2.getPassword());
      if (username.equals("") || password.equals("")) {//判斷賬號密碼是否為空
      JOptionPane.showMessageDialog(null, "未檢測到輸入內容!", "information", JOptionPane.ERROR_MESSAGE);
      } else {
      yan = Connecting.Yanzheng(username, password);//向數據庫發送消息 將賬號和密碼進行驗證處理
      //返回值 為true 和false

      }
      if (yan == true) {
      JOptionPane.showMessageDialog(null, "登錄成功", "information", JOptionPane.INFORMATION_MESSAGE);
      Example.ex=new Example();
      Example.enw.setVisible(false);//設置窗體不可見

      } else {
      JOptionPane.showMessageDialog(null, "賬號或者密碼錯誤", "information", JOptionPane.ERROR_MESSAGE);
      }
      // TODO Auto-generated method stub

      }

      });

      registerButton.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent e) {
      // TODO Auto-generated method stub
      rw=new RegisterWindow();//調用RegisterWindow的構造方法
      rw.setVisible(true);

      }

      });

      }

      }

      =========================================================================================================================================================


      package ZY;

      import java.awt.Color;
      import java.awt.Panel;
      import java.awt.TextField;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      import javax.swing.JPasswordField;


      public class RegisterWindow extends JFrame {
      public static boolean successful = false;
      public static boolean failure = false;
      public static boolean registerOpen = false;
      Connecting ct2 = new Connecting();
      private Panel mp = new Panel();
      private JLabel user;
      private JLabel pass;
      private TextField tf1;
      private JPasswordField tf2;
      private JButton StartButton;
      private JButton registerButton;

      public RegisterWindow() {
      this.setTitle("注冊界面");
      mp.setBounds(400, 400, 380, 280);
      this.setBounds(400, 400, 380, 280);
      this.setResizable(false);
      this.add(mp);
      mp.setBackground(Color.orange);
      // this.setLayout(null);
      mp.setLayout(null);
      init();
      this.addWindowListener(new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent e) {
      // TODO Auto-generated method stub
      // JOptionPane.showMessageDialog(null, "不要在這里點X", "information",
      // JOptionPane.ERROR_MESSAGE);
      }

      });
      }

      public void init() {

      user = new JLabel("用戶:");
      pass = new JLabel("密碼:");
      user.setBounds(80, 45, 32, 16);
      pass.setBounds(80, 85, 32, 16);

      tf1 = new TextField();
      tf2 = new JPasswordField();
      tf1.setBounds(120, 40, 200, 25);
      tf2.setBounds(120, 80, 200, 25);
      registerButton = new JButton("提交");
      registerButton.setBounds(180, 120, 80, 30);

      mp.add(registerButton);
      mp.add(user);
      mp.add(tf1);
      mp.add(pass);
      mp.add(tf2);

      registerButton.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent e) {
      // TODO Auto-generated method stub
      @SuppressWarnings("deprecation")
      String username = new String(tf1.getText());
      String password = new String(tf2.getPassword());
      if (username.equals("") && !password.equals("")) {
      JOptionPane.showMessageDialog(null, "未檢測到用戶名輸入", "information", JOptionPane.ERROR_MESSAGE);
      } else if (!username.equals("") && password.equals("")) {
      JOptionPane.showMessageDialog(null, "未檢測到密碼輸入", "information", JOptionPane.ERROR_MESSAGE);
      } else if (username.equals("") && password.equals("")) {
      JOptionPane.showMessageDialog(null, "請輸入內容??!", "information", JOptionPane.ERROR_MESSAGE);
      } else {
      Connecting.addInfoData(username, password);
      }

      if (registerOpen == true) {
      JOptionPane.showMessageDialog(null, "此用戶已存在 請重新注冊", "information", JOptionPane.ERROR_MESSAGE);
      System.exit(0);
      // EnterWindow.successful=true;

      } else if (!username.equals("") && !password.equals("")) {
      JOptionPane.showMessageDialog(null, "注冊成功", "information", JOptionPane.INFORMATION_MESSAGE);
      EnterWindow.rw.setVisible(false);
      }

      }

      });

      }
      }

      =====================================================================================================================================================

      package ZY;

      import java.awt.*;
      import java.awt.event.*;
      import java.io.Reader;
      import javax.swing.*;
      import java.sql.*;
      import java.util.*;
      import java.awt.event.*;
      import java.sql.*;

      public class Example extends JFrame {
      //定義類的變量
      public static Example ex;
      public static EnterWindow enw;
      //定義窗體變量
      private JButton reg;//注冊按鈕
      private JButton show;//查看按鈕
      private JButton edit;//編輯按鈕
      private JButton del;//刪除按鈕
      private JPanel up;
      private JPanel down;
      private JLabel uptext;
      private JTextField user;//賬號框
      private JTextField pass;//密碼框
      private JLabel result;
      private JButton editpass=new JButton("修改密碼");//提交改密按鈕
      private JButton dell=new JButton("刪除用戶");//提交刪除按鈕
      private Map usermap=new HashMap();
      //定義數據庫變量
      Statement stmt = null;
      Connection conn = null;
      ResultSet rs = null;
      //查看
      public void show() {
      Object[] tabletop= {"用戶名","密碼"};
      Object[][] tableuser= new Object[usermap.size()][];//創建二維數組
      Setuserkey=usermap.keySet();
      int i=0;
      for(String user:userkey) {//用foreach循環將userkey的值賦值給user
      tableuser[i]=new Object[]{user,usermap.get(user)};
      i++;
      }
      tableuser=readuser();
      JTable table = new JTable(tableuser,tabletop);//創建表并傳入tableuser,tabletop
      JDialog dialog = new JDialog(Example.this, true );
      dialog.setTitle("顯示用戶名");
      dialog.setSize(300, 200);//窗體大小
      dialog.setLocation(50, 50);//位置
      dialog.add(table);//將表格加入窗體
      dialog.setVisible(true);//窗體可視
      }
      //修改
      void edit() {
      JDialog dialog = new JDialog(Example.this, true );
      dialog.setLayout(new GridLayout(3,2));//設置布局
      dialog.setTitle("用戶修改");
      dialog.setSize(300, 200);
      dialog.setLocation(50, 50);
      JLabel usertext=new JLabel("用戶名");
      JLabel passtext=new JLabel("密碼");
      user=new JTextField();
      pass=new JTextField();
      result=new JLabel();
      dialog.add(usertext);//添加按鈕及文本框
      dialog.add(user);//添加按鈕及文本框
      dialog.add(passtext);//添加按鈕及文本框
      dialog.add(pass);//添加按鈕及文本框
      dialog.add(editpass);//添加按鈕及文本框
      dialog.add(result);//添加按鈕及文本框

      dialog.setVisible(true);//窗體可視
      }
      //刪除
      void del() {
      JDialog dialog = new JDialog(Example.this, true );
      dialog.setLayout(new GridLayout(3,2));
      dialog.setTitle("用戶刪除");
      dialog.setSize(300, 200);
      dialog.setLocation(50, 50);
      JLabel usertext=new JLabel("用戶名");
      JLabel passtext=new JLabel("密碼");
      user=new JTextField();
      pass=new JTextField();
      result=new JLabel();
      //將上面的按鈕和文框添加進去
      dialog.add(usertext);
      dialog.add(user);
      dialog.add(passtext);
      dialog.add(pass);
      dialog.add(dell);
      dialog.add(result);

      dialog.setVisible(true);//窗體可呈現出來
      }
      //數據庫的coon
      void conn() {
      try {
      // 1. 注冊數據庫的驅動
      Class.forName("com.mysql.jdbc.Driver");
      // 2.通過DriverManager獲取數據庫連接
      String url = "jdbc:mysql://localhost:3306/java";
      String username = "root";
      String password = "123456";
      conn = DriverManager.getConnection(url, username, password);
      // 3.通過Connection對象獲取Statement對象
      stmt = conn.createStatement();
      // 4.使用Statement執行SQL語句。
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      //數據庫的回收
      void closedb() {
      // 6.回收數據庫資源
      if (rs != null) {
      try {
      rs.close();
      } catch (SQLException e) {
      e.printStackTrace();
      }
      rs = null;
      }
      if (stmt != null) {
      try {
      stmt.close();
      } catch (SQLException e) {
      e.printStackTrace();
      }
      stmt = null;
      }
      if (conn != null) {
      try {
      conn.close();
      } catch (SQLException e) {
      e.printStackTrace();
      }
      conn = null;
      }
      }
      public Example() {
      //窗體
      JFrame f=new JFrame("注冊窗口");
      f.setSize(300,200);
      f.setLocation(300,200);
      show=new JButton("查看");//查看按鈕
      edit=new JButton("修改") ;//編輯按鈕
      del=new JButton("刪除");//刪除按鈕
      f.setLayout(new BorderLayout());//布局
      up=new JPanel() ;
      down=new JPanel();
      f.add(up,BorderLayout.NORTH);//邊框布局——北
      f.add(down,BorderLayout.SOUTH);//邊框布局——南
      down.add(show);//添加查看按鈕
      down.add(edit);//添加編輯按鈕
      down.add(del);//添加刪除按鈕
      uptext=new JLabel("歡迎進入!");//發送歡迎辭
      uptext.setFont(new Font("宋體",Font.PLAIN,25));//設置文本字體
      up.add(uptext);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //菜單
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("操作");
      JMenuItem item2 = new JMenuItem("查看用戶");//將注冊用戶賦予給item2
      JMenuItem item3 = new JMenuItem("修改用戶");//將注冊用戶賦予給item3
      JMenuItem item4 = new JMenuItem("刪除用戶");//將注冊用戶賦予給item4

      f.setJMenuBar(menuBar);
      menuBar.add(menu);
      menu.add(item2);//將item2加入
      menu.add(item3);//將item3加入
      menu.add(item4);//將item4加入
      f.setVisible(true);
      //查看用戶名
      show.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      show();
      }
      });
      //菜單查看用戶名
      item2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
      show();

      }
      });
      //按鈕修改密碼
      edit.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
      edit();

      }
      });
      //菜單修改密碼
      item3.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
      edit();

      }
      });
      //按鈕刪除監聽
      del.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      del();

      }
      });
      //菜單刪除監聽
      item4.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      del();

      }
      });


      //提交修改密碼監聽
      editpass.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      String usersave=user.getText();//獲取用戶文本框
      String passave=pass.getText();//獲取密碼文本框
      if(usermap.size()>0){
      Setuserkey=usermap.keySet();
      for(String user:userkey) {
      if(!usersave.equals(user)) {
      usermap.put(usersave,passave);
      result.setText("修改密碼成功");
      update(usersave,passave);
      }else {
      result.setText("密碼重復");
      }
      }
      }else {
      usermap.put(usersave,passave);
      result.setText("修改密碼成功");
      update(usersave,passave);
      }
      }

      private void update(String usersave, String passave) {
      // TODO 自動生成的方法存根
      }
      });
      //刪除賬戶與密碼監聽
      dell.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      String usersave=user.getText();
      String passave=pass.getText();
      if(usermap.size()>0){
      Setuserkey=usermap.keySet();
      for(String user:userkey) {
      if(!usersave.equals(user)) {
      usermap.put(usersave,passave);
      result.setText("刪除成功");
      del(usersave,passave);
      }else {
      result.setText("不要重復刪除");
      }
      }
      }else {
      usermap.put(usersave,passave);
      result.setText("刪除成功");
      del(usersave,passave);
      }
      }

      });
      }

      //讀取數據庫用戶
      Object[][] readuser() {
      Object[][] tableuser = null;
      try {
      conn();
      int count=0;
      String sqlall = "select count(*) as result from user";
      rs = stmt.executeQuery(sqlall);
      while(rs.next())
      {
      count = rs.getInt("result");
      }
      tableuser= new Object[count][];
      String sql = "select * from user";
      rs = stmt.executeQuery(sql);
      int i=0;
      while (rs.next()) {
      String user = rs.getString("user");
      String pass = rs.getString("pass");
      tableuser[i]=new Object[]{user,pass};
      i++;
      }
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      closedb();
      }
      return tableuser;
      }
      //插入數據庫用戶
      void insertuser(String user,String pass) {
      try {
      conn();
      String sql = "INSERT INTO user(user,pass) value('"+user+"','"+pass+"')";//插入數據庫語句
      stmt.executeUpdate(sql);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      closedb();
      }
      }

      //修改數據庫
      void editpass(String user,String pass) {
      try {
      //1. 注冊數據庫的驅動
      Class.forName("com.mysql.jdbc.Driver");
      //2.通過DriverManager獲取數據庫連接
      String url = "jdbc:mysql://localhost:3306/java";
      String username = "root";
      String password = "123456";
      conn = DriverManager.getConnection(url, username, password);
      //3.通過Connection對象獲取Statement對象
      stmt = conn.createStatement();
      //4.使用Statement執行SQL語句。
      String sql = "update user set pass='"+pass+" where 'user='"+user+"'";//修改數據庫語句
      stmt.executeUpdate(sql);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      closedb();
      }

      }
      //刪除數據庫用戶
      void del(String user,String pass) {
      try {
      //1. 注冊數據庫的驅動
      Class.forName("com.mysql.jdbc.Driver");
      //2.通過DriverManager獲取數據庫連接
      String url = "jdbc:mysql://localhost:3306/java";
      String username = "root";
      String password = "123456";
      conn = DriverManager.getConnection(url, username, password);
      //3.通過Connection對象獲取Statement對象
      stmt = conn.createStatement();
      //4.使用Statement執行SQL語句。
      String sql = "delete from user where user='"+user+"' and pass='"+pass+"'";//刪除賬戶和密碼sql語句
      stmt.executeUpdate(sql);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      closedb();
      }

      }
      public static void main(String[] aa) {
      enw=new EnterWindow();//調用這個類里面的對象和方法
      enw.setVisible(true);//可視化
      }
      }





      上一篇:
      java聊天室案例
      下一篇:
      java的Socket 連接
      標簽:
      網友評論
      發布評論

      訪客的評論 2024/07/10 12:44

      文中描述的是準確的嗎,如何報名!

      相關推薦
      我也來發表評價關閉
      我對該內容的評價:
      0
      評價500
      驗證碼: 看不清 換一張
      提交 (匿名發布,無須擔心別人知道您的身份)
      學校免費發布信息關閉
      我們審核后會盡快展示,如有圖片請發郵件到:edu63@foxmail.com

      姓      名:

      內      容:

      手機號碼:

      驗  證  碼:  換一張

      確認提交
      填寫需求信息關閉
      我們會根據您的需求匹配并審核留言

      姓      名:

      意向城市:

      留      言:

      手機號碼:

      驗  證  碼:  換一張

      確認提交
      完善補充本文信息關閉
      非常感謝您幫助完善補充本文信息


       換一張

      確認提交
      女班长给我看她的小内内

        <b id="yt35u"></b>
    1. <b id="yt35u"></b>

    2. <tt id="yt35u"><form id="yt35u"></form></tt>
      <button id="yt35u"></button>

      <button id="yt35u"></button>

      <strike id="yt35u"><dfn id="yt35u"></dfn></strike>

      <i id="yt35u"></i>
      1. <tt id="yt35u"></tt>
        <b id="yt35u"><sub id="yt35u"><tr id="yt35u"></tr></sub></b>