找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1277|回復(fù): 0
收起左側(cè)

java JSP數(shù)據(jù)庫核心技術(shù)實(shí)驗(yàn)

[復(fù)制鏈接]
ID:1030622 發(fā)表于 2022-5-29 11:55 | 顯示全部樓層 |閱讀模式
實(shí)驗(yàn)內(nèi)容:利用Statement演示SQL
實(shí)驗(yàn)要求:
1.    =演示用戶登錄功能;
2.    用戶輸入用戶名,如tom;用戶輸入密碼,如123,成功登錄,顯示success!;否則登錄不成功,顯示error!
3.    演示SQL,用戶名輸入:jack ‘ or 1=1 -- ,密碼輸入:345,成功登錄,顯示success!
51hei.png
【貼代碼】
package com;
import java.sql.*;
import java.util.Scanner;
public class Jdbc {
    private final String URL="jdbc:mysql://localhost:3306/mon";
    private final String USER="root";
    private final String PWD="1234";
   
    public void login() {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(URL,USER,PWD);
            stmt=con.createStatement();
            
            Scanner input = new Scanner(System.in);
            System.out.println("input user:");
            String u=input.nextLine();
            System.out.println("input pwd:");
            String p=input.nextLine();
            String sql="select count(*) from login where user='"+u+"' and pwd='"+p+"'";
            rs=stmt.executeQuery(sql);
            int c=-1;
            if(rs.next()) {
                c=rs.getInt(1);               
            }
            if(c>0) {
                System.out.println("success!");
            } else {
                System.out.println("error!");
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs!=null) rs.close();
                if(stmt!=null) rs.close();
                if(con!=null) rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }                        
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Jdbc b= new Jdbc();
        b.login();
    }
}

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表