廢話不多說了,直接給大家貼關鍵代碼了,具體代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
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數據庫代碼詳解的全部敘述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!