java编写在数据库中按条件查询数据

Posted 薛岩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编写在数据库中按条件查询数据相关的知识,希望对你有一定的参考价值。

package lianxi2;

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


public class lianxi2 {

    public static void main(String[] args) {
        Connection conn = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String strUrl ="jdbc:Oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(strUrl, "test1", "duibuqi19950220");
        System.out.println("链接数据库成功!");
        Statement st = conn.createStatement();
        
        System.out.println("请输入身份证号或者学生考号");
        System.out.println("a:身份证号");
        System.out.println("b:准考证号");
        Scanner b = new Scanner (System.in);
        String str=b.next();
        if(str.equals("a"))
        {
        System.out.println("请输入身份证号");
        String bb = b.next();
        ResultSet rs = st.executeQuery("select * from EXAMSTUDENT where IDCard = ‘"+bb+"‘");
        
        while(rs.next())
        {
            int FlowID = rs.getInt(1);
            int Type   = rs.getInt(2);
         String IDCard = rs.getString(3);
         String ExamCard = rs.getString(4);
         String StudentName = rs.getString(5);
         String Location = rs.getString(6);
           int Grade = rs.getInt(7);
           System.out.println("序列号是"+FlowID+"四六级"+Type+"身份证号是"+IDCard+"学生考号是"+ExamCard+
                   "姓名是"+StudentName+"在这所城市"+Location+"成绩是"+Grade);
        }
        }
        else if(str.equals("b"))
        {
            System.out.println("输入准考证号:");
        String aa = b.next();
        ResultSet ru = st.executeQuery("select * from EXAMSTUDENT where ExamCard = ‘"+aa+"‘");
        
        while(ru.next())
        {
            int FlowID = ru.getInt(1);
            int Type   = ru.getInt(2);
         String IDCard = ru.getString(3);
         String ExamCard = ru.getString(4);
         String StudentName = ru.getString(5);
         String Location = ru.getString(6);
           int Grade = ru.getInt(7);
           System.out.println("序列号是"+FlowID+"四六级"+Type+"身份证号是"+IDCard+"学生考号是"+ExamCard+
                   "姓名是"+StudentName+"在这所城市"+Location+"成绩是"+Grade);
        }
        }
        else
        {
            System.out.println("输入有误!");
        }
        } 
    catch (Exception e) 
    {
       e.printStackTrace();
    }

    }

}
package lianxi2;

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


public class lianxi2 {

    public static void main(String[] args) {
        Connection conn = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String strUrl ="jdbc:Oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(strUrl, "test1", "duibuqi19950220");
        System.out.println("链接数据库成功!");
        Statement st = conn.createStatement();
        
        System.out.println("请输入身份证号或者学生考号");
        System.out.println("a:身份证号");
        System.out.println("b:准考证号");
        Scanner b = new Scanner (System.in);
        String str=b.next();
        if(str.equals("a"))
        {
        System.out.println("请输入身份证号");
        String bb = b.next();
        ResultSet rs = st.executeQuery("select * from EXAMSTUDENT where IDCard = ‘"+bb+"‘");
        
        while(rs.next())
        {
            int FlowID = rs.getInt(1);
            int Type   = rs.getInt(2);
         String IDCard = rs.getString(3);
         String ExamCard = rs.getString(4);
         String StudentName = rs.getString(5);
         String Location = rs.getString(6);
           int Grade = rs.getInt(7);
           System.out.println("序列号是"+FlowID+"四六级"+Type+"身份证号是"+IDCard+"学生考号是"+ExamCard+
                   "姓名是"+StudentName+"在这所城市"+Location+"成绩是"+Grade);
        }
        }
        else if(str.equals("b"))
        {
            System.out.println("输入准考证号:");
        String aa = b.next();
        ResultSet ru = st.executeQuery("select * from EXAMSTUDENT where ExamCard = ‘"+aa+"‘");
        
        while(ru.next())
        {
            int FlowID = ru.getInt(1);
            int Type   = ru.getInt(2);
         String IDCard = ru.getString(3);
         String ExamCard = ru.getString(4);
         String StudentName = ru.getString(5);
         String Location = ru.getString(6);
           int Grade = ru.getInt(7);
           System.out.println("序列号是"+FlowID+"四六级"+Type+"身份证号是"+IDCard+"学生考号是"+ExamCard+
                   "姓名是"+StudentName+"在这所城市"+Location+"成绩是"+Grade);
        }
        }
        else
        {
            System.out.println("输入有误!");
        }
        } 
    catch (Exception e) 
    {
       e.printStackTrace();
    }

    }

}

 

以上是关于java编写在数据库中按条件查询数据的主要内容,如果未能解决你的问题,请参考以下文章

在嵌套选择查询中按条件分组后加入

iOS中FMDB按条件查询数据

分页,条件查询

如何禁用在android片段类中按下的后退按钮

如何防止在android片段中按下后退按钮时调用onCreateView

在 Django ORM 中按查询分组