首页 > 编程语言 > 详细

java学习笔记二 2019.6.27 周四

时间:2019-06-27 23:29:00      阅读:171      评论:0      收藏:0      [点我收藏+]
一、查询数据表select
com.rupeng.jdbctest1;

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

public class Test2 {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    try {
        Class.forName("com.jdbc.mysql.Driver");
    } catch (ClassNotFoundException e) {
        System.err.println("加载jdbc驱动"+e.getMessage());
    }
    Connection conn=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost/mystudy1?seUnicode=true&characterEncoding=UTF-8", "root", "root");
        stmt=conn.prepareStatement("select * from t_persons");
        rs=stmt.executeQuery();
        while(rs.next()){
            int id=rs.getInt("ID");
            String name=rs.getString("name");
            int age=rs.getInt("age");
            String hobbies=rs.getString("hobbies");
            System.out.println("ID:"+id+";"+"姓名:"+name+";"+"年龄:"+age+";"+"hobbies:"+hobbies);
        }
    } catch (SQLException e) {
        // TODO 自动生成的 catch 块
        //e.printStackTrace();
        System.err.println("链接jdbc不成功"+e.getMessage());
    }finally{

    }

}

}

二、sql实现登录和漏洞
package com.rupeng.jdbctest1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test3 {

public static void main(String[] args) {
    // TODO 自动生成的方法存
    Scanner sc=new Scanner(System.in);

    System.out.println("enter name");
    String name=sc.nextLine();
    System.out.println("enter yourpassword");
    String password=sc.nextLine();

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        // TODO 自动生成的 catch 块
        //e.printStackTrace();
        System.out.println("加载jdbc驱动" + e.getMessage());
    }
    Connection conn=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost/mystudy1?seUnicode=true&characterEncoding=UTF-8", "root", "root");
        //String sql="select count(*) c from t_persons2 where name="+name+" and password"+password;
        //System.out.println(sql);
        //select count(*) c from t_persons2 where name=zhaohan and password123
        //缺少单引号
        //stmt=conn.prepareStatement(sql);
        String sql="select count(*) c from t_persons2 where name=‘"+name+"‘ and password‘"+password+"‘";
        System.out.println(sql);
        stmt=conn.prepareStatement("select count(*) c from t_persons2 where name=‘"+name+"‘and password=‘"+password+"‘");
        rs=stmt.executeQuery();
        rs.next();
        int c=rs.getInt("c");
        System.out.println(c);
        if(c<=0){
            System.out.println("失败");
        }else{
            System.out.println("success");
        }

// enter name
// admin
// enter yourpassword
// a‘ or ‘a‘=‘a
// select count(*) c from t_persons2 where name=‘admin‘ and password‘a‘ or ‘a‘=‘a‘
// 1
// success
} catch (SQLException e) {
// TODO 自动生成的 catch 块
//e.printStackTrace();
System.out.println("链接失败" + e.getMessage());
}
}

}

三、改造漏洞
package com.rupeng.jdbctest1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test3 {

public static void main(String[] args) {
    // TODO 自动生成的方法存
    Scanner sc=new Scanner(System.in);

    System.out.println("enter name");
    String name=sc.nextLine();
    System.out.println("enter yourpassword");
    String password=sc.nextLine();

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        // TODO 自动生成的 catch 块
        //e.printStackTrace();
        System.out.println("加载jdbc驱动" + e.getMessage());
    }
    Connection conn=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost/mystudy1?seUnicode=true&characterEncoding=UTF-8", "root", "root");
        //String sql="select count(*) c from t_persons2 where name="+name+" and password"+password;
        //System.out.println(sql);
        //select count(*) c from t_persons2 where name=zhaohan and password123
        //缺少单引号
        //stmt=conn.prepareStatement(sql);

        //String sql="select count(*) c from t_persons2 where name=‘"+name+"‘ and password‘"+password+"‘";
        //System.out.println(sql);
        //stmt=conn.prepareStatement("select count(*) c from t_persons2 where name=‘"+name+"‘and password=‘"+password+"‘");

// enter name
// admin
// enter yourpassword
// a‘ or ‘a‘=‘a
// select count(*) c from t_persons2 where name=‘admin‘ and password‘a‘ or ‘a‘=‘a‘
// 1
// success

        String sql="select count(*) c from t_persons2 where name=?  and password=?";
        stmt=conn.prepareStatement(sql);
        stmt.setString(1, name);
        stmt.setString(2, password);
        rs=stmt.executeQuery();
        rs.next();
        int c=rs.getInt("c");
        System.out.println(c);
        if(c<=0){
            System.out.println("失败");
        }else{
            System.out.println("success");
        }

//
} catch (SQLException e) {
// TODO 自动生成的 catch 块
//e.printStackTrace();
System.out.println("链接失败" + e.getMessage());
}
}

}

java学习笔记二 2019.6.27 周四

原文:https://blog.51cto.com/14394144/2414556

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!