最新Python开发简单爬虫课程项目实战(完整)

create table customer(

    id int PRIMARY KEY auto_increment,
    name varchar (20) not null,
    adress varchar (20) not null

);

create table orders(

    order_num varchar(20) PRIMARY KEY,

    price FLOAT not NULL,

    customer_id int, -- 进行和customer 关联的字段 外键
    constraint cus_ord_fk foreign key (customer_id) REFERENCES customer(id)

);

insert into customer(name,adress) values("zs","北京");

insert into customer(name,adress) values("ls","上海");

SELECT * from customer;

INSERT INTO orders values("010",30.5,1);

INSERT INTO orders values("011",60.5,2);

INSERT INTO orders values("012",120.5,1);

SELECT * from orders;public class TestInsert1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1 加载驱动类
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2 获取数据库连接对象(连接指定的数据库)
        Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","oracle");
        //3 获取sql命令对象(编译和发送sql命令给数据库)
        Statement stmt=conn.createStatement();
        //4 创建sql命令
        String sql="insert into dept values(92,'吃鸡学院','北京')";
        //5 指定sql命令
        int i=stmt.executeUpdate(sql);
        System.out.println("执行结果:"+i);
        //6 关闭资源
            stmt.close();
            conn.close();
    }
}<?php
    include 'global.php';
    
    function AttackFilter($StrKey,$StrValue,$ArrReq){  
        if(is_array($StrValue))
        {
            $StrValue=implode($StrValue);
        }
        if (preg_match("/".$ArrReq."/is",$StrtValue)==1){   
            print "holy shit!";
            exit();
        }      
    } 
    
    $filter = "union|select|from|where|join|sleep|benchmark|,|\(|\)";
    
    foreach($_POST as $key=>$value){ 
        AttackFilter($key,$value,$filter);
    }
    
    if(!isset($_POST['key1']) || !isset($_POST['key2'])) {
        print <<<DBAPP
    
    <img src='image/img.jpg' />
    <!--index.phps-->
    DBAPP;
        die;
    }
    
    $query = mysql_query("SELECT * FROM tb_ctf WHERE key1 = '{$_POST['key1']}'"); 
    if(mysql_num_rows($query) == 1) { 
        $key = mysql_fetch_array($query);
        if($key['key2'] == $_POST['key2']) {
            print $flag;
        }else{
            print "Error!";
            }
    }else{
        print "Error!";
    }
@Service
public class TestLockImpl implements TestLock {
    //总票数
    public static int total = 1000000;
    public static int all = 1000000;
    int count = 0;
   
    @Override
    public synchronized String getTicket()  {
 
        total--;
        try{
        //如果看不出效果,可以加入延迟
        //Thread.sleep(200);
        count++;
        
            }catch (Exception e){}
       // redisLock.unlock(total+"",time+"");
        return "总票数为="+all+"---------"+"剩余票数为"+total+"购买人数为"+count;
    }
@Component
public class RedisLock {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    /**
     * 加锁
     *
     * @param key  票数,即需要加锁的类的total 
     * @param value 当前时间+超时时间
     * @return
     */
    public boolean lock(String key, String value) {
        //相对于redis中的setNX
        if (stringRedisTemplate.opsForValue().setIfAbsent(key, value)) {
            return true;
        }
        //如果上面代码加锁失败,根据key去redis获取value值
        String currentValue = stringRedisTemplate.opsForValue().get(key);
        //如果锁过期,这是为了解决redis死锁的问题,即你用redis进行了锁,但是代码还没运行到解锁,
//程序出现异常,就不会运行到解锁的代码了
        if (!StringUtils.isEmpty(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {//currentValue不为空且小于当前时间
            //锁过期,拿到原来的值,为key设置新的value,即新的过期时间
            //注意,这里是先拿旧的value,再设置新的value
            String oldValue = stringRedisTemplate.opsForValue().getAndSet(key, value);
            //这里是为了解决并发问题,假设锁已经过期,两个线程同时访问到这里时,线程一访问,
//currentValue 和oldValue此时都一样,都为A,当线程B执行时,currentValue 依然为A,但是oldValue 
//已经被改成了B,所以线程二无法return true
            if (!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)) {
                
                return true;
            }
        }
 
 
        return false;
    }
 
    /**
     * 解锁
     * @param key
     * @param value
     */
    public void unlock(String key,String value){
        String currentValue = stringRedisTemplate.opsForValue().get(key);
        try {
            if (!StringUtils.isEmpty(currentValue) && currentValue.equals(value)) {
                stringRedisTemplate.opsForValue().getOperations().delete(key);
            }
        }catch (Exception e){
            System.out.println("出异常了");
        }
    }
}
@Service
public class TestLockImpl implements TestLock {
    //总票数
    public static int total = 1000000;
    public static int all = 1000000;
    int count = 0;
    @Autowired
    RedisLock redisLock;
 
