我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java登陆流程代码 java登录流程

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

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

宁晋ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

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写的用户登陆实例,用eclipse开发的具体步奏和代码

import java.awt.Container;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;import javax.swing.*;public class Login extends JFrame{

JLabel user,passwd;

JTextField userput;

JPasswordField passput;

JButton denglu,tuichu;

public Login(){

super("用户登录");

Container c=getContentPane();

c.setLayout(null);

Font f=new Font("宋体",Font.PLAIN,12);

user=new JLabel("账号");

passwd=new JLabel("密码");

userput=new JTextField();

passput=new JPasswordField();

denglu=new JButton("登录");

denglu.setFont(f);

denglu.addActionListener(new NewAction());

tuichu=new JButton("退出");

tuichu.setFont(f);

tuichu.addActionListener(new NewAction());

user.setBounds(50,50,60,20);

userput.setBounds(110,50,150,20);

passwd.setBounds(50,80,60,20);

passput.setBounds(110,80,150,20);

denglu.setBounds(50,160,60,30);

tuichu.setBounds(200,160,60,30);

c.add(user);

c.add(userput);

c.add(passwd);

c.add(passput);

c.add(denglu);

c.add(tuichu);

setSize(350, 300);

setVisible(true);

}

class NewAction implements ActionListener{

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CDM";

String user="sa";

String passwd="394513265";

java.sql.Connection con;

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

con= DriverManager.getConnection(url,user,passwd);

}catch(Exception ep){

JOptionPane.showMessageDialog(null, "加载驱动失败!");

}

if(e.getSource()==denglu){

Find();

}

if(e.getSource()==tuichu){

dispose();

}

} public void Find(){

String lk="select * from login";

try{

Statement sql=con.createStatement();

ResultSet rs=sql.executeQuery(lk);

while(rs.next()){

if(rs.getString(1).equals(userput.getText()) rs.getString(2).equals(passput.getText()))

new MainClient();

else

JOptionPane.showMessageDialog(null, "用户名或密码错误");

}

rs.close();

}catch(SQLException p){

JOptionPane.showMessageDialog(null, p.getMessage());

}

}

}

public static void main(String[] args) {

Login l=new Login();

l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

步骤就是建个工程 然后建个class

java怎样实现登录验证

1.打开编程工具:

打开java编程的界面,采用的是eclipse软件;

2

2.建立一个java工程:

简洁操作如下:单击“file”-“new”-“java project”;

然后,在工程菜单中选中工程,单击鼠标右键出出来菜单,依次选中“new”-“class”;

具体查看“ eclipse如何建立一个java工程”;

0eclipse如何建立一个java工程

END

2.代码实现步骤

1.建立输入扫描:

采用java中的Scanner类实现输入数据的获取,具体代码如下;

Scanner scan = new Scanner(System.in);

2.接收用户名:

建立一个提示信息,提示输入用户名,并储存输入的用户名,代码如下:

System.out.println("请输入登陆用户名:");

String usename=scan.nextLine();

3.接收密码:

建立一个提示信息,提示输入密码,并存储输入的密码,代码如下:

System.out.println("请输入登陆的密码:");

String password=scan.nextLine();

4.验证信息:

采用if else语句来实现对用户名和密码的验证,并打印提示信息,代码如下:

if(!usename.equals("me")){

System.out.println("用户名非法。");

}else if(!password.equals("123456")){

System.out.println("登陆密码错误。");

}else{

System.out.println("恭喜您,登陆信息通过验证。");

}

登陆界面的java代码怎么写?

import java.awt.*; \x0d\x0aimport javax.swing.*; \x0d\x0aimport java.awt.event.*; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0aclass LoginFrm extends JFrame implements ActionListener \x0d\x0a{ \x0d\x0aJLabel lbl1=new JLabel("用户名"); \x0d\x0aJLabel lbl2=new JLabel("密码"); \x0d\x0aJTextField txt=new JTextField(15); \x0d\x0aJPasswordField pf=new JPasswordField(); \x0d\x0aJButton btn1=new JButton("确定"); \x0d\x0aJButton btn2=new JButton("取消"); \x0d\x0a\x0d\x0apublic LoginFrm() \x0d\x0a{ \x0d\x0athis.setTitle("登陆"); \x0d\x0aJPanel jp=(JPanel)this.getContentPane(); \x0d\x0ajp.setLayout(new GridLayout(3,2,10,10)); \x0d\x0ajp.add(lbl1);jp.add(txt); \x0d\x0ajp.add(lbl2);jp.add(pf); \x0d\x0ajp.add(btn1);jp.add(btn2); \x0d\x0abtn1.addActionListener(this); \x0d\x0abtn2.addActionListener(this); \x0d\x0a} \x0d\x0a\x0d\x0apublic void actionPerformed(ActionEvent ae) \x0d\x0a{ \x0d\x0aif(ae.getSource()==btn1) \x0d\x0a{ \x0d\x0atry \x0d\x0a{ \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); \x0d\x0aConnection con=DriverManager.getConnection("jdbc:odbc:MyDB","",""); \x0d\x0aStatement cmd=con.createStatement(); \x0d\x0aResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'"); \x0d\x0aif(rs.next()) \x0d\x0a{ \x0d\x0aJOptionPane.showMessageDialog(null,"登陆成功!"); \x0d\x0a} \x0d\x0aelse \x0d\x0aJOptionPane.showMessageDialog(null,"用户名或密码错误!"); \x0d\x0a} catch(Exception ex){} \x0d\x0a\x0d\x0aif(ae.getSource()==btn2) \x0d\x0a{ \x0d\x0atxt.setText(""); \x0d\x0apf.setText(""); \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0apublic static void main(String arg[]) \x0d\x0a{ \x0d\x0aJFrame.setDefaultLookAndFeelDecorated(true); \x0d\x0aLoginFrm frm=new LoginFrm(); \x0d\x0afrm.setSize(400,200); \x0d\x0afrm.setVisible(true); \x0d\x0a} \x0d\x0a}


本文题目:java登陆流程代码 java登录流程
文章网址:http://mswzjz.cn/article/dosehpj.html

其他资讯