程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> java連接操作Oracle,java連接oracle

java連接操作Oracle,java連接oracle

編輯:Oracle教程

java連接操作Oracle,java連接oracle


  1 package com.sp.test;
  2 
  3 import java.sql.*;
  4 import java.util.*;
  5 
  6 public class Text_lianxi extends Thread {
  7     public void run() {
  8         try {
  9             yunxing();
 10             Thread.sleep(10000);
 11         } catch (InterruptedException e) {
 12             // TODO 自動生成的 catch 塊
 13             e.printStackTrace();
 14         }
 15 
 16     }
 17     //輸入函數
 18     public String[] shuru() {
 19         System.out.println("請按順序依次輸入考生的詳細信息:\n考試等級,身份證號,准考證號,考生姓名,考試地點,考試成績");
 20         Scanner sc = new Scanner(System.in);
 21         String[] str = new String[6];
 22         for (int i = 0; i < str.length; i++) {
 23             str[i] = sc.nextLine();
 24         }
 25         System.out.println("信息輸入完畢");
 26         sc.close();
 27         return str;
 28     }
 29     //查詢函數
 30     public String chaxun() {
 31         System.out.println("請選擇查詢方式:\n a:身份證號     b:准考證號");
 32         Scanner sc = new Scanner(System.in);
 33         String s = sc.nextLine().toLowerCase();
 34         String str = "";
 35         if (s.equals("a")) {
 36             System.out.println("請輸入查詢號碼:");
 37             String st = sc.nextLine();
 38             if (st.length() == 18) {
 39                 str = "select * from examstudent where idcard = " + st;
 40             } else {
 41                 System.out.println("身份證位數輸入有誤");
 42             }
 43         } else if (s.equals("b")) {
 44             System.out.println("請輸入查詢號碼:");
 45             String st = sc.nextLine();
 46             if (st.length() == 15) {
 47                 str = "select * from examstudent where examcard = " + st;
 48             } else {
 49                 System.out.println("准考證位數輸入有誤");
 50             }
 51         } else {
 52             System.out.println("你輸入的查詢方式有誤,請重新進入程序");
 53         }
 54         sc.close();
 55         return str;
 56     }
 57     //刪除函數
 58     public String shanchu() {
 59         Scanner sc = new Scanner(System.in);
 60         System.out.println("請輸入考生的准考證號:");
 61         String str = sc.nextLine();
 62         if (str.length() != 15) {
 63             System.out.println("准考證號輸入有誤,請重新輸入");
 64         }
 65         sc.close();
 66         return str;
 67     }
 68     //運行
 69     public void yunxing() {
 70         synchronized ("") {
 71             try {
 72                 Connection conn = null;
 73                 // 鏈接數據庫
 74                 Class.forName("oracle.jdbc.driver.OracleDriver");
 75                 String strURL = "jdbc:oracle:thin:@localhost:1521:SP";
 76                 conn = DriverManager.getConnection(strURL, "test", "123");
 77                 System.out.println(Thread.currentThread().getName()+"數據庫連接成功");
 78                 Statement st = conn.createStatement();
 79                 // 選擇功能
 80                 Scanner sc = new Scanner(System.in);
 81                 System.out.println("請選擇功能:\n 1:輸入信息    2:查詢信息   3:刪除信息");
 82                 int num = sc.nextInt();
 83                 if (num == 1) {
 84                     // 輸入信息
 85                     String[] str = shuru();
 86                     if (str[1].length() != 18 && str[2].length() != 15) {
 87                         System.out.println("號碼位數有誤(身份證號18位,准考證號15位),請重新進入系統輸入");
 88                     } else {
 89                         st.execute("insert into examstudent values(fiowid.nextval,to_number(" + str[0] + "),'" + str[1]
 90                                 + "','" + str[2] + "','" + str[3] + "','" + str[4] + "'," + "to_number(" + str[5]
 91                                 + "))");
 92                         System.out.println("信息錄入成功");
 93                     }
 94                 } else if (num == 2) {
 95                     // 查詢
 96                     String str1 = chaxun();
 97                     ResultSet r = st.executeQuery(str1);
 98                     // 輸出查詢結果
 99                     if (r.next()) {
100                         System.out.println("考試等級:" + r.getString(2) + "\n身份證號:" + r.getString(3) + "\n准考證號:"
101                                 + r.getString(4) + "\n考生姓名:" + r.getString(5) + "\n考試地區:" + r.getString(6) + "\n考試成績:"
102                                 + r.getString(7));
103                     } else {
104                         System.out.println("查無此人,請重新進入系統");
105                     }
106                     r.close();
107                 } else if (num == 3) {
108                     // 刪除
109                     String str2 = shanchu();
110                     int a = st.executeUpdate("delete examstudent where examcard = " + str2);
111                     if (a > 0) {
112                         System.out.println("刪除成功");
113                     } else {
114                         System.out.println("查無此人,請重新進入程序");
115                     }
116                 } else {
117                     System.out.println("抱歉,暫未開放此功能");
118                 }
119                 sc.close();
120                 st.close();
121             } catch (Exception e) {
122                 e.printStackTrace();
123             }
124         }
125     }
126 
127     public static void main(String[] args) {
128         
129         Text_lianxi lx1 = new Text_lianxi();
130 //        Text_lianxi lx2 = new Text_lianxi();
131 //        Text_lianxi lx3 = new Text_lianxi();
132 
133         lx1.setName("窗口1");
134         lx1.start();
135 //        lx2.setName("窗口2");
136 //        lx2.start();
137 //        lx3.setName("窗口3");
138 //        lx3.start();
139 
140     }
141 
142 }

開始運行:

信息輸入:                                                                                                身份證號查詢:

                                        

准考證號查詢:                                                                                               信息刪除:

                                                             

輸入錯誤信息:

                                                         

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved