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(); } } }