zoukankan      html  css  js  c++  java
  • JDBC 查询数据练习

    package come.hanqi;
    
    import java.sql.*;
    import java.util.Scanner;
    
    public class test4 {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            System.out.println("请选择您要输入的类型:");
            System.out.println(" " + "a:身份证号");
            System.out.println(" " + "b:准考证号");
            String s = sc.next();
    
            Connection conn = null;
            try
            {
                Class.forName("oracle.jdbc.OracleDriver");
                String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
                conn = DriverManager.getConnection(url, "test1", "574004");
                System.out.println("连接数据库成功");
    
                Statement st = conn.createStatement();
                if (s.equals("a")) 
                {
                    for (int i = 0; i < 9; i++)
                    {
                        System.out.println("请输入身份证号:");
                        String ID = sc.next();
                        String IDCard = "select * from EXAMSTUDENT t where idcard=" + ID;
                        ResultSet rs = st.executeQuery(IDCard);
    
                        if (ID != "select idcard from EXAMSTUDENT t ")
                        {
                            System.out.println("输入有误请重新输入");
                            i--;
                            
                        }
                        while (rs.next()) 
                        {
                            String fl = rs.getString(1);
                            String ty = rs.getString(2);
                            String id = rs.getString(3);
                            String ex = rs.getString(4);
                            String stu = rs.getString(5);
                            String lo = rs.getString(6);
                            String ga = rs.getString(7);
    
                            System.out.println("flowid=" + fl + ",type=" + ty + ",idcard=" + id + ",examcard=" + ex + ",姓名:"
                                    + stu + ",籍贯:" + lo + ",分数:" + ga);
                        }
    
                    }
    
                } 
                else if (s.equals("b"))
                {
                    for (int i = 0; i < 9; i++)
                    {
                        System.out.println("请输入准考证号:");
                        String EX = sc.next();
                        String EXcard = "select * from EXAMSTUDENT t where examcard=" + EX;
                        ResultSet rs = st.executeQuery(EXcard);
    
                        if (EX != "select EXAMCARD from EXAMSTUDENT t ")
                        {
                            System.out.println("您输入有误,请重新输入");
                        }
                        while (rs.next()) 
                        {
                            String fl = rs.getString(1);
                            String ty = rs.getString(2);
                            String id = rs.getString(3);
                            String ex = rs.getString(4);
                            String stu = rs.getString(5);
                            String lo = rs.getString(6);
                            String ga = rs.getString(7);
    
                            System.out.println("flowid=" + fl + ",type=" + ty + ",idcard=" + id + ",examcard=" + ex + ",姓名:"
                                    + stu + ",籍贯:" + lo + ",分数:" + ga);
                        }
                    }
                }
                else 
                {
                    System.out.println("您输入有误");
                }
    
            }
            catch (Exception e)
            {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
                System.out.println("连接数据库失败");
            }
    
        }
    
    }

  • 相关阅读:
    Atom 和 markdown 基本使用
    c++11 正则表达式基本使用
    Emacs 之窗口管理
    Emacs 之列编辑模式
    Emacs 之查看帮助
    使用 json_in_java
    Linux服务 httpd
    Linux 编译安装BIND
    Kerberos
    Linux服务 DNS&BIND
  • 原文地址:https://www.cnblogs.com/zhailiming/p/5606060.html
Copyright © 2011-2022 走看看