Java連接操作Oracle數(shù)據(jù)庫代碼詳解
廢話不多說了,直接給大家貼關(guān)鍵代碼了,具體代碼如下所示:
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();
}
}
//輸入函數(shù)
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;
}
//查詢函數(shù)
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("身份證位數(shù)輸入有誤");
}
} 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("準考證位數(shù)輸入有誤");
}
} else {
System.out.println("你輸入的查詢方式有誤,請重新進入程序");
}
sc.close();
return str;
}
//刪除函數(shù)
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;
// 鏈接數(shù)據(jù)庫
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()+"數(shù)據(jù)庫連接成功");
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("號碼位數(shù)有誤(身份證號18位,準考證號15位),請重新進入系統(tǒng)輸入");
} 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);
// 輸出查詢結(jié)果
if (r.next()) {
System.out.println("考試等級:" + r.getString(2) + "\n身份證號:" + r.getString(3) + "\n準考證號:"
+ r.getString(4) + "\n考生姓名:" + r.getString(5) + "\n考試地區(qū):" + r.getString(6) + "\n考試成績:"
+ r.getString(7));
} else {
System.out.println("查無此人,請重新進入系統(tǒng)");
}
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數(shù)據(jù)庫代碼詳解的全部敘述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
springdata jpa使用Example快速實現(xiàn)動態(tài)查詢功能
這篇文章主要介紹了springdata jpa使用Example快速實現(xiàn)動態(tài)查詢功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java復制文件的4種方式及拷貝文件到另一個目錄下的實例代碼
這篇文章主要介紹了java復制文件的4種方式,通過實例帶給大家介紹了java 拷貝文件到另一個目錄下的方法,需要的朋友可以參考下2018-06-06
Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的真正理解
這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
解讀System.getProperty("ENM_HOME")中的值從哪獲取的
這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
關(guān)于Spring?@Transactional事務(wù)傳播機制詳解
我們?nèi)粘9ぷ髦袠O少使用事務(wù)傳播級別,單純只是使用事務(wù)和rollbackfor拋出異常來解決事務(wù)問題,但其實我們很多時候使用的是不正確的,或者說會造成事務(wù)粒度過大,本文詳解一下事務(wù)傳播級別,也讓自己更好地處理事務(wù)問題,需要的朋友可以參考下2023-08-08
使用Spring Batch實現(xiàn)批處理任務(wù)的詳細教程
在企業(yè)級應用中,批處理任務(wù)是不可或缺的一部分,它們通常用于處理大量數(shù)據(jù),如數(shù)據(jù)遷移、數(shù)據(jù)清洗、生成報告等,Spring Batch是Spring框架的一部分,本文將介紹如何使用Spring Batch與SpringBoot結(jié)合,構(gòu)建和管理批處理任務(wù),需要的朋友可以參考下2024-06-06

