
正文
java模拟用户注册代码 java模拟用户注册代码是什么
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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);
}
}
相关问答
Q1: 用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注
所有功能均已实现,如有不满意的地方我再修改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Login extends JPanel
{
//声明各个控件
private JLabel user_name_label = null;
private JLabel password_label = null;
private JTextField user_name_text = null;
private JTextField password_text = null;
private JButton login = null;
private JButton regist = null;
//声明文件用以保存注册信息
private final String file_name = "注册.txt";
public Login()
{
//获得各个控件并且为之设置显示文本
user_name_label = new JLabel();
user_name_label.setText("姓名:");
password_label = new JLabel();
password_label.setText("密码:");
user_name_text = new JTextField();
password_text = new JTextField();
login = new JButton();
login.setText("登录");
regist = new JButton();
regist.setText("注册");
//设置面板的布局为网格布局
setLayout(new GridLayout(3,2));
//将控件添加到面板里
add(user_name_label);
add(user_name_text);
add(password_label);
add(password_text);
add(login);
add(regist);
//为两个按钮添加监听
regist.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String str = null;
String[] result = null;
try
{
if((name.length() == 0)(password.length() == 0))
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
else if(name.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
else if(password.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}
InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);
while((str = buffered_reader.readLine()) != null)
{
result = str.split(" ");
if(result[0].equals(name))
{
int a = JOptionPane.showConfirmDialog(null,"该用户已存在,请重新注册","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
}
OutputStream out = new FileOutputStream(file_name,true);
OutputStreamWriter writer = new OutputStreamWriter(out);
BufferedWriter buffered_writer = new BufferedWriter(writer);
buffered_writer.write(name+" "+password);
buffered_writer.newLine();
buffered_writer.close();
int a = JOptionPane.showConfirmDialog(null,"恭喜你,注册成功!","确认对话框",JOptionPane.YES_NO_OPTION);
}
catch(Exception e1)
{
}
}
});
login.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name = user_name_text.getText().toString();
String password = password_text.getText().toString();
String result = null;
try
{
if((name.length() == 0)(password.length() == 0))
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名和密码","确认对话框",JOptionPane.YES_NO_OPTION);
throw new Exception("");
}
else if(name.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入用户名","确认对话框",JOptionPane.YES_NO_OPTION);
}
else if(password.length() == 0)
{
int a = JOptionPane.showConfirmDialog(null,"请输入密码","确认对话框",JOptionPane.YES_NO_OPTION);
}
InputStream in = new FileInputStream(file_name);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffered_reader = new BufferedReader(reader);
while((result = buffered_reader.readLine()) != null)
{
if(result.equals(name+" "+password))
{
int a = JOptionPane.showConfirmDialog(null,"登陆成功","确认对话框",JOptionPane.YES_NO_OPTION);
break;
}
}
if(!(result.equals(name+" "+password)))
{
int a = JOptionPane.showConfirmDialog(null,"用户名或密码错误","确认对话框",JOptionPane.YES_NO_OPTION);
}
}
catch(Exception e1)
{
//e1.printStackTrace();
}
}
});
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Login(),BorderLayout.NORTH);
}
}
Q2: java(String类)控制台模拟新用户注册程序
import java.util.*;
public class Yugi {
public static void main(String[] args) {
String reg_email="^([a-z\\d_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
String reg_pass = "^(?!([A-Za-z]+|[A-Z\\d]+|[a-z\\d]+)$)[A-Za-z\\d]+$";
Scanner scan = new Scanner(System.in);
int step = 1;
String pass = "";
while (true) {
if(step == 1) {
System.out.println("输入邮箱: ");
String email = scan.nextLine();
if (!email.matches(reg_email)) {
continue;
} else {
step = 2;
}
}else if(step == 2){
System.out.println("输入密码: ");
pass = scan.nextLine();
if(!pass.matches(reg_pass)){
continue;
}else{
step = 3;
}
}else if(step == 3){
System.out.println("再次输入密码: ");
String repass = scan.nextLine();
if(!repass.equals(pass)){
continue;
}else{
System.out.println("注册成功");
scan.close();
break;
}
}
}
}
}
java模拟用户注册代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java模拟用户注册代码是什么、java模拟用户注册代码的信息别忘了在本站进行查找喔。






