PackageCome.hanqi;ImportJava.sql.*;ImportJava.util.Scanner; Public classtest4 { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please select the type you want to enter:"); System.out.println ("" + "A: Social Security Number"); System.out.println ("" + "B: Ticket Number"); 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 ("Connection Database succeeded"); Statement St=conn.createstatement (); if(S.equals ("a")) { for(inti = 0; I < 9; i++) {System.out.println ("Please enter your ID number:"); 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 ("Input Error Please re-enter"); 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 + ", Name:" + Stu + ", birthplace: "+ Lo +", Score: "+GA); } } } Else if(S.equals ("B")) { for(inti = 0; I < 9; i++) {System.out.println ("Please enter the ticket number:"); 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 ("You entered the error, please re-enter"); } 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 + ", Name:" + Stu + ", birthplace: "+ Lo +", Score: "+GA); } } } Else{System.out.println ("You entered the wrong"); } } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); System.out.println ("Failed to connect to database"); } }}
JDBC Query Data Exercise