Oracle JDBC and Views - java

I am trying to get SELECT statement from a view with executeQuery but i cant get a resultset, the meta data is intact (names and number of columns)
i am also tried change to PreparedStatement and CallableStatement and it is the same
Here is my code
private Statement stmt=null;
private ResultSet rset=null;
........
rset = stmt.executeQuery("SELECT STUDENT_ID,FIRST_NAME,LAST_NAME FROM STUDENT_VIEW");
columnNum=rset.getMetaData().getColumnCount();
toClient.writeInt(columnNum);
for(int i=1;i<=columnNum;i++)
toClient.writeUTF(rset.getMetaData().getColumnName(i));
if (rset.last()) {
toClient.writeInt(rset.getRow());
rset.beforeFirst();
}
while (rset.next())
for(int i=1;i<=columnNum;i++){
if(rset.getString(i)!=null)
toClient.writeUTF(rset.getString(i));
else
toClient.writeUTF(" ");
}
in the debug mode java can't execute rset.last() command
but when i do
rset = stmt.executeQuery("SELECT * FROM STUDENT_VIEW")
the code run perfectly.
Can some one help?
the STUDENT_VIEW
CREATE OR REPLACE VIEW STUDENT_VIEW
AS
SELECT
s.student_id,s.first_name,s.last_name,s.phone,s.birth_date,s.street,
s.zip_code,d.name as Department,d.credits_to_degree,s.credits,s.eligible,c.name as College
FROM
STUDENT s,DEPARTMENT d,COLLEGE c
WHERE
s.department=d.department_id and d.college_id=c.college_id;
The other tables
create table Department (
Department_ID varchar(4) not null,
Name varchar(25) unique,
Department_Head_ID varchar(9),
College_ID varchar(4),
Credits_To_Degree NUMBER(3),
Students NUMBER(3),
Faculty NUMBER(3),constraint pkDepartment primary key (Department_ID));
ALTER TABLE Department ADD FOREIGN KEY (Department_Head_ID) REFERENCES Faculty(Faculty_Id) INITIALLY DEFERRED;
ALTER TABLE Department ADD FOREIGN KEY(College_ID) REFERENCES College(College_ID) INITIALLY DEFERRED;
create table Student (
Student_ID varchar(9) not null,
First_Name varchar(25),
Last_Name varchar(25),
Phone char(11),
Birth_Date date,
Street varchar(100),
Zip_Code char(5),
Department varchar(4),
Credits integer,
Eligible char(4), constraint pkStudent primary key (Student_ID),constraint fkDeptId foreign key (Department) references Department(Department_ID));
create table College(
College_ID varchar(4) not null,
Name varchar(25),
Since date,
Dean_ID varchar(9),constraint pkCollege primary key (College_ID));

Related

Why a SQL SELECT statement doesn't return COUNT() result on Java Spring boot project?

