本文目錄一覽:
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 = “”; // 定義用戶名和密碼
將該變數等於為全局變數 或局部變數即可
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/153085.html