jdbc作业2
2.输入准考证号或身份证号 查询学生信息
package com.hanqi.db; import java.sql.*; import java.util.Scanner; public class JdbcHW2 { public static void main(String[] args) { // TODO 自动生成的方法存根 Connection con=null; Scanner sc=new Scanner(System.in); System.out.println("请选择您要输入的类型:"); System.out.println(" "+"a:准考证号"); System.out.println(" "+"b:身份证号"); String s=sc.next(); try { //连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); String strUrl="jdbc:oracle:thin:@localhost:1521:ORCL"; con=DriverManager.getConnection(strUrl,"test","test"); Statement st=con.createStatement(); if(s.equals("b")) { System.out.println("请输入您的身份证号:"); String id=sc.next(); String idcard="select * from EXAMSTUDENT t where idcard="+id; ResultSet rs=st.executeQuery(idcard); while(rs.next()) { String ty=rs.getString("type"); String ic=rs.getString("idcard"); String ec=rs.getString("examcard"); String sn=rs.getString("studentname"); String lt=rs.getString("location"); String gr=rs.getString("grade"); System.out.println("type="+ty+",idcard="+ic+",examcard="+ec+",姓名:"+sn+",籍贯:"+lt+",分数:"+gr); } }else if(s.equals("a")) { System.out.println("请输入您的准考证号:"); String id=sc.next(); String examcard="select * from EXAMSTUDENT t where examcard="+id; ResultSet rs=st.executeQuery(examcard); while(rs.next()) { String ty=rs.getString("type"); String ic=rs.getString("idcard"); String ec=rs.getString("examcard"); String sn=rs.getString("studentname"); String lt=rs.getString("location"); String gr=rs.getString("grade"); System.out.println("type="+ty+",idcard="+ic+",examcard="+ec+",姓名:"+sn+",籍贯:"+lt+",分数:"+gr); } }else { System.out.println("您的输入有误,请重新进入程序..."); } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { if(con!=null) { try { con.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } }
运行结果: