这是我不明白的。我有一个预先制作的SQL结构,我加载到本地JDBC数据库。我可以向表添加元素,并使用此程序处理所述元素。但是,当我尝试添加已经存在于预制SQL表中的元素时,它会锁定如下所示:
当我们尝试添加我们在Java程序中创建的元素时,不会发生这种情况,如下所示:
我无法弄清楚发生了什么事,以前有没有人犯过这个错误?或者有谁知道如何解决它?出于某种原因,我不能让这个工作。我在过去两周内研究过这个问题并且这个问题不会消失。我正在粘贴下面的代码。
Driver.java:
package dylanstepp;
import java.util.Scanner;
import java.sql.*;
public class Driver {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); //Load the Oracle JDBC driver
} catch (ClassNotFoundException e) {
System.out.println("Could not load the driver");
}
try {
try (Scanner sc = new Scanner(System.in)) {
int choice;
boolean loopTrigger = true;
try (Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Chicken1") //connect oracle
) {
Statement st = conn.createStatement();
PreparedStatement updateGrade = conn.prepareStatement("Update Registered "
+ "SET grade = ? Where ssn = ?");
//st.executeUpdate("insert into login values('user2','123456')");
do {
//User Interface Text
System.out.println("*********************************************************************"
+ "\n*** ***"
+ "\n*** Welcome to Online Registration System ***"
+ "\n*** ***"
+ "\n*********************************************************************"
+ "\n\n\t1. Add a course\n\t2. Delete a course\n\t3. Add a student\n\t4. Delete a student"
+ "\n\t5. Register a course\n\t6. Drop a course\n\t7. Check student registration"
+ "\n\t8. Upload Grades\n\t9. Check grades\n\t10. Quit");
choice = sc.nextInt();
Course cOb = new Course();
Student sOb = new Student();
Registry rOb = new Registry();
switch (choice) {
case 1: //add a course
cOb.addCourse(st);
break;
case 2: //delete a course
cOb.delCourse(st);
break;
case 3: //add a student
sOb.addStudent(st);
break;
case 4: //delete a student
sOb.delStudent(st);
break;
case 5: //register a course
rOb.registerCourse(st);
break;
case 6: //drop a course
rOb.dropCourse(st);
break;
case 7: //check student registration
rOb.checkGrade(st);
break;
case 8: //upload grades
rOb.uploadGrades(updateGrade, st);
break;
case 9: //check grades
rOb.checkGrade(st);
break;
case 10://quitting the application
System.out.println("Quitting Application...");
loopTrigger = false;
break;
default:
System.out.println("Invalid option, please select again.");
break;
}
} while (loopTrigger);
updateGrade.close();
st.close();
sc.close();
conn.close();
}
}
} catch (SQLException e) {
System.out.println("Sorry, there was an error!");
System.err.println(e.getMessage());
}
}
}
每个这样的函数都会发生这种情况,所以我将在下面添加Course.java类。
Course.java:
package dylanstepp;
import java.util.*;
import java.sql.*;
public class Course {
private final Scanner sc = new Scanner(System.in);
public void addCourse(Statement st){ //**********This function adds a course to the database.
String courseCode, courseTitle;
int entriesChanged;
try {
System.out.println("Please enter a course code you would like to add: ");
courseCode = sc.nextLine().toUpperCase();
ResultSet rs = st.executeQuery("select * from Course where code = '" + courseCode + "'");
if (rs.next()) {
System.out.println("This course code already exists, please try again.");
} else {
System.out.println("Please enter the title for the course: ");
courseTitle = sc.nextLine().toUpperCase();
entriesChanged = st.executeUpdate("Insert into Course values ('" + courseCode + "','" + courseTitle + "')");
System.out.println("You just added " + entriesChanged + " entries to the course list.");
}
rs.close(); //**********Closed the result set in this function.
} catch (SQLException e) {
System.out.println("There was an error during runtime, Error: " + e.getMessage());
}
}
public void delCourse(Statement st) { //**********This function deletes a course from the database.
String courseCode;
int entriesChangedCourse, entriesChangedReg;
try {
System.out.println("Please enter a course code you would like to delete: ");
courseCode = sc.next().toUpperCase();
ResultSet rs = st.executeQuery("Select * From Course Where code = " + courseCode);
if (rs.next()) {
entriesChangedReg = st.executeUpdate("Delete from Registered Where code = " + courseCode);
entriesChangedCourse = st.executeUpdate("Delete From Course Where code = " + courseCode);
System.out.println("You just deleted " + (entriesChangedCourse + entriesChangedReg) + " entries from the database.");
} else {
System.out.println("Could not find course code, please try again.");
}
rs.close(); //**********Closed the result set in this function.
} catch (SQLException e) {
System.out.println("There was an error during runtime, Error: " + e.getMessage());
}
}
public void ScannerClose()
{
sc.close();
}
}
此架构中预先制作并通过SQL控制台输入的任何内容都会导致程序在尝试添加Java程序中已存在的值时停止运行。
Drop table Registered;
Drop table Course;
Drop table Student;
Create table Student
(
ssn numeric primary key,
name char(50),
address varchar(100),
major char(10)
);
Create table Course
(
code char(10) primary key,
title varchar(50)
);
Create table Registered
(
ssn numeric,
code char(10),
year int,
semester char(10),
grade char(1),
foreign key (ssn) references Student(ssn),
foreign key (code) references Course(code),
primary key (ssn,code,year,semester)
);
Insert into Student values(555550001,'Tom Hanks', '100 Ellen St., Spartanburg, SC 29301', 'CIS');
Insert into Student values(555550003,'Matt Cutts', '130 Martin St., Spartanburg, SC 29301', 'CS');
Insert into Student values(555550007,'Mary Jane', '5340 University Way, Spartanburg, SC 29303', 'CS');
Insert into Student values(555550005,'Courtney Ranes', '1670 Woodwill St., Spartanburg, SC 29301', 'Math');
Insert into Student values(555550002,'Terry Love', '346 Hill St., Spartanburg, SC 29301', 'CIS');
Insert into Student values(555550006,'Steve Harry', '7543 Briarwood St., Spartanburg, SC 29304', 'CIS');
Insert into Student values(555550004,'Will Smith', '7342 Esquire Dr., Spartanburg, SC 29301', 'Math');
Insert into Student values(555550008,'Caroline Manzo', '246 Altanta Hwy, Spartanburg, SC 29302', 'CS');
Insert into Student values(555550009,'Jerry Hood', '752 Ellen St., Spartanburg, SC 29301', 'HIS');
Insert into Student values(555550011,'Underson Hoper', '768 Hood St., Spartanburg, SC 29302', 'CS');
Insert into Course values('CS200', 'Computer Science I');
Insert into Course values('CS300', 'Computer Science II');
Insert into Course values('MATH200', 'Algebra');
Insert into Course values('MATH100', 'Pre-Calculus');
Insert into Course values('CS520', 'Database Systems');
Insert into Course values('CS350', 'Analysis of Algorithms');
Insert into Registered(ssn,code,year,semester) values(555550001, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550002, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550003, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550004, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550005, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550006, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550007, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550008, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550009, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550011, 'MATH100',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550003, 'CS200',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550008, 'CS200',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550007, 'CS200',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550011, 'CS200',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550003, 'CS520',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550008, 'CS520',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550011, 'CS350',2013, 'Spring');
Insert into Registered(ssn,code,year,semester) values(555550003, 'CS350',2013, 'Spring');