MySQL_(Java)使用JDBC建立用戶名和密碼校驗查詢方法

 

 

  MySQL_(Java)使用JDBC向數據庫發起查詢請求  傳送門html

 

  MySQL數據庫中的數據,數據庫名garysql,表名garytb,數據庫中存在的用戶表java

  

 

  經過JDBC對MySQL中的數據用戶名和密碼校驗查詢,當數據庫中存在該用戶且帳號密碼相匹配,則返回true,不然返回falsemysql

 

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC01 {

    public static void main(String[] args) throws SQLException  {
        //selectAll();
        System.out.println(selectByUernamePassword("Gary","123"));
    }

    public static void selectAll() throws SQLException {
        //註冊驅動    使用驅動鏈接數據庫
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            //String url ="jdbc:mysql://localhost:3306/garysql";
            //指定編碼查詢數據庫
            String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
            String user = "root";
            String password = "123456";
            //創建和數據庫的鏈接
            con = DriverManager.getConnection(url,user,password);
            
            //數據庫的增刪改查
            stmt = con.createStatement();
            //返回一個結果集
            rs =stmt.executeQuery("select * from garytb");
            
            while(rs.next()) {
                //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
                System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
            }
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
    }

    public static boolean  selectByUernamePassword(String username,String password) throws SQLException {
        Connection con=null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
            con = DriverManager.getConnection(url,"root","123456");
            stmt =con.createStatement();
            String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                return true;
            }else {
                return false;
            }
                
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
        
        return false;
    }
    
}
JDBC01.java

 

  注意:直接使用該字符串對數據庫信息查詢時會產生sql注入的危險sql

String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";

 

//校驗用戶
    public static boolean  selectByUernamePassword(String username,String password) throws SQLException {
        Connection con=null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            
            String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
            con = DriverManager.getConnection(url,"root","123456");
            stmt =con.createStatement();
            String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
            //System.out.println(sql);
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                return true;
            }else {
                return false;
            }
                
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
        
        return false;
    }

 

 

  咱們能夠在selectByUernamePassword(String username,String password)方法中輸出該SQL語句數據庫

 

  發現or後半段的條件永遠是成立的!ide

  解決此方法可查看個人另外一篇博文  使用preparestatement解決SQL注入的問題  傳送門編碼

相關文章
相關標籤/搜索