码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-06-24 10:46:22      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

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编写在数据库中按条件查询数据

标签:

原文地址:http://www.cnblogs.com/zzyu/p/5613292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!