Java连接操作Oracle数据库代码详解

时间:2021-05-20

废话不多说了,直接给大家贴关键代码了,具体代码如下所示:

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数据库代码详解的全部叙述,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章