當前位置:首頁 » 編程語言 » java實現登錄

java實現登錄

發布時間: 2022-02-24 05:42:55

java中如何用網路編程實現登陸功能

privatevoidpostData(Stringurl,Stringdata)throwsException{
URLurl=newURL(url);
URLConnectionurlConnection=url.openConnection();
urlConnection.setDoOutput(true);
//提交數據
try(PrintWriterpw=newPrintWriter(urlConnection.getOutputStream(),true)){
pw.write(data);
}
//獲得返回結果
try(BufferedReaderbr=newBufferedReader(
newInputStreamReader(urlConnection.getInputStream(),"UTF-8"))){
Stringline;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
}

這是當初我登錄我們學校校園網的代碼(基於 JDK7),參數 url 是你登錄的起始網址(就是讓你輸入用戶名和密碼的那個,參數 data 是要提交的數據,就是形如 username=xxx&password=yyy&action=login&... 這樣的格式。

你可以自己先使用瀏覽器通過「審查元素」,然後再控制台看一下登錄的時候到底需要提交哪些欄位。

Ⅱ java語言實現用戶注冊和登錄

//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.sql.*;

class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密碼:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("確定");
JButton btn2 = new JButton("取消");

public LoginFrm() {
this.setTitle("登陸");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql資料庫
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 資料庫名為Car_zl,密碼為1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名為user,user_ID和User_ps是存放用戶名和密碼的欄位名

if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陸成功!");
} else
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!");
} catch (Exception ex) {

}

if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}

public static void main(String arg[]) {
JFrame.(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}

Ⅲ 用Java實現一個登錄頁面 可以用什麼方法

那應該用jsp頁面里寫一個表單,主要包括用戶名、密碼等欄位,然後再寫一個提交和重置按鈕,採用post或者get方式把表單數據提交到後台進行處理,比如說通過java的jdbc訪問資料庫,判斷輸入表單的數據是否是已經存在的合法用戶,如果是就跳轉到系統主頁面,如果不是就顯示提示錯誤信息。

Ⅳ 用java編程實現用戶注冊並進行登錄操作

String username = "",password = "",passwordagain = ""; // 定義用戶名和密碼

將該變數等於為全局變數 或局部變數即可

Ⅳ java怎樣實現登錄驗證

  • 1.打開編程工具:

    打開java編程的界面,採用的是eclipse軟體;

Ⅵ 用Java編寫注冊登錄程序

什麼都不說了 直接給你代碼吧
package com.moliying.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登錄");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("確定");
private JButton cancel = new JButton("取消");
public Login() {
frame.setSize(300, 200);
frame.setBounds(450, 300, 300, 200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
// 頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系統管理員登錄"));
c.add(titlePanel, "North");
// 中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel a1 = new JLabel("用戶名:");
a1.setBounds(50, 20, 50, 20);
JLabel a2 = new JLabel("密 碼:");
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(110, 20, 120, 20);
password.setBounds(110, 60, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel, "Center");
// 底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel, "South");

ok.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println(username.getText().toString());
}
});

cancel.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
}
public static void main(String[] args) {
// new Login();

String ss = "abbabbbaabbbccba";

System.out.println(ss.split("b").length);

}
}

Ⅶ 用JAVA語言編程實現一個用戶登錄窗口

方法一:
採用JOptionPane中的一個非常有用的靜態方法 showOptionPane();
源碼如下:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;
public class Login1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
usernameField = new JTextField(10);
passwordField = new JPasswordField(10);
Object[] options = {"登錄","取消"};
int i = JOptionPane.showOptionDialog(null,createLoginPanel(),"登錄信息",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,options,options[0]);
if(i==0) {
String username = usernameField.getText();
String password = passwordField.getText();
if(!username.equals("") && !password.equals("")) {
mainFrame.getContentPane().add(new JLabel("用戶名:"+username+" 密碼是:"+password,JLabel.CENTER));
mainFrame.show();
}
else {
JOptionPane.showMessageDialog(null,"用戶名和密碼不能為空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
else System.exit(0);
}
static JPanel createLoginPanel() {
JPanel ret = new JPanel();

JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用戶名:",JLabel.RIGHT));
usernamePanel.add(usernameField);
JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 碼:",JLabel.RIGHT));
passwordPanel.add(passwordField);

Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
ret.add(box);

ret.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填寫登錄信息"));
return ret;
}
static JFrame mainFrame = null;
static JTextField usernameField = null;
static JPasswordField passwordField = null;
}
運行:
javac -deprecation Login1.java
java Login
(因為有一個過期的API,所以用了 -deprecation 命令)

方法二,使用了兩個JFrame類共同實現,第一次顯示第一個frame,當點了登錄後且操作合法時,第一個窗口就被釋放了 dispose();再顯示第二個窗口:
源碼如下:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Login2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static void createAndShowGUI() {
//////////////////////////////////////////////////////////////
loginWindow = new JFrame("登錄信息");
loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginWindow.setBounds(350,350,250,200);
loginWindow.setResizable(false);
JPanel usernamePanel = new JPanel();
usernamePanel.add(new JLabel("用戶名:",JLabel.CENTER));
usernamePanel.add(usernameField);

JPanel passwordPanel = new JPanel();
passwordPanel.add(new JLabel("密 碼:",JLabel.CENTER));
passwordPanel.add(passwordField);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(usernamePanel); box.add(passwordPanel);
JPanel infoPanel = new JPanel();
infoPanel.add(box);
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new Color(244,144,44)),"填寫登錄信息"));
JButton submitButton = new JButton("登錄");
JButton cancelButton = new JButton("取消");
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();

