① 用java做了一個簡單的登陸窗口,已經連上資料庫,如何用資料庫驗證登陸的用戶名和密碼
public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==Enter)
  {
   String username , password;
   username = name.getText();
      password = pwd.getText();
   try
   {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   }
   catch (ClassNotFoundException ce)
   {
    JOptionPane.showMessageDialog(ss,ce.getMessage());
   }
   if(stu.isSelected())
   {
    try
       {
     Connection con = DriverManager.getConnection("jdbc:odbc:DS_121301_12","sa","");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from stu where ID='"+username+"' and Pwd='"+password+"'");
        while(rs.next())
        {
         if((rs.getString("ID").equals(username))&&(rs.getString("Pwd").equals(password)))
         {        
       JOptionPane.showMessageDialog(ss,"登陸成功");
       Students stu = new Students();
         }
        
        else
      {
       JOptionPane.showMessageDialog(ss,"登錄失敗");
      }
       
        }
② 用JAVA編寫一個用戶登陸界面,用戶驗證通過資料庫實現
這是我以前寫的系統登錄框的login
package java1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Rectangle;
import java.awt.Font;
public class login  extends JFrame
{
  public login() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public static void main(String[] args)
  {
    login frm=new login();
    frm.setBounds(300,200,300,260);
    frm.setVisible(true);
  }
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    jLabel1.setForeground(Color.red);
    jLabel1.setText("用戶名");
    jLabel1.setBounds(new Rectangle(25, 72, 68, 29));
    jButton2.setBounds(new Rectangle(142, 159, 81, 30));
    jButton2.setText("取消");
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton2_actionPerformed(e);
            }
        });
        jButton1.setBounds(new Rectangle(38, 159, 81, 29));
    jButton1.setText("確定");
    jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jTextField1.setBounds(new Rectangle(109, 79, 136, 23));
    jPasswordField1.setBounds(new Rectangle(109, 115, 136, 23));
    this.getContentPane().add(jLabel1);
    jLabel3.setFont(new java.awt.Font("隸書", Font.PLAIN, 24));
    jLabel3.setText("學  生  考  試  系  統");
    jLabel3.setBounds(new Rectangle(22, 15, 268, 47));
    this.getContentPane().add(jTextField1);
    this.getContentPane().add(jLabel2);
    this.getContentPane().add(jButton2);
    this.getContentPane().add(jButton1);
    this.getContentPane().add(jLabel3);
    this.getContentPane().add(jPasswordField1);
    jLabel2.setForeground(Color.red);
    jLabel2.setText("密碼");
    jLabel2.setBounds(new Rectangle(26, 112, 68, 29));
  }
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JTextField jTextField1 = new JTextField();
    JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLabel3 = new JLabel();
    JPasswordField jPasswordField1 = new JPasswordField();
    main2 wo=new main2();
    int sum;
    
    //判斷許可權的方法
    public void success()
    {
     try{
      ResultSet rs1;
      String s1="select * from 管理許可權 where name='"+jTextField1.getText()+"' and password='"+jPasswordField1.getText()+"'";                                  
      rs1=sqlcx.Rs_jiluji(s1);
      rs1.first();
      sum=Integer.parseInt((String)(rs1.getString(3)));
      System.out.println(sum);
      very();
      }
     catch(Exception c){System.out.println("success error");}
    }
//根據許可權設置窗口
    public void very()
   {
    if (sum==1)
    {JOptionPane.showOptionDialog(this, "您現在是以教師許可權登陸", "登陸信息"
                                         , JOptionPane.DEFAULT_OPTION,
                                         JOptionPane.ERROR_MESSAGE,
                                         null, null, null);
     
     }
    else
    {
     JOptionPane.showOptionDialog(this, "你現在是以學生許可權登陸", "登陸信息"
                                         , JOptionPane.DEFAULT_OPTION,
                                         JOptionPane.ERROR_MESSAGE,
                                         null, null, null);
       wo.jButton2.setVisible(false);
       wo.jButton3.setVisible(false);
       wo.jButton4.setVisible(false);
       wo.jButton5.setVisible(false);
       wo.jButton6.setVisible(false);
       wo.jButton7.setVisible(false);
       wo.jButton8.setVisible(false);
       wo.jMenu2.setVisible(false);
       wo.jMenu3.setVisible(false);
       wo.jMenu4.setVisible(false);
    }
    
    }
