Java连接操作Oracle数据库代码详解
程序员文章站
2024-03-12 21:50:20
废话不多说了,直接给大家贴关键代码了,具体代码如下所示:
package com.sp.test;
import java.sql.*;
import jav...
废话不多说了,直接给大家贴关键代码了,具体代码如下所示:
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数据库代码详解的全部叙述,希望对大家有所帮助