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

java登錄

發布時間: 2022-01-09 18:25:55

java編寫用戶登錄

代碼發給你,如果什麼都不懂的話,估計你也難以運行成功,而且圖片也沒有,要自己會改就能運行了。這些代碼希望能幫到你!

資料庫文件:

create database hotel_Flyer
go

use hotel_Flyer

create table userInfo(
id int primary key identity(1,1),
userName varchar(20),
password varchar(10),
realName nvarchar(20),
sex nvarchar(2),
age int,
favorite nvarchar(100),
telNum varchar(12),
email varchar(50),
address nvarchar(200)
)
go
insert into userInfo(userName,password) values('李連傑','123')
insert into userInfo(userName,password) values('成龍','123')
insert into userInfo(userName,password) values('甄子丹','123')
insert into userInfo(userName,password) values('趙文卓','123')
insert into userInfo(userName,password) values('吳京','123')
insert into userInfo(userName,password) values('周星馳','123')
insert into userInfo(userName,password) values('劉德華','123')

載入驅動類:

public class ConTosql {
Connection con=null;
public Connection getConnection() throws Exception{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=hotel_Flyer", "sa", "sa");
return con;
}

}

連接資料庫類:

public class DengluUC {
Connection con=null;
PreparedStatement stm=null;
ResultSet rs=null;

public boolean checkUser(String user,String pwd){
boolean tof=false;
try {

ConToSQL cts=new ConToSQL();
con=cts.getConnection();
stm=con.prepareStatement("select password from userInfo where userName=?");
stm.setString(1, user);
rs=stm.executeQuery();
if(rs.next()){
if(rs.getString(1).equals(pwd)){
tof= true;
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*finally{
try {
rs.close();
stm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return tof;
}*/
return tof;
}
}

登錄類代碼:

package com.hbsoft.hotel_Flyer.login;
import com.hbsoft.hotel_Flyer.usermanagerForm.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.text.html.Option;

import com.hbsoft.hotel_Flyer.proc.Process;

import java.awt.Point;
import java.sql.*;

import com.hbsoft.hotel_Flyer.database.*;

public class Denglu {

private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="61,26"
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JComboBox jComboBox = null;
private JLabel jL4 = null;
private JLabel jLabel1 = null;
private JPasswordField jPasswordField = null;
private JButton jB1 = null;
private JButton jB2 = null;
private JComboBox jComboBox1 = null;
private JLabel jLabel = null;
private JLabel jLabel2 = null;
private Connection con=null; // @jve:decl-index=0:
private Statement sts=null;
private ResultSet rs=null;
private DatabaseMetaData dmd=null;
private int rowCount=0;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
public JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame("系統登陸");
jFrame.setSize(new Dimension(854, 539));
jFrame.setLocation(new Point(100, 100));
jFrame.setContentPane(getJContentPane());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setResizable(false);
}
return jFrame;
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(3, 4, 845, 503));
jLabel2.setIcon(new ImageIcon("E:/java\uff088\uff09\u73ed\u5468\u67ab/\u7b2c\u5341\u7ec4\u5de5\u4f5c\u7ad9/hotel_Flyer/\u767b\u5f55\u80cc\u666f.jpg"));
jLabel2.setText("");
jLabel = new JLabel();
jLabel.setText("");
jLabel.setIcon(new ImageIcon(getClass().getResource("/com/hbsoft/hotel_Flyer/img/\u767b\u5f55\u80cc\u666f.jpg")));
jLabel.setBounds(new Rectangle(-1, 1, 851, 509));
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(438, 141, 363, 231));
jLabel1.setText("");
jL4 = new JLabel();
jL4.setBounds(new Rectangle(484, 341, 289, 28));
jL4.setHorizontalAlignment(SwingConstants.CENTER);
Timer t =new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e) {
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
jL4.setText(df.format(d));
}
});
t.start();
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBorder(BorderFactory.createLineBorder(Color.cyan, 2));
jContentPane.setFont(new Font("\u96b6\u4e66", Font.PLAIN, 14));
jContentPane.add(getJPanel(), null);
jContentPane.add(jL4, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPasswordField(), null);
jContentPane.add(getJB1(), null);
jContentPane.add(getJB2(), null);
jContentPane.add(getJComboBox1(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel, null);
}
return jContentPane;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(0, 0, 415, 0));
}
return jPanel;
}
/**
* This method initializes jB1
*
* @return javax.swing.JButton
*/
void ThisDispose(){
this.getJFrame().dispose();
}
/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(570, 263, 201, 29));
}
return jPasswordField;
}

