美文网首页
JDBC:增、删、改、查

JDBC:增、删、改、查

作者: KaveeDJ | 来源:发表于2019-04-11 01:00 被阅读0次

    查询因为要返回数据,所以和增删改不一样

    增加

    String sql = "insert into hero values(null, "+"'提莫'"+","+313.0f+","+50+")";
    

    删除

    String sql = "delete from hero where id=5";
    

    修改

    String sql = "update hero set name = 'name 5' where id = 3";
    

    查询语句

    • executeQuery执行SQL查询语句
    String sql = "select * from hero";
    ResultSet rs = s.executeQuery(sql);
    while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString(2);
            float hp = rs.getFloat("hp");
            int damage = rs.getInt(4);
            System.out.printf("%d\t%s\t%f\t%d%n", id, name, hp, damage);
    }
    

    SQL语句判断账号密码是否正确

    String name = "dashen";
    String password = "thisispassword";
    String sql = "select * from user where name = '" + name + "' and password = '" + password +"'";
    ResultSet rs = s.executeQuery(sql);
    if (rs.next()) 
        System.out.println("账号密码正确");
    else 
        System.out.println("账号密码错误");   
    

    获取总数

    String sql = "select count(*) from hero";
    ResultSet rs = s.executeQuery(sql);
    int total = 0;
    while (rs.next()) {
        total = rs.getInt(1);
    }
    System.out.println("表Hero中总共有:" + total + "条数据");
    

    分页查询:Limit语句

    • 设计一个方法,进行分页查询:public static void list(int start, int count)
    • start是起始编号,count是数量
    String sql = "select * from hero limit " + start + "," + count;
    

    相关文章

      网友评论

          本文标题:JDBC:增、删、改、查

          本文链接:https://www.haomeiwen.com/subject/xcgaiqtx.html