• JDBC(二)


    使用Statement查询数据
    /**
    * 连接数据库查询
    * @author Administrator
    *
    */
    public class Test01 {
    public static void main(String[] args) throws Exception {
    //创建连接
    Connection con=JDBC.getConnection();
    //准备sql语句
    String sql="select *from Student";
    //命令对象
    Statement stmt = con.createStatement();
    //返回
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()){
    int no = rs.getInt("No");
    String name = rs.getString("Name");
    System.out.println(no+" "+name);
    }

    //关闭连接(后打开的先关闭)
    rs.close();
    stmt.close();

    }

    }

    连接数据库.使用Statement登录

    import java.sql.*;
    import java.util.Scanner;

    /**
    * 登录
    * @author Administrator
    *
    */
    public class Test02 {
    public static void main(String[] args) throws Exception {
    Connection con=JDBC.getConnection();
    Scanner input=new Scanner(System.in);
    System.out.println("请输入名字");
    String name=input.next();
    System.out.println("请输入密码");
    int no=input.nextInt();
    //sql语句
    String sql="select *from Student where Name='"+name+"' and No='"+no+"'";
    System.out.println(sql);
    //命令对象
    Statement stmt = con.createStatement();
    /*PreparedStatement stmt = con.prepareStatement(sql);
    stmt.setString(1,name);
    stmt.setInt(2, no);*/

    //处理结果
    ResultSet rs = stmt.executeQuery(sql);
    if(rs.next()){
    System.out.println("欢迎");
    }else{

    System.out.println("登录失败");
    }
    //关闭连接(后打开的先关闭),释放资源
    rs.close();
    stmt.close();
    }

    }

    Statement接口执行sql命令的三个方法:

    ResultSet  executeQuery(String sql):可以执行sql查询并获取ResultSet对象

    int executeUpdate(String sql):可以执行插入,删除,更新的操作,返回值是受影响的行数

    boolean execute(String  sql):可以执行任意sql语句

  • 相关阅读:
    JS的运行机制
    Vue路由
    javascript的逼格
    Vue开发中遇到的问题及解决方案
    模块模式
    2019年终总结
    http知识总结
    小议函数的节流和防抖
    nodejs初探一二
    Object是个什么鬼
  • 原文地址:https://www.cnblogs.com/sujulin/p/6672673.html
Copyright © 2020-2023  润新知