用户登陆框以及身份验证源代码:import java.lang.*;
import java.awt.*;public class j2005220
{
        private static j2005220 Application;        myFrame1 Form1;
        myFrame2 Form2;
        myFrame3 Form3;
        public static void main(String [] args)
        {
                Application=new j2005220();
                Application.Form1=new myFrame1(Application);
                Application.Form2=new myFrame2(Application);                Application.Form1.setVisible(true);
        }}import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class myFrame1 extends Frame
{
        private j2005220 Application;
        private myFrame1 Form1;
        private myFrame2 Form2;
        private myFrame3 Form3;        Button B1;
        Label L1;
        Label L2;
        TextField T1;
        TextField T2;
        public myFrame1(j2005220 app)
        {
                Application=app;
                Form1=this;                this.setLayout(null);
                B1 = new Button("OK");
                B1.setBounds(80,160,70,30);
                B1.setBackground(Color.pink);
                B1.addMouseListener(new myMouseListener());
                this.add(B1);
                L1=new Label("用户ID:");
                L1.setBounds(20,80,50,30);
                L1.setBackground(Color.orange);
                this.add(L1);
                L2=new Label("用户密码:");
                L2.setBounds(20,120,60,30);
                L2.setBackground(Color.orange);
                this.add(L2);
                T1=new TextField("");
                T1.setBounds(80,80,130,30);
                this.add(T1);
                T2=new TextField("");
                T2.setBounds(80,120,130,30);
                T2.setEchoChar('*');
                this.add(T2);
                this.setBackground(Color.orange);
                this.setBounds(200,100,250,220);
                this.addWindowListener(new closej2005220());
                this.setVisible(true);
        }
        class closej2005220 extends WindowAdapter
        {
                public void windowOpened(WindowEvent e)
                {
                        Form2=Application.Form2;                }
                public void windowClosing(WindowEvent e)                {
                        JOptionPane.showMessageDialog(null,"您确定要退出本程序吗?");                        System.exit(0);
                }        }
                class myMouseListener extends MouseAdapter
        {

解决方案 »

  1.   

    昨天刚做了个验证用户登陆的servlet,测试成功。
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
    import javax.servlet.ServletConfig;import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import java.sql.SQLException;import java.io.IOException;
    import java.io.PrintWriter;class sqlResult{
    private Connection con = null;
    private Statement state = null;
    private ResultSet rs = null;
    private String username = "";
    private String password = "";

    public sqlResult(String un,String pw){
    this.username = un;
    this.password = pw;
    }

    public Connection getConnection(){
           try{
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    con = DriverManager.getConnection   ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xjj","sa","");
    }
               catch(ClassNotFoundException e){
          System.out.println("注册失败");

    catch(SQLException e){
               System.out.println("连接失败");
    }
    return con;
    }

    public ResultSet getResultSet(Connection con){
           try{
    state = con.createStatement();

    }
    catch(SQLException e){                        System.out.println("连接失败");
    }

    try{
    String sql = "select * from members where Username='" +   username + "' and Password='"+password+"'";
      
    rs = state.executeQuery(sql); }
    catch(SQLException e){
    System.out.println("结果失败");
    }

    return rs;
    }


    public boolean isCorrect(ResultSet rs){
    try{
    if(rs.next())
     return true;
    else
    return false;  
    }
    catch(SQLException e){
    e.printStackTrace();
    return false;
    }



    }

    public void close(){

    try{ 
    if(rs != null)
    rs.close();
    if(state != null)
    state.close();
    if(con != null)
    con.close();
    }
    catch(SQLException e){
    System.out.println("关闭出错");
    }
    }
    }public class loginInfo extends HttpServlet{
    private String username = "";
    private String password = "";

    public void init(ServletConfig config)throws ServletException{
    super.init(config);
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    PrintWriter out = response.getWriter();
    username = request.getParameter("username");
    password = request.getParameter("password");
    sqlResult sql_result = new sqlResult(username,password);
    Connection con = sql_result.getConnection();
    ResultSet rs = sql_result.getResultSet(con);
    if(sql_result.isCorrect(rs)){   //若返回是TRUE,表示通过验证
    response.setContentType("text/html;charset=gb2312");
    out.println("<html>");
    out.println("<head><title>This is the first servlet</title></head>");
    out.println("<body><center><h1>");
    out.println("Welcome To This System");
    out.println("</h1><br>");
    out.println("<h3>Username:"+username+"<br></h3></center></body></html>");
    }
    else {
    response.setContentType("text/html;charset=gb2312");
    out.println("<html>");
    out.println("<head><title>This is the first servlet</title></head>");
    out.println("<body><center><h1>");
    out.println("WrongInformation");
    out.println("</h1></center></body></html>");
    }

    sql_result.close();
    }


     

    }