Java銜接操作Oracle數據庫代碼詳解。本站提示廣大學習愛好者:(Java銜接操作Oracle數據庫代碼詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java銜接操作Oracle數據庫代碼詳解正文
空話不多說了,直接給年夜家貼症結代碼了,詳細代碼以下所示:
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("請選擇查詢方法:\n a:身份證號 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數據庫代碼詳解的全體論述,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!