本文目录一览:
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写提示注册成功的方法如下:
1、首先用户注册完成后,返回一个boolean值的变量;
2、利用Servlet类判断这个变量,如果为true,跳转到提示界面,提示用户注册成功,如果为false,跳转到提示界面,提示用户注册失败;
3、具体代码如下所示:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String usepwd= request.getParameter("usepwd");
boolean flag = Dao.register(username,usepwd);//注册方法
if(flag){
//提示注册成功
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
//提示注册失败
request.getRequestDispatcher("/success.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4、至此,就完成了提示注册成功的功能。
用java编程实现用户注册并进行登录操作
String username = "",password = "",passwordagain = ""; // 定义用户名和密码
将该变量等于为全局变量 或局部变量即可