    @Override
    public String getTicket() {
        //超时时间为10秒
        long time = System.currentTimeMillis()+10*1000;
        //加锁
        if(!redisLock.lock(total+"",time+"")){
            throw new RuntimeException("出错了");
        }
        total--;
        try{
        //Thread.sleep(200);
        count++;
        //Thread.sleep(200);
            }catch (Exception e){}
        //解锁
       redisLock.unlock(total+"",time+"");
        return "总票数为="+all+"---------"+"剩余票数为"+total+"购买人数为"+count;
    }
}
# 创建并运行一个名为 myredis 的容器
docker run \
-p 6379:6379 \
-v $PWD/data:/data \
-v $PWD/conf/redis.conf:/etc/redis/redis.conf \
--privileged=true \
--name myredis \
-d redis redis-server /etc/redis/redis.conf
 
# 命令分解
docker run \
-p 6379:6379 \ # 端口映射 宿主机:容器
-v $PWD/data:/data:rw \ # 映射数据目录 rw 为读写
-v $PWD/conf/redis.conf:/etc/redis/redis.conf:ro \ # 挂载配置文件 ro 为readonly
--privileged=true \ # 给与一些权限
--name myredis \ # 给容器起个名字
-d redis redis-server /etc/redis/redis.conf # deamon 运行容器 并使用配置文件启动容器内的 redis-server 

# 创建新表
CREATE TABLE user(name VARCHAR(20), date DATE);
usertbl
# 插入数据
INSERT INTO user(name, date) VALUES('张三', '2018-12-12');

# 选择记录
SELECT * FROM user;

# 更新数据
UPDATE user set name = '李四' WHERE name = '张三';

# 删除记录
DELETE FROM user WHERE name = '李四' ;

# 添加栏位
ALTER TABLE user ADD email VARCHAR(40);

# 更新结构
ALTER TABLE user ALTER COLUMN date SET NOT NULL;

# 更名栏位
ALTER TABLE user RENAME COLUMN date TO signup;

# 删除栏位
ALTER TABLE user DROP COLUMN email;

# 表格更名 
ALTER TABLE user RENAME TO backuptbl;

# 删除表格
DROP TABLE IF EXISTS backup_tbl;package com.leo;
 
import lombok.Data;
 
@Data
public class Student {
    /**
     *
     */
    private Integer id;
 
    /**
     *
     */
    private String name;
 
    /**
     *
     */
    private String sex;
 
    /**
     *
     */
    private String specialty;
 
    /**
     *
     */
    private String grade;
}
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.leo.StudentMapper">
    <!--auto generated Code-->
    <resultMap id="BaseResultMap" type="com.leo.Student">
        <result column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="VARCHAR"/>
        <result column="specialty" property="specialty" jdbcType="VARCHAR"/>
        <result column="grade" property="grade" jdbcType="VARCHAR"/>
    </resultMap>
 
    <!--auto generated Code-->
    <sql id="Base_Column_List">
        id,
        `name`,
        sex,
        specialty,
        grade
    </sql>
 
    <!--auto generated Code-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="student.id">
        INSERT INTO student (
            id,
            `name`,
            sex,
            specialty,
            grade
        ) VALUES (
            #{student.id,jdbcType=INTEGER},
            #{student.name,jdbcType=VARCHAR},
            #{student.sex,jdbcType=VARCHAR},
            #{student.specialty,jdbcType=VARCHAR},
            #{student.grade,jdbcType=VARCHAR}
        )
    </insert>
 
    <!--auto generated Code-->
    <insert id="insertSelective" useGeneratedKeys="true" keyProperty="student.id">
        INSERT INTO student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="student.id!=null">id,</if>
            <if test="student.name!=null">`name`,</if>
            <if test="student.sex!=null">sex,</if>
            <if test="student.specialty!=null">specialty,</if>
            <if test="student.grade!=null">grade,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="student.id!=null">#{student.id,jdbcType=INTEGER},
            </if>
            <if test="student.name!=null">#{student.name,jdbcType=VARCHAR},
            </if>
            <if test="student.sex!=null">#{student.sex,jdbcType=VARCHAR},
            </if>
            <if test="student.specialty!=null">#{student.specialty,jdbcType=VARCHAR},
            </if>
            <if test="student.grade!=null">#{student.grade,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
 
    <!--auto generated Code-->
    <insert id="insertList">
        INSERT INTO student (
        id,
        `name`,
        sex,
        specialty,
        grade
        )VALUES
        <foreach collection="students" item="student" index="index" separator=",">
            (
            #{student.id,jdbcType=INTEGER},
            #{student.name,jdbcType=VARCHAR},
            #{student.sex,jdbcType=VARCHAR},
            #{student.specialty,jdbcType=VARCHAR},
            #{student.grade,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
 
    <!--auto generated Code-->
    <update id="updateByPrimaryKeySelective">
        UPDATE student
        <set>
            <if test="student.name != null">`name`= #{student.name,jdbcType=VARCHAR},</if>
            <if test="student.sex != null">sex= #{student.sex,jdbcType=VARCHAR},</if>
            <if test="student.specialty != null">specialty= #{student.specialty,jdbcType=VARCHAR},</if>
            <if test="student.grade != null">grade= #{student.grade,jdbcType=VARCHAR}</if>
        </set>
        WHERE id = #{student.id,jdbcType=INTEGER}
    </update>
 
 
    <select id="selectOne" resultType="com.leo.Student">
        select * from student where id = #{id};
    </select>
 
 
</mapper>
 
 

猜你喜欢

转载自blog.csdn.net/ya1602/article/details/89917556