废话不多说了,直接给大家贴关键代码了,具体代码如下所示:
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数据库代码详解的全部叙述,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!