/**
* This method initializes jB1
*
* @return javax.swing.JButton
*/
/*void ThisdDispose(){
this.getJFrame().dispose();
}*/
private JButton getJB1() {
if (jB1 == null) {
jB1 = new JButton();
jB1.setBounds(new Rectangle(489, 313, 103, 35));
jB1.setText("登陸");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(new DengluUC().checkUser(jComboBox1.getSelectedItem().toString(), new String(jPasswordField.getPassword()))){
//message.setText("登錄成功");
//message.setForeground(new Color(255,0,0));
new Process((String)jComboBox1.getSelectedItem()).setVisible(true);
ThisDispose();

}else{
if(new String(jPasswordField.getPassword()).length()==0){
JOptionPane.showMessageDialog(null, "密碼不能為空");
}else{
JOptionPane.showMessageDialog(null, "密碼不正確,請重新輸入");
}
}

}
});
}
return jB1;
}

/**
* This method initializes jB2
*
* @return javax.swing.JButton
*/
private JButton getJB2() {
if (jB2 == null) {
jB2 = new JButton();
jB2.setBounds(new Rectangle(642, 312, 103, 35));
jB2.setText("退出");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jB2;
}

/**
* This method initializes jComboBox1
*
* @return javax.swing.JComboBox
*/
public void getResultSet(){
try{
con=new ConToSQL().getConnection();
sts=con.createStatement();
rs=sts.executeQuery("select * from userInfo");

}catch(Exception e){
e.printStackTrace();
}

}
public int getRowCount(){
try {
rs.last();
if (rs.isLast()) {
rowCount = rs.getRow();
}
} catch (Exception e) {
e.printStackTrace();
}
return rowCount;
}
public void closeData(){
try{
rs.close();
sts.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
private JComboBox getJComboBox1() {
getResultSet();
String [] arr=new String[7];
int i=0;
try {
while (rs.next()) {
if(i<arr.length){
arr[i] = rs.getString(2);
i++;}
}
} catch (Exception e) {
e.printStackTrace();
}
if (jComboBox1 == null) {
jComboBox1 = new JComboBox(arr);
closeData();
jComboBox1.setBounds(new Rectangle(569, 218, 201, 29));
}
return jComboBox1;
}

public static void main(String [] args) throws Exception{
// javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
new Denglu().getJFrame().setVisible(true);
}

}

② 用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

③ java注冊登錄

不清楚LZ的意思,不連接資料庫么?

④ Java中登陸時如何實現的

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

⑤ JAVA 用戶登錄怎麼寫 如何給許可權

其實最簡單的方法就是使用角色和用戶兩級管理
首先建立角色:分為管理員和普通用戶等,管理員用來管理系統的後台,而普通用戶就是注冊用戶,可以實現瀏覽商品、管理個人信息等操作!
其次為用戶分配角色,實現分配使用系統模塊的許可權。
角色許可權的分配通過一張資料庫許可權表來實現。把系統的功能模塊的使用許可權設置為表的欄位,然後為每個角色分配許可權,1代表有許可權,0代表無許可權。可以設置一個輔助欄位為是否登錄,通過該欄位判斷用戶是否可以修改個人信息。

⑥ 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編程實現用戶注冊並進行登錄操作

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

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

⑨ 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做登錄界面

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();
}
}

熱點內容
gon引擎自動回收腳本 發布:2024-09-20 05:39:39 瀏覽:246
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:98
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236
e盒的演算法 發布:2024-09-20 03:30:52 瀏覽:144