public void jButton1_actionPerformed(ActionEvent e)
    {
        //判斷用戶名是否為空
        if (jTextField1.getText().length() == 0) {
            JOptionPane.showOptionDialog(this, "用戶名不能為空", "錯誤信息"
                                         , JOptionPane.DEFAULT_OPTION,
                                         JOptionPane.ERROR_MESSAGE,
                                         null, null, null);
        }
    //判斷密碼是否為空
       else if (jPasswordField1.getText().length()==0)
        {
            JOptionPane.showOptionDialog(this,"密碼不能為空", "錯誤信息"
                                   , JOptionPane.DEFAULT_OPTION,
                                   JOptionPane.ERROR_MESSAGE,
                                   null, null, null);
       }
       else
       {
          try
          {
              ResultSet rs;//聲明記錄集
              String sql="select * from 管理許可權 where name='"+jTextField1.getText()+"'";
               rs = sqlcx.Rs_jiluji(sql);
              if (rs.next())
              {
                  if(jPasswordField1.getText().equals(rs.getString(2)))
                  {
                      
                      success();
                      wo.setBounds(60,40,700,600);
                      wo.setVisible(true);
                      this.dispose();
                 }
                 else
                 {
                     JOptionPane.showOptionDialog(this,"密碼錯誤", "錯誤信息"
                                       , JOptionPane.DEFAULT_OPTION,
                                       JOptionPane.ERROR_MESSAGE,
                                       null, null, null);
                 }
              }
              else
              {
                  JOptionPane.showOptionDialog(this,"沒有所要找的用戶名", "錯誤信息"
                                       , JOptionPane.DEFAULT_OPTION,
                                       JOptionPane.ERROR_MESSAGE,
                                       null, null, null);
              }
}
          catch(Exception c){
              JOptionPane.showOptionDialog(this,"連接資料庫失敗", "錯誤信息"
                                       , JOptionPane.DEFAULT_OPTION,
                                       JOptionPane.ERROR_MESSAGE,
                                       null, null, null);
                             }
       }
    }
    public void jButton2_actionPerformed(ActionEvent e) {
    System.exit(0);
    }
}
③ java 如何實現同一賬戶登錄驗證
今天繼續討論?-0-#這個只需要session和application就好了,用戶登錄時,這樣寫:User
user
=
.login(userName,
password);//
資料庫中判斷用戶名和密碼if
(null
!=
user)
{//
表示用戶存在
session.setAttribute("user",
user);//
把用戶放進session中
application.setAttribute(userName,
session.getId());/*
把用戶所在的sessionId放進application中,首先要明白一點,一個session對應一個瀏覽器,其次要注意一點,userName必須是唯一的*/}當用戶訪問到其他url的時候,可以在過濾器或你的攔截器中這樣寫:User
user
=
(User)
session.getAttribute("user");//
從session中取出用戶if
(null
==
user)
{//
未登錄或者登錄已經過期
response.sendRedirect(request.getContextPath());//
跳轉到首頁或登錄頁面}String
sessionId
=
(String)
application.getAttribute(user.getUserName());if
(null
==
sessionId
||
!sessionId.equals(session.getId()))
{/*這說明用戶已經在其他電腦或其它瀏覽器登錄了,那麼之前登錄的session就無效了,自動被後面的登錄給踢掉*/
response.sendRedirect(request.getContextPath());//
跳轉到首頁或登錄頁面}chain.doFilter(request,
response);//
通過驗證,放行用戶進入目標url這種方式是我的一個前輩想到的,我們公司的所有項目都採納了這種方式,確保一個賬號只能在一個瀏覽器中使用
④ java鏈接mysql資料庫實現登陸如何驗證
//這是我以前寫的核對資料庫實現登陸的方法,你只看jdbc部分就好,我還特地給你加了點注釋
String sql = "select username,password from account";
		String user = request.getParameter("user");
		String pass = request.getParameter("password");
		int j = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JDBCTools1.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
