String username = "",password = "",passwordagain = ""; // 定義用戶(hù)名和密碼
成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供茶陵企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為茶陵眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
將該變量等于為全局變量 或局部變量即可
//這個(gè)是我寫(xiě)的,里面有連接數(shù)據(jù)庫(kù)的部分。你可以拿去參考一下
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("用戶(hù)名:");
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數(shù)據(jù)庫(kù)
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 數(shù)據(jù)庫(kù)名為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是存放用戶(hù)名和密碼的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陸成功!");
} else
JOptionPane.showMessageDialog(null, "用戶(hù)名或密碼錯(cuò)誤!");
} 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寫(xiě)提示注冊(cè)成功的方法如下:
1、首先用戶(hù)注冊(cè)完成后,返回一個(gè)boolean值的變量;
2、利用Servlet類(lèi)判斷這個(gè)變量,如果為true,跳轉(zhuǎn)到提示界面,提示用戶(hù)注冊(cè)成功,如果為false,跳轉(zhuǎn)到提示界面,提示用戶(hù)注冊(cè)失??;
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);//注冊(cè)方法
if(flag){
//提示注冊(cè)成功
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}else{
//提示注冊(cè)失敗
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}
}
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
doGet(request,?response);
}
}
4、至此,就完成了提示注冊(cè)成功的功能。