java连接操作Oracle

  1 package com.sp.test;
  2 
  3 import java.sql.*;
  4 import java.util.*;
  5 
  6 public class Text_lianxi extends Thread {
  7     public void run() {
  8         try {
  9             yunxing();
 10             Thread.sleep(10000);
 11         } catch (InterruptedException e) {
 12             // TODO 自动生成的 catch 块
 13             e.printStackTrace();
 14         }
 15 
 16     }
 17     //输入函数
 18     public String[] shuru() {
 19         System.out.println("请按顺序依次输入考生的详细信息:
考试等级,身份证号,准考证号,考生姓名,考试地点,考试成绩");
 20         Scanner sc = new Scanner(System.in);
 21         String[] str = new String[6];
 22         for (int i = 0; i < str.length; i++) {
 23             str[i] = sc.nextLine();
 24         }
 25         System.out.println("信息输入完毕");
 26         sc.close();
 27         return str;
 28     }
 29     //查询函数
 30     public String chaxun() {
 31         System.out.println("请选择查询方式:
 a:身份证号     b:准考证号");
 32         Scanner sc = new Scanner(System.in);
 33         String s = sc.nextLine().toLowerCase();
 34         String str = "";
 35         if (s.equals("a")) {
 36             System.out.println("请输入查询号码:");
 37             String st = sc.nextLine();
 38             if (st.length() == 18) {
 39                 str = "select * from examstudent where idcard = " + st;
 40             } else {
 41                 System.out.println("身份证位数输入有误");
 42             }
 43         } else if (s.equals("b")) {
 44             System.out.println("请输入查询号码:");
 45             String st = sc.nextLine();
 46             if (st.length() == 15) {
 47                 str = "select * from examstudent where examcard = " + st;
 48             } else {
 49                 System.out.println("准考证位数输入有误");
 50             }
 51         } else {
 52             System.out.println("你输入的查询方式有误,请重新进入程序");
 53         }
 54         sc.close();
 55         return str;
 56     }
 57     //删除函数
 58     public String shanchu() {
 59         Scanner sc = new Scanner(System.in);
 60         System.out.println("请输入考生的准考证号:");
 61         String str = sc.nextLine();
 62         if (str.length() != 15) {
 63             System.out.println("准考证号输入有误,请重新输入");
 64         }
 65         sc.close();
 66         return str;
 67     }
 68     //运行
 69     public void yunxing() {
 70         synchronized ("") {
 71             try {
 72                 Connection conn = null;
 73                 // 链接数据库
 74                 Class.forName("oracle.jdbc.driver.OracleDriver");
 75                 String strURL = "jdbc:oracle:thin:@localhost:1521:SP";
 76                 conn = DriverManager.getConnection(strURL, "test", "123");
 77                 System.out.println(Thread.currentThread().getName()+"数据库连接成功");
 78                 Statement st = conn.createStatement();
 79                 // 选择功能
 80                 Scanner sc = new Scanner(System.in);
 81                 System.out.println("请选择功能:
 1:输入信息    2:查询信息   3:删除信息");
 82                 int num = sc.nextInt();
 83                 if (num == 1) {
 84                     // 输入信息
 85                     String[] str = shuru();
 86                     if (str[1].length() != 18 && str[2].length() != 15) {
 87                         System.out.println("号码位数有误(身份证号18位,准考证号15位),请重新进入系统输入");
 88                     } else {
 89                         st.execute("insert into examstudent values(fiowid.nextval,to_number(" + str[0] + "),'" + str[1]
 90                                 + "','" + str[2] + "','" + str[3] + "','" + str[4] + "'," + "to_number(" + str[5]
 91                                 + "))");
 92                         System.out.println("信息录入成功");
 93                     }
 94                 } else if (num == 2) {
 95                     // 查询
 96                     String str1 = chaxun();
 97                     ResultSet r = st.executeQuery(str1);
 98                     // 输出查询结果
 99                     if (r.next()) {
100                         System.out.println("考试等级:" + r.getString(2) + "
身份证号:" + r.getString(3) + "
准考证号:"
101                                 + r.getString(4) + "
考生姓名:" + r.getString(5) + "
考试地区:" + r.getString(6) + "
考试成绩:"
102                                 + r.getString(7));
103                     } else {
104                         System.out.println("查无此人,请重新进入系统");
105                     }
106                     r.close();
107                 } else if (num == 3) {
108                     // 删除
109                     String str2 = shanchu();
110                     int a = st.executeUpdate("delete examstudent where examcard = " + str2);
111                     if (a > 0) {
112                         System.out.println("删除成功");
113                     } else {
114                         System.out.println("查无此人,请重新进入程序");
115                     }
116                 } else {
117                     System.out.println("抱歉,暂未开放此功能");
118                 }
119                 sc.close();
120                 st.close();
121             } catch (Exception e) {
122                 e.printStackTrace();
123             }
124         }
125     }
126 
127     public static void main(String[] args) {
128         
129         Text_lianxi lx1 = new Text_lianxi();
130 //        Text_lianxi lx2 = new Text_lianxi();
131 //        Text_lianxi lx3 = new Text_lianxi();
132 
133         lx1.setName("窗口1");
134         lx1.start();
135 //        lx2.setName("窗口2");
136 //        lx2.start();
137 //        lx3.setName("窗口3");
138 //        lx3.start();
139 
140     }
141 
142 }

开始运行:

信息输入:                                                                                                身份证号查询:

                                        

准考证号查询:                                                                                               信息删除:

                                                             

输入错误信息:

                                                         

原文地址:https://www.cnblogs.com/ouyangtangfeng99/p/5605765.html