//從表中查詢獲取所有賬戶的用戶名&密碼的ResultSet 對象
			while(rs.next()){
				int i = 0;
				
				String username[] = new String[10];//用戶名數組
				String password[] = new String[10];//密碼數組
				username[i] = rs.getString(1);
				password[i] = rs.getString(2);
				if(user.equals(username[i])&&pass.equals(password[i])){//比對
					response.getWriter().print("you are welcome!");
					j++;
				}else if(user.equals(username[i])&&!pass.equals(password[i])){
					response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");
					response.getWriter().println("and you password is   :"+user +","+pass+" :so the username or password may not right");
					j++;
				}else{
					continue;
				}
				i++;
			}
			if(j == 0){
				response.getWriter().println("Your username may not be properly");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCTools1.release(rs, ps, conn);
		}
//這是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver;
		String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;
//你的資料庫的用戶名密碼
		String user = null;
		String password = null;
		// 通過反射創建Driver對象
		Class.forName(driverClass);
		return DriverManager.getConnection(jdbcUrl, user, password);}
//這是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
			Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
⑤ java和資料庫連接,實現登錄功能
//驗證用戶和密碼
@Override
publicintqueryUser(UserVovo){
//TODOAuto-generatedmethodstub
Connectionconn=null;
PreparedStatementstmt=null;
ResultSetrs=null;
Stringsql="selectcount(*)fromuser_infowhereusername=?andpassword=?";
intcount=0;
try{
//獲取連接對像
conn=ConnTools.getConnection();
//獲取stmt
stmt=conn.prepareStatement(sql);
//設置動態參數
stmt.setString(1,vo.getUsername());
stmt.setString(2,vo.getPassword());
//執行查詢
rs=stmt.executeQuery();
if(rs.next()){
count=rs.getInt(1);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//釋放資源
ConnTools.close(conn,stmt,rs);
}
returncount;
}
⑥ java連接資料庫怎麼驗證用戶名和密碼
1,當你建一個資料庫:是有用戶名與密碼的。
2,java中,通過建立一個類,比如說:databaseconnector,用於與資料庫進行連接。
3,類建好後,如果你用的是mysql資料庫的話,還需要再java中載入相應的jar包(mysql-connector-java-3.0.jar),其他資料庫也需要載入相應的包。
4.用戶名與密碼,放於資料庫中一個表中,當用戶登錄時,在資料庫該表中查找是否有用戶名與密碼與所給相同的欄位。如有,登錄成功,跳轉到相應頁面。如無,登錄失敗。
⑦ java 如何用資料庫中信息登錄資料庫
打開microsoft
sql
server
managerment
studio並以windows驗證方式登錄,左側的對象資源管理器->安全性->登錄名,右擊sa->屬性,為sa用戶添加密碼,選擇sqlserver身份驗證,在"狀態"選項中授予連接到資料庫和登錄啟用.
右擊對象資源管理器的根節點,選擇屬性->安全性->sqlserver和windows身份驗證模式,這樣就為sql
server
2008創建了以sql
server身份驗證的用戶sa.
在java代碼中用兩種方式連接sqlserver2008資料庫,一種是sa身份驗證模式,另外一種是混合身份驗證模式
⑧ java登錄查詢資料庫驗證
調用你已經創建專門的連接資料庫的類和返回查詢數據的類的方法獲取資料庫連接 然後執行sql就可以了
⑨ JAVA驗證登陸資料庫
目測了一下,查詢語句裡面有一個中文的逗號,拼串的時候沒有單引號,而且你這種方式查詢可以直接返回rs.next(),而且這個查詢方法不應該try catch,應該拋出異常
