「java登录程序」java登录界面代码简单

博主:adminadmin 2023-03-17 13:11:09 443

本篇文章给大家谈谈java登录程序,以及java登录界面代码简单对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎么用JAVA写一个用户登入程序

同意楼上的说法,具体点可以这样:创建一个用户表,里边包括LoginName(登录名),UserName(用户名),Password(密码),Age(年龄),Address(地址)。然后编写Java程序(用MVC架构)模型层(M):DBConnection.java(负责连接数据库)

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.*;

public class DBConnection {

private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";

private static final String DB_URL = "jdbc:odbc:text";

public DBConnection() {

}

public static Connection getConnection() {

Connection conn = null;

try {

Class.forName(DRIVER_CLASS);

conn = DriverManager.getConnection(DB_URL);

} catch (SQLException ex) {

System.out.println(ex.getMessage());

} catch (ClassNotFoundException ex) {

System.out.println(ex.getMessage());

}

return conn;

}

}

第2个负责数据库查询操作的类:DBUserManager.java

import edu.systop.text.model.entity.User;

import edu.systop.text.model.dao.DBConnection;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.DriverManager;

import java.sql.*;

public class DBUserManager {

private static final String SQL_SELECT =

"SELECT LoginName,UserName,PassWord,Age,Address FROM UserInfo WHERE LoginName = ? AND PassWord = ?";

public DBUserManager() {

}

public boolean checkDB(User u) {

boolean b = false;

Connection conn = null;

PreparedStatement psmt = null;

ResultSet rs = null;

conn = DBConnection.getConnection();

try {

psmt = conn.prepareStatement(SQL_SELECT);

psmt.setString(1, u.getLoginName());

psmt.setString(2, u.getPassWord());

rs = psmt.executeQuery();

b = rs.next();

if (rs.next()) {

b = true;

}

} catch (SQLException ex) {

System.out.println(ex.getMessage());

} finally {

cleanDB(rs, psmt, conn);

}

return b;

}

public User checkBC(User u) {

Connection conn = null;

PreparedStatement psmt = null;

ResultSet rs = null;

User tmp = new User();

conn = DBConnection.getConnection();

try {

psmt = conn.prepareStatement(SQL_SELECT);

psmt.setString(1, u.getLoginName());

psmt.setString(2, u.getPassWord());

rs = psmt.executeQuery();

if (rs.next()) {

tmp.setLoginName(rs.getString(1));

tmp.setUserName(rs.getString(2));

tmp.setAge(rs.getInt(4));

tmp.setAddress(rs.getString(5));

}

} catch (SQLException ex) {

System.out.println(ex.getMessage());

} finally {

cleanDB(rs, psmt, conn);

}

return tmp;

}

public void cleanDB(ResultSet rs, PreparedStatement psmt, Connection conn) {

try {

if (rs != null) {

rs.close();

}

if (psmt != null) {

psmt.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException ex) {

System.out.println(ex.getMessage());

}

}

第3个实体用户类:User.java

package edu.systop.text.model.entity;

public class User {

private String loginName;

private String userName;

private String passWord;

private int age;

private String address;

public User() {

}

public void setLoginName(String loginName) {

this.loginName = loginName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public void setPassWord(String passWord) {

this.passWord = passWord;

}

public void setAge(int age) {

this.age = age;

}

public void setAddress(String address) {

this.address = address;

}

public String getLoginName() {

return loginName;

}

public String getUserName() {

return userName;

}

public String getPassWord() {

return passWord;

}

public int getAge() {

return age;

}

public String getAddress() {

return address;

}

}

然后编写控制层(C):GetInfoServlet.java

package edu.systop.text.control;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import edu.systop.text.model.entity.User;

import edu.systop.text.model.service.UserManager;

public class GetInfoServlet extends HttpServlet {

private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables

public void init() throws ServletException {

}

//Process the HTTP Get request

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

}

//Process the HTTP Post request

public void doPost(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

String loginName = request.getParameter("loginName");

String passWord = request.getParameter("passWord");

User u = new User();

u.setLoginName(loginName);

u.setPassWord(passWord);

UserManager m = new UserManager();

RequestDispatcher d;

if (m.checkUser(u)) {

User o = m.checkBC(u);

request.setAttribute("JavaBEAN",o);

d = request.getRequestDispatcher("GetInfoUser.jsp");

} else {

d = request.getRequestDispatcher("GetInfoFinale.jsp");

}

d.forward(request, response);

}

//Clean up resources

public void destroy() {

}

}

最后,创建表示层(V):包括3个Jsp(登录页面GetInfo.jsp、登录成功页面GetInfoUser.jsp、登录失败页面GetInfoFinale.jsp)

上面的就是Jsp结合Servlet用MVC架构写的用户登录程序。

用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程序编写一个简单的登录界面怎么写?

程序如下:

mport java.awt.HeadlessException;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

@SuppressWarnings("serial")

public class MainFrame extends JFrame {

JLabel lbl1 = new JLabel("用户名:");

JLabel lbl2 = new JLabel("密     码:");

JTextField txt = new JTextField("admin",20);

JPasswordField pwd = new JPasswordField(20);

JButton btn = new JButton("登录");

JPanel pnl = new JPanel();

private int error = 0;

public MainFrame(String title) throws HeadlessException {

super(title);

init();

}

private void init() {

this.setResizable(false);

pwd.setEchoChar('*');

pnl.add(lbl1);

pnl.add(txt);

btn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if ("admin".equal花憨羔窖薏忌割媳公颅s(new String(pwd.getPassword()))){

pnl.removeAll();

JLabel lbl3 = new JLabel();

ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));

lbl3.setIcon(icon);

pnl.add(lbl3);

}

else{

if(error 3){

JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");

error++;

}

else{

JOptionPane.showMessageDialog(null,"对不起,您不是合法用户");

txt.setEnabled(false);

pwd.setEnabled(false);

btn.setEnabled(false);

}

}

}

});

}