if(!username.equals("") && !password.equals("")) {
loginWindow.dispose();
mainFrame.getContentPane().add(new JLabel("用戶名:"+username+" 密碼是:"+password,JLabel.CENTER));
mainFrame.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"用戶名和密碼不能為空","提示",JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
}
});
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(submitButton); buttonPanel.add(cancelButton);
loginWindow.getContentPane().add(infoPanel,BorderLayout.CENTER);
loginWindow.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.EAST);
loginWindow.getContentPane().add(new JPanel(),BorderLayout.WEST);
loginWindow.setVisible(true);
/////////////////////////////////////////////////////////////////
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(250,250,400,300);
mainFrame.setVisible(false);
}
static JFrame loginWindow,mainFrame;
static final JTextField usernameField = new JTextField(10);
static final JPasswordField passwordField = new JPasswordField(10);
}
運行:
javac -deprecation Login2.java
java Login2

Ⅷ Java中登陸時如何實現的

如果時java web開發的話,那就是跟資料庫掛鉤了。用戶輸入用戶名和密碼,然後後台程序和資料庫保存的用戶名和密碼進行比較,如果成功,就進入允許的界面,如果不成功,重新回到登錄界面,或者返回網站首頁。
希望能幫到你!!!

Ⅸ java實現簡單登錄界面

自己寫的比較規范的代碼,都有注釋:

import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按鈕
import javax.swing.JLabel;//標簽
import javax.swing.JTextField;//文本框
import java.awt.Font;//字體
import java.awt.Color;//顏色
import javax.swing.JPasswordField;//密碼框
import java.awt.event.ActionListener;//事件監聽
import java.awt.event.ActionEvent;//事件處理
import javax.swing.JOptionPane;//消息窗口

public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;

public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}

public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉框架的同時結束程序
this.setSize(300,200);//設置框架大小為長300,寬200
this.setResizable(false);//設置框架不可以改變大小
this.setTitle("用戶登錄");//設置框架標題
this.pnluser.setLayout(null);//設置面板布局管理
this.pnluser.setBackground(Color.cyan);//設置面板背景顏色
this.lbluserLogIn.setText("用戶登錄");//設置標簽標題
this.lbluserLogIn.setFont(new Font("宋體",Font.BOLD | Font.ITALIC,14));//設置標簽字體
this.lbluserLogIn.setForeground(Color.RED);//設置標簽字體顏色
this.lbluserName.setText("用戶名:");
this.lbluserPWD.setText("密 碼:");
this.btnSub.setText("登錄");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//設置標簽x坐標120,y坐標15,長60,寬20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名類實現ActionListener介面
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//載入標簽到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//載入面板到框架
this.setVisible(true);//設置框架可顯
}

public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"賬號不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密碼不能為空","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"賬號或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
return;
}
}

public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}

public static void main(String[] args){
new UserLogIn();
}
}

Ⅹ 用JAVA編寫一個登陸系統。

第一個java文件LogoingDemo.java

importjava.util.Scanner;

publicclassLogoingDemo{
publicstaticvoidmain(String[]args){
System.out.println("請輸入用戶名");
Scannersc=newScanner(System.in);
Stringname=sc.nextLine();
System.out.println("請輸入密碼");
Stringpsw=sc.nextLine();
sc.close();
CheckDemocd=newCheckDemo(name,psw);//用戶名和密碼傳入驗證類
booleanbo=cd.check();//調用方法進行驗證
if(bo){
System.out.println("登錄成功");
}else{
System.out.println("登錄失敗:提示用戶名admin密碼123");
}
}
}

第二個java文件CheckDemo.java

publicclassCheckDemo{
publicStringname;
publicStringpsw;
publicCheckDemo(Stringname,Stringpsw){//構造器
this.name=name;
this.psw=psw;
}
publicbooleancheck(){
//用戶名密碼不能為空.用戶名=admin密碼=123
if(name!=null&&psw!=null&&name.equals("admin")&&psw.equals("123")){
returntrue;
}
returnfalse;
}
}

效果


請輸入用戶名
admin
請輸入密碼
123
登錄成功
------------------------------------
請輸入用戶名
add
請輸入密碼
123
登錄失敗:提示用戶名admin密碼123
熱點內容
解壓縮好卡 發布:2025-01-09 10:18:16 瀏覽:794
物資補給站我的世界伺服器 發布:2025-01-09 10:18:09 瀏覽:62
伺服器ip顯示泛播 發布:2025-01-09 10:10:34 瀏覽:711
緩存火影圖片 發布:2025-01-09 10:05:00 瀏覽:645
設置消費密碼驗證的渠道是什麼 發布:2025-01-09 09:59:21 瀏覽:871
小米9域名伺服器地址 發布:2025-01-09 09:59:14 瀏覽:607
各類資料庫 發布:2025-01-09 09:58:30 瀏覽:254
php判斷進制 發布:2025-01-09 09:54:44 瀏覽:282
何謂編程結構 發布:2025-01-09 09:54:09 瀏覽:381
python期末 發布:2025-01-09 09:54:01 瀏覽:709