创立数据库表 examstudent

package com.hanqi.test;
import java.sql.*;
import java.util.*;
public class LianXi {

    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, "test", "5211314");
            System.out.println("连接数据库成功!");
            
            System.out.println("输入学生信息");
            System.out.println("Type:");
            Scanner a1=new Scanner(System.in);
            int b1=a1.nextInt();
            System.out.println("IDCard:");
            Scanner a2=new Scanner(System.in);
            String b2=a2.next();
            System.out.println("ExamCard:");
            Scanner a3=new Scanner(System.in);
            String b3=a3.next();
            System.out.println("StudentName:");
            Scanner a4=new Scanner(System.in);
            String b4=a4.next();
            System.out.println("Location:");
            Scanner a5=new Scanner(System.in);
            String b5=a5.next();
            System.out.println("Grade:");
            Scanner a6=new Scanner(System.in);
            int b6=a6.nextInt();
            Statement st = conn.createStatement(); 
            st.executeUpdate("insert into examstudent values( flowid.nextval,"+b1+",'"+b2+"','"+b3+"','"+b4+"','"+b5+"',"+b6+")");
            
            ResultSet rs= st.executeQuery("select * from examstudent");
            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);
               System.out.println("信息录入成功");
            }
            rs.close();
            st.close();
            conn.close();
            
        } 
        catch (Exception e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }

}

在 eclipse 中建立 java 程序:输入身份证号或准考证号可以查询到学生的基本信息。

package com.hanqi.test;
import java.sql.*;
import java.util.*;
public class ChaXun {

    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, "test", "5211314");
           System.out.println("连接数据库成功!");
           
           System.out.println("请选择你要输入的类型:");
           System.out.println("     a:准考证号");
           System.out.println("     b:身份证号");
           Scanner shuru=new Scanner(System.in);
           String str=shuru.nextLine();
           if(!str.equals("a")&&!str.equals("b"))
           {               
               System.out.println("您输入的有误!请重新进入程序..");
               System.exit(-1);
               
           }
          
           System.out.println("请输入号码");
           Scanner haoma=new Scanner(System.in);
           String str1=haoma.nextLine();
           
           Statement st = conn.createStatement(); 
           ResultSet rs=st.executeQuery("select * from examstudent");
          
           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);
                   if(str.equals("a"))
                   {
                   
                       if(str1.equals(ExamCard))
                       {
                       System.out.println("流水号:"+ FlowID + "   四级/六级:"+Type + "   身份证号码:"+IDCard+ "   准考证号码:"+ExamCard+ "   学生姓名:"+StudentName+ "   区域:"+Location+"   成绩:"+Grade);
                       }
                   }

                   else if(str.equals("b"))
                   {
                       if(str1.equals(IDCard))
                       {
                       System.out.println("流水号:"+ FlowID + "   四级/六级:"+Type + "   身份证号码:"+IDCard+ "   准考证号码:"+ExamCard+ "   学生姓名:"+StudentName+ "   区域:"+Location+"   成绩:"+Grade);
                       }  
                   }
                  
                } 

               rs.close();
               st.close();
               conn.close();


          
       } 
        catch (Exception e) 
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

}

完成学生信息的删除功能

package com.hanqi.test;
import java.sql.*;
import java.util.*;
public class ShanChu {

    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, "test", "5211314");
           System.out.println("连接数据库成功!");
           
         
           System.out.println("请输入学生的考号:");
           @SuppressWarnings("resource")
           Scanner haoma=new Scanner(System.in);
           String str=haoma.nextLine();
           
           Statement st = conn.createStatement(); 
           ResultSet rs=st.executeQuery("select * from examstudent");
          
           while(rs.next())
                {
                   
                      String ExamCard= rs.getString(4);                  
                  
                       if(str.equals(ExamCard))
                       {
                           st.execute("delete from EXAMSTUDENT t where t.examcard='"+ExamCard+"'");
                           System.out.println("删除成功!");
                           rs.close();
                           st.close();
                           conn.close();
                            
                           System.exit(-1);
                       }            

                  
                } 
           System.out.println("查无此人,请重新进入程序...");
            rs.close();
            st.close();
            conn.close();
            
          
       } 
        catch (Exception e) 
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

}

posted @ 2016-06-20 23:44  凌零聆  阅读(1254)  评论(0编辑  收藏  举报