public static void main(String[] args) {

MainFrame frm = new MainFrame("测试");

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setBounds(100, 100, 300, 120);

frm.setVisible(true);

}

}

编程的注意事项:

1、Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

2、 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

3、2006年11月13日,Java技术的发明者Sun公司宣布,将Java技术作为免费软件对外发布。Sun公司正式发布的有关Java平台标准版的第一批源代码,以及Java迷你版的可执行源代码。从2007年3月起,全世界所有的开发人员均可对Java源代码进行修改。

java完成系统登录程序

package com.hg;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int failCount = 0;

String userName = "";

String pwd = "";

while(true){

while(true){

System.out.print("请输入用户名:");

scanner = new Scanner(System.in);

userName = scanner.nextLine();

if(!"".equals(userName)){

break;

}

}

while(true){

System.out.print("请输入密码:");

scanner = new Scanner(System.in);

pwd = scanner.nextLine();

if(!"".equals(pwd)){

break;

}

}

if("mldn".endsWith(userName) "hello".endsWith(pwd)){

System.out.println("登录成功!");

break;

}else{

System.out.println("用户名或密码错误!");

failCount++;

}

if(failCount = 3){

System.out.println("您已经连续3次输入错误。系统退出。");

break;

}

}

}

}

模拟一个简单的用户登陆程序,判断登录的用户名和密码是否正确,输出登录判断结果,用java怎么写?

package test;\x0d\x0aimport java.util.Scanner;\x0d\x0apublic class TestLogin {\x0d\x0aprivate static final String USERNAME = "Tom";//此处定义用户名\x0d\x0aprivate static final String PASSWORD = "123";//定义密码\x0d\x0apublic static void main(String[] args) {\x0d\x0aScanner sc = new Scanner(System.in);\x0d\x0aSystem.out.print("请输入您的用户名:");\x0d\x0aString username_in = sc.next();\x0d\x0aSystem.out.print("请输入您的密码:");\x0d\x0aString password_in = sc.next();\x0d\x0aif(username_in.equals(USERNAME) password_in.equals(PASSWORD)){\x0d\x0aSystem.out.println("登录成功");\x0d\x0a}else{\x0d\x0aSystem.out.println("用户名或密码错误");\x0d\x0a}\x0d\x0a}\x0d\x0a} \x0d\x0a这是从控制台输入的最简单的方法。

java登录程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java登录界面代码简单、java登录程序的信息别忘了在本站进行查找喔。