this is my first question here so, please ask if you need more information. I am working on a personal project. I have a relatively complex relational database structure. I create schema.sql on my spring boot project as well as data.sql with sample data. I try to create a web application for simulated fitness centre web pages. I try to display the location name and number of visits for the user. I create a userLocation bean for keeping the result set as a list of the select query. I can test the statement on H2 database and its work. However, on my code, I cannot get the number of visits from the select statement.
Here is my userlocation bean,
#Data
#NoArgsConstructor
public class UserLocation {
private String locName;
private int numOfVisit;
}
Controller class getMapping method
#GetMapping("/secure/userLocation")
public String myLocation(Model model, Authentication authentication) {
String email = authentication.getName();
User currentUser = da.findUserAccount(email);
model.addAttribute("myLocationList", da.getUserLocationList(currentUser.getUserId()));
return "/secure/userLocation";
}
Here database access method;
public List<UserLocation> getUserLocationList(Long userId) {
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
String query = "SELECT l.locName, COUNT(ul.dayOfVisit) FROM location l "
+ "INNER JOIN userLocation ul ON l.locId = ul.locId "
+ "INNER JOIN sec_user sc ON ul.userId = sc.userId "
+ "WHERE sc.userId = :userId AND ul.locId = 1"
+ "GROUP BY l.locName";
namedParameters.addValue("userId", userId);
return jdbc.query(query, namedParameters, new BeanPropertyRowMapper<UserLocation>(UserLocation.class));
}
here schema.sql
CREATE TABLE location (
locId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
locName VARCHAR(75),
locAddress VARCHAR(255),
locPhone VARCHAR(25),
locEmail VARCHAR(75)
);
CREATE TABLE sec_user (
userId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(75),
lastName VARCHAR(75),
adress VARCHAR(255),
phone VARCHAR(10),
email VARCHAR(75) NOT NULL UNIQUE,
encryptedPassword VARCHAR(128) NOT NULL,
enabled BIT NOT NULL
);
CREATE TABLE coach (
coachId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
coachName VARCHAR(75),
coachLevel BIGINT,
coachRating BIGINT,
aboutMe VARCHAR(255)
);
CREATE TABLE fitnessClass (
classId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
className VARCHAR(75),
classPrice DOUBLE
);
CREATE TABLE generalCert (
certId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
certName VARCHAR(75)
);
CREATE TABLE certCoach (
certId BIGINT NOT NULL,
coachId BIGINT NOT NULL
);
ALTER TABLE certCoach
ADD CONSTRAINT certCoach_FK1 FOREIGN KEY (certId)
REFERENCES generalCert (certId);
ALTER TABLE certCoach
ADD CONSTRAINT certCoach_FK2 FOREIGN KEY (coachId)
REFERENCES coach (coachId);
CREATE TABLE userLocation (
userLocId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
locId BIGINT NOT NULL,
userId BIGINT NOT NULL,
isHomeLoc BIT,
dayOfVisit DATE
);
ALTER TABLE userLocation
ADD CONSTRAINT userLocation_FK1 FOREIGN KEY (locId)
REFERENCES location (locId);
ALTER TABLE userLocation
ADD CONSTRAINT userLocation_FK2 FOREIGN KEY (userId)
REFERENCES sec_user (userId);
CREATE TABLE amenity (
amenityId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
amenityName VARCHAR(75),
locId BIGINT
);
ALTER TABLE amenity
ADD CONSTRAINT amenity_FK FOREIGN KEY (locId)
REFERENCES location (locId);
CREATE TABLE room (
roomId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
roomName VARCHAR(75),
locId BIGINT
);
ALTER TABLE room
ADD CONSTRAINT room_FK FOREIGN KEY (locId)
REFERENCES location (locId);
CREATE TABLE classCoach (
classId BIGINT NOT NULL,
coachId BIGINT NOT NULL
);
ALTER TABLE classCoach
ADD CONSTRAINT classCoachFK1 FOREIGN KEY (classId)
REFERENCES fitnessClass(classId);
ALTER TABLE classCoach
ADD CONSTRAINT classCoachFK2 FOREIGN KEY (coachId)
REFERENCES coach(coachId);
CREATE TABLE schedule (
ScheduleId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
ScheduleDate DATE,
ScheduleTime TIME,
RoomId BIGINT,
ClassId BIGINT NOT NULL,
LocId BIGINT NOT NULL
);
ALTER TABLE schedule
ADD CONSTRAINT scheduleFK1 FOREIGN KEY (roomId)
REFERENCES room(RoomId);
ALTER TABLE schedule
ADD CONSTRAINT scheduleFK2 FOREIGN KEY (classId)
REFERENCES fitnessClass(classId);
ALTER TABLE schedule
ADD CONSTRAINT ScheduleFK3 FOREIGN KEY (LocId)
REFERENCES location(LocId);
CREATE TABLE reservation (
ClassId BIGINT NOT NULL,
userId BIGINT NOT NULL
);
ALTER TABLE reservation
ADD CONSTRAINT reservationFK1 FOREIGN KEY (classId)
REFERENCES fitnessClass(classId);
ALTER TABLE reservation
ADD CONSTRAINT reservationFK2 FOREIGN KEY (userId)
REFERENCES sec_user(userId);
CREATE TABLE workFrom (
coachId BIGINT NOT NULL,
locId BIGINT NOT NULL
);
ALTER TABLE workFrom
ADD CONSTRAINT workFromFK1 FOREIGN KEY (coachId)
REFERENCES coach(coachId);
ALTER TABLE workFrom
ADD CONSTRAINT workFromFK2 FOREIGN KEY (locId)
REFERENCES location(locId);
CREATE TABLE review (
ReviewId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
CoachId BIGINT NOT NULL,
userId BIGINT NOT NULL,
ReviewDate DATE,
ComScore CHAR(1),
EnthScore CHAR(1),
PunctScore CHAR(1),
ReviewText VARCHAR(500)
);
ALTER TABLE review
ADD CONSTRAINT reviewFK1 FOREIGN KEY (coachId)
REFERENCES coach(coachId);
ALTER TABLE review
ADD CONSTRAINT reviewFK2 FOREIGN KEY (userId)
REFERENCES sec_user(userId);
CREATE TABLE Reference (
CoachId BIGINT NOT NULL,
userId BIGINT NOT NULL
);
ALTER TABLE Reference
ADD CONSTRAINT ReferenceFK1 FOREIGN KEY (coachId)
REFERENCES coach(coachId);
ALTER TABLE review
ADD CONSTRAINT ReferenceFK2 FOREIGN KEY (userId)
REFERENCES sec_user(userId);
CREATE TABLE ClientCoach (
coachId BIGINT NOT NULL,
userId BIGINT NOT NULL,
myCoach BIT
);
ALTER TABLE ClientCoach
ADD CONSTRAINT ClientCoachFK1 FOREIGN KEY (coachId)
REFERENCES coach(coachId);
ALTER TABLE ClientCoach
ADD CONSTRAINT ClientCoachFK2 FOREIGN KEY (userId)
REFERENCES sec_user(userId);
CREATE TABLE sec_role(
roleId BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
roleName VARCHAR(30) NOT NULL UNIQUE
);
CREATE TABLE user_role
(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
userId BIGINT NOT NULL,
roleId BIGINT NOT NULL
);
ALTER TABLE user_role
ADD CONSTRAINT user_role_uk UNIQUE (userId, roleId);
ALTER TABLE user_role
ADD CONSTRAINT user_role_fk1 FOREIGN KEY (userId)
REFERENCES sec_user (userId);
ALTER TABLE user_role
ADD CONSTRAINT user_role_fk2 FOREIGN KEY (roleId)
REFERENCES sec_role (roleId);
Here the result web page
Here is EERD for the schema
Please try:
SELECT l.locName, COUNT(ul.dayOfVisit) AS numOfVisit -- ...
(to alias numOfVisit), since we are using a BeanPropertyRowMapper (which mapps by "bean properties" (i.e. "field names"): https://www.google.com/search?q=java+bean+naming+conventions).
Alternatively use an other/custom RowMapper.
And since even javadoc recommends:
... For best performance, consider using a custom RowMapper implementation.
Best:
return jdbc.query(query, namedParameters,
(ResultSet rs, int rowNum) -> { // one ResultSet per row:
// column indices start with 1(!):
return new UserLocation(rs.getString(1), rs.getInt(2));
// alternatively (name based): rs.getString("locName")...
}
);
;)
RowMapper javadoc (spring-jdbc:current)
ResultSet javadoc (jdk17)
and since RowMapper is a/meets the requirements of a functional interface, we can write it as lambda expression.

