廢話不多說了,直接給大家貼關鍵代碼了,具體代碼如下所示:
package com.sp.test;import java.sql.*;import java.util.*;public class Text_lianxi extends Thread {public void run() {try {yunxing();Thread.sleep(10000);} catch (InterruptedException e) {// TODO 自動生成的catch 塊e.printStackTrace();}}//輸入函數public String[] shuru() {System.out.println("請按順序依次輸入考生的詳細信息:/n考試等級,身份證號,准考證號,考生姓名,考試地點,考試成績");Scanner sc = new Scanner(System.in);String[] str = new String[6];for (int i = 0; i < str.length; i++) {str[i] = sc.nextLine();}System.out.println("信息輸入完畢");sc.close();return str;}//查詢函數public String chaxun() {System.out.println("請選擇查詢方式:/na:身份證號b:准考證號");Scanner sc = new Scanner(System.in);String s = sc.nextLine().toLowerCase();String str = "";if (s.equals("a")) {System.out.println("請輸入查詢號碼:");String st = sc.nextLine();if (st.length() == 18) {str = "select * from examstudent where idcard = " + st;} else {System.out.println("身份證位數輸入有誤");}} else if (s.equals("b")) {System.out.println("請輸入查詢號碼:");String st = sc.nextLine();if (st.length() == 15) {str = "select * from examstudent where examcard = " + st;} else {System.out.println("准考證位數輸入有誤");}} else {System.out.println("你輸入的查詢方式有誤,請重新進入程序");}sc.close();return str;}//刪除函數public String shanchu() {Scanner sc = new Scanner(System.in);System.out.println("請輸入考生的准考證號:");String str = sc.nextLine();if (str.length() != 15) {System.out.println("准考證號輸入有誤,請重新輸入");}sc.close();return str;}//運行public void yunxing() {synchronized ("") {try {Connection conn = null;// 鏈接數據庫Class.forName("oracle.jdbc.driver.OracleDriver");String strURL = "jdbc:oracle:thin:@localhost:1521:SP";conn = DriverManager.getConnection(strURL, "test", "123");System.out.println(Thread.currentThread().getName()+"數據庫連接成功");Statement st = conn.createStatement();// 選擇功能Scanner sc = new Scanner(System.in);System.out.println("請選擇功能:/n 1:輸入信息2:查詢信息3:刪除信息");int num = sc.nextInt();if (num == 1) {// 輸入信息String[] str = shuru();if (str[1].length() != 18 && str[2].length() != 15) {System.out.println("號碼位數有誤(身份證號18位,准考證號15位),請重新進入系統輸入");} else {st.execute("insert into examstudent values(fiowid.nextval,to_number(" + str[0] + "),'" + str[1]+ "','" + str[2] + "','" + str[3] + "','" + str[4] + "'," + "to_number(" + str[5]+ "))");System.out.println("信息錄入成功");}} else if (num == 2) {// 查詢String str1 = chaxun();ResultSet r = st.executeQuery(str1);// 輸出查詢結果if (r.next()) {System.out.println("考試等級:" + r.getString(2) + "/n身份證號:" + r.getString(3) + "/n准考證號:"+ r.getString(4) + "/n考生姓名:" + r.getString(5) + "/n考試地區:" + r.getString(6) + "/n考試成績:"+ r.getString(7));} else {System.out.println("查無此人,請重新進入系統");}r.close();} else if (num == 3) {// 刪除String str2 = shanchu();int a = st.executeUpdate("delete examstudent where examcard = " + str2);if (a > 0) {System.out.println("刪除成功");} else {System.out.println("查無此人,請重新進入程序");}} else {System.out.println("抱歉,暫未開放此功能");}sc.close();st.close();} catch (Exception e) {e.printStackTrace();}}}public static void main(String[] args) {Text_lianxi lx1 = new Text_lianxi();// Text_lianxi lx2 = new Text_lianxi();// Text_lianxi lx3 = new Text_lianxi();lx1.setName("窗口1");lx1.start();// lx2.setName("窗口2");// lx2.start();// lx3.setName("窗口3");// lx3.start();}}開始運行:
信息輸入: 身份證號查詢:
准考證號查詢: 信息刪除:
輸入錯誤信息:
以上所述是小編給大家介紹的Java連接操作Oracle數據庫代碼詳解的全部敘述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!