本文目录一览:
怎么用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怎样实现登录验证
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怎么实现QQ登录界面?
用java做QQ登录界面的写法如下:
package ch10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1、//定义该类继承自JFrame,实现ActionListener接口
public class LoginTest extends JFrame implements ActionListener
{
2、//创建JPanel对象
private JPanel jp=new JPanel();
3、//创建3个标并加入数组
JLabel name = new JLabel("请输入用户名");
JLabel password = new JLabel("请输入密码");
JLabel show = new JLabel("");
private JLabel[] jl={name,password,show};
4、//创建登陆和重置按扭并加入数组
JButton login = new JButton("登陆");
JButton reset = new JButton("重置");
private JButton[] jb={login,reset};
5、//创建文本框以及密码框
private JTextField jName=new JTextField();
private JPasswordField jPassword =new JPasswordField();
public LoginTest()
{
6、//设置布局管理器为空布局,这里自己摆放按钮、标签和文本框
jp.setLayout(null);
for(int i=0;i2;i++)
{
7、//设置标签和按扭的位置与大小
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
8、//添加标签和按扭到JPanel容器中
jp.add(jl[i]);
jp.add(jb[i]);
//为2个按钮注册动作事件监听器
jb[i].addActionListener(this);
}
9、//设置文本框的位置和大小,注意满足美观并足够用户名的长度
jName.setBounds(130,15,100,20);
10、//添加文本框到JPanel容器中
jp.add(jName);
11、//为文本框注册动作事件监听器
jName.addActionListener(this);
12、//设置密码框的位置和大小,注意满足美观和足够密码的长度
jPassword.setBounds(130,60,100,20);
13、//添加密码框到JPanel容器中
jp.add(jPassword);
14、//设置密码框中的回显字符,这里设置美元符号
jPassword.setEchoChar('$');
15、//为密码框注册动作事件监听器
jPassword.addActionListener(this);
16、//设置用于显示登陆状态的标签大小位置,并将其添加进JPanel容器
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
17、//添加JPanel容器到窗体中
this.add(jp);
18、//设置窗体的标题、位置、大小、可见性及关闭动作
this.setTitle("登陆窗口");
this.setBounds(200,200,270,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
19、//实现动作监听器接口中的方法actionPerformed
public void actionPerformed(ActionEvent e)
{
20、//如果事件源为文本框
if(e.getSource()==jName)
{
21、//切换输入焦点到密码框
jPassword.requestFocus();
}
22、//如果事件源为重置按扭
else if(e.getSource()==jb[1])
{
23、//清空姓名文本框、密码框和show标签中的所有信息
jl[2].setText("");
jName.setText("");
jPassword.setText("");
24、//让输入焦点回到文本框
jName.requestFocus();
}
25、//如果事件源为登陆按钮,则判断登录名和密码是否正确
else
{
26、//判断用户名和密码是否匹配
if(jName.getText().equals("lixiangguo")
String.valueOf(jPassword.getPassword()).equals("19801001"))
{
27、jl[2].setText("登陆成功,欢迎您的到来!");
}
else
{
28、jl[2].setText("对不起,您的用户名或密码错误!");
}
}
}
public static void main(String[] args)
{
29、//创建LoginTest窗体对象
new LoginTest();
}
}
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.setDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
如何过java登陆器
这样过java登陆器:通过用户名将密码注释,比如:admin'--,或者传说的万能密码等等,不过现在打都是用的框架处理,或者防SQL注入很到位。