#0001: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

I want to delete a row. My TABLES are 'goal' and 'contribute'.It shows above error.
Please tell immediately what's the problem.
Table structure is ,
//goal TABLE
CREATE TABLE `goal` (`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(555) NOT NULL,
`target_value` double NOT NULL,
`target_date` date NOT NULL,
`created_date` datetime NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`))
ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
COLLATE=utf8mb4_0900_ai_ci
//Contribute TABLE
CREATE TABLE `contribute`
(`id` int(11) NOT NULL AUTO_INCREMENT,`goal` int(11) NOT NULL,
`amount` double NOT NULL, `date` date NOT NULL,
PRIMARY KEY (`id`),KEY `idgoal_idx` (`goal`),
CONSTRAINT `fk` FOREIGN KEY (`goal`) REFERENCES `goal` (`id`))
ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8m
COLLATE=utf8mb4_0900_ai_ci
//Code
public static boolean delete(int id) {
try {
Connection con = DB.getConnection();
String sql = "ALTER TABLE 'goal' ADD CONSTRAINT 'fk' FOREIGN
KEY('goal') REFERENCES 'goal' ('id') ON DELETE CASCADE ";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
In my oppinion your code is bad. You are executing this
String sql = "ALTER TABLE 'goal' ADD CONSTRAINT 'fk' FOREIGN
KEY('goal') REFERENCES 'goal' ('id') ON DELETE CASCADE ";
every time you invoke this method. You should add constraints when creating the tables.
In order to delete a row from some table i suggest you to create a database procedure or function which does it and invoke it through java.
String sql = "{? = call your_schema.your_package.delete_object(?)}";
try (CallableStatement cs = con.createCallableStatement(sql)) {
cs.setInt(1, id);
cs.executeQuery();
}
This is just an example but i think this is the correct way to do it. In this procedure you accept your ID as parameter and delete the row there.
Here's two problem:
1.your sql doesn't contain any parameter keyword : ?
You use java set parameter ps.setInt(1, id), but your sql doesn't contain keyword ?
Example for parameter using ? :
PreparedStatement p = con.prepareStatement("select * from xxxTable where xxx = ?");
p.setString(1, xxx);
More details you can learn from mysql - java.sql.SQLException Parameter index out of range (1 > number of parameters, which is 0) - Stack Overflow
2.Your SQL maybe wrong
ALTER TABLE 'goal' ADD CONSTRAINT 'fk' FOREIGN
KEY('goal') REFERENCES 'goal' ('id') ON DELETE CASCADE
'goal' table doesn't contain 'goal' column.
it should be changed like:
ALTER TABLE `contribute`
ADD FOREIGN KEY (`goal`) REFERENCES `goal`(`id`) ON DELETE CASCADE ;
SQL Fiddle Demo Link

Phantom MySQLIntegrityConstraintViolationException between two CallableStatementS

We got strange non-permanent error: MySQLIntegrityConstraintViolationException inside java transaction.
Imagine next code:
sql:
CREATE TABLE test (
ruleID INT(11) NOT NULL AUTO_INCREMENT,
name varchar(250),
PRIMARY KEY (ruleID)
) engine=InnoDB;
CREATE TABLE test2 (
ruleID INT(11) NOT NULL,
name varchar(250),
PRIMARY KEY (ruleID),
CONSTRAINT _FK1 FOREIGN KEY (ruleID)
REFERENCES test (ruleID)
ON DELETE CASCADE
) engine=InnoDB;
CREATE PROCEDURE proc1(
inout p_ruleID INT (11),
p_name varchar(250)
)
proc: begin
IF (p_ruleID < 1) THEN
INSERT INTO test (ruleID, name) values (null, p_name);
SELECT LAST_INSERT_ID() INTO p_ruleID;
else
UPDATE test
SET name = p_name
WHERE ruleID = p_ruleID;
SELECT p_ruleID;
END IF;
END //
CREATE PROCEDURE proc2(
p_ruleID INT (11),
p_name varchar(250)
)
BEGIN
DELETE FROM test2
WHERE ruleID = p_ruleID;
INSERT INTO test2(ruleID, name)
VALUES (p_ruleID, p_name);
END //
java:
Connection conn;
int id;
String name;
String subName;
...
conn.setAutoCommit(false);
id=1;
CallableStatement cs1 = conn.prepareCall("{call proc1(?,?)}");
cs1.setInt(1, id);
cs1.setString(2, name);
cs1.registerOutParameter(1, java.sql.Types.INTEGER);
cs1.execute();
int parentID = cs1.getInt(1);
cs1.close();
cs1 = conn.prepareCall("{call proc2(?,?)}");
cs1.setInt(1, parentID);
cs1.setString(2, subName);
cs1.execute();
...
So once during the "cs1.execute()" for "proc2" we got exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot add or update a child row:
a foreign key constraint fails (test2, CONSTRAINT '_FK1' FOREIGN KEY ('ruleID') REFERENCES 'test' ('ruleID') ON DELETE CASCADE)
We got it only once and can't repeat it.
One "guru" said - java transaction, performed by Connection, don't garanty the visibility of results between CallableStatementS.
Is it true? Or may be the truth is out there?

unique constraint violated?

i'm trying to update a table:
create table Out_Patient(Patient_ID number(8) primary key ,
First_Name varchar2(20) ,
Last_Name varchar2(20) ,
Gender varchar2(10) ,
Mobile_Number number(12) ,
Address varchar2(20),
Date_Of_Birth Date ,
Date_Of_Entry Date)
with foll. code:
PreparedStatement st= con.prepareStatement("Update Out_Patient Set Patient_ID=?,First_Name=?,Last_Name=?,Gender=?,Mobile_Number=?,Address=?,Date_Of_Birth=?,Date_Of_Entry=?");
String t1=text1.getText();
int id=Integer.parseInt(t1);
String t2=text2.getText();
String t3=text3.getText();
String t4=text4.getText();
String t5=text5.getText();
String t6=text_dob.getText();
String t7=text_doe.getText();
String s1=combo_gender.getSelectedItem().toString();
st.setInt(1,Integer.parseInt(t1));
st.setString(2,t2);
st.setString(3,t3);
st.setString(4,s1);
st.setString(5,t5);
st.setString(6,t4);
st.setString(7,t6);
st.setString(8,t7);
st.executeUpdate();
But i'm getting ORA-00001: unique constraint violated.
I know Patient_ID is primary key in table,but while entering details i'm keeping it same as the one in the table.What changes should be made.
Please help!!
Thanks..
Theres no WHERE clause in your JDBC update statement, therefore you're trying to set the same ID for all records. Take a look at this example: Creating a PreparedStatement Object

UPDATE sql Foreign Key Constraint

I have an application that I am working on that will query a database and display the results. The program also makes changes to the database (update, delete, insert). I had most of these features working correctly until recently when I made some changes. Now I am getting an SQLException that tells me I am violating a foreign key constraint. I have looked it up and found out that the violation is a result of more than one table sharing data. Is there a way to overcome this? How would I make the update without violating the constraint? here is my update method:
InstructorEditorPanel updateEditorPanel = new InstructorEditorPanel();
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.B_NUMBER, updBNumber);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.FIRST_NAME, updFName);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.LAST_NAME, updLName);
int result = JOptionPane.showConfirmDialog(null, updateEditorPanel,
"Update Instructor", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.OK_OPTION)
{
for (InstructorEditorPanel.FieldTitle fieldTitle : InstructorEditorPanel.FieldTitle
.values()) {
bNum = getBNumber(updateEditorPanel.getFieldText(fieldTitle.values()[0]));
fName = getFirstName(updateEditorPanel.getFieldText(fieldTitle.values()[1]));
lName = getLastName(updateEditorPanel.getFieldText(fieldTitle.values()[2]));
}
try
{
connection = DriverManager.getConnection(URL);
updateInstructor = connection.prepareStatement(
"UPDATE Instructor SET BNUMBER = ?, FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?");
}catch(SQLException sqlException){
sqlException.printStackTrace();
System.exit(1);
}//end catch
try
{
updateInstructor.setString(1, bNum);
updateInstructor.setString(2, fName);
updateInstructor.setString(3, lName);
updateInstructor.setString(4,mNumber);
updateInstructor.executeUpdate();
}catch(SQLException sqlException){
sqlException.printStackTrace();
}//end of catch
finally
{
close();
}//end
}
Display(panel); }
I had the method working without any problems until recently when I made some changes. I don't know what I did, but now I am getting the foreign key constraint violated exception
here is the exception:java.sql.SQLIntegrityConstraintViolationException: UPDATE on table 'INSTRUCTOR' caused a violation of foreign key constraint 'SQL120408141918440' for key (1234500000). The statement has been rolled back.
database info
CREATE TABLE Instructor (
BNumber varchar(10) NOT NULL,
FirstName varchar(20),
LastName varchar(30),
PRIMARY KEY (BNumber)
);
CREATE TABLE Section (
CRN int NOT NULL,
Term varchar(6) NOT NULL,
SectionNumber varchar(3),
CourseID varchar(9),
Enrollment smallint,
BNumber varchar(10),
PercentResp numeric(5,2),
CONSTRAINT CRN_Term PRIMARY KEY (CRN,Term),
FOREIGN KEY (CourseID) REFERENCES Course (CourseID),
FOREIGN KEY (BNumber) REFERENCES Instructor (BNumber)
);
INSERT INTO Instructor (BNumber, FirstName, LastName)
VALUES
('0000012345','Bill','Smith'),
('0000023456','Sue','Taylor'),
('0000034567','Skilar','Ramsey'),
('1234500000','Sam','Jones'),
('2345600000','Tyson','Quilez');
INSERT INTO Section (CRN, Term, SectionNumber, CourseID, Enrollment,
BNumber, PercentResp)
VALUES
(40035,'201040','02B','CHM2210', 31,'0000034567',100),
(40001,'201040','02B','CGS1000', 27,'0000012345',100),
(40002,'201040','70B','CGS2100', 25,'0000012345',100),
(40003,'201040','71B','CGS2100', 19,'0000012345',100),
(40004,'201040','01B','COP1000', 15,'0000012345',100),
(40030,'201040','01B','BSCC1005',30,'0000023456',100),
(40031,'201040','02B','BSCC1005',25,'0000023456',100),
(40032,'201040','70B','BSCC1005',24,'0000023456',100),
(40000,'201040','01B','CGS1000', 15,'0000012345',100),
(40034,'201040','01B','CHM2210', 27,'0000034567',100);
Replace
"UPDATE Instructor SET BNUMBER = ?, FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?"
With
"UPDATE Instructor SET FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?"
You should not update the primary key if it's the matching condition in your update query.
You should put ON UPDATE CASCADE after the references

Categories

Resources