Click here to Skip to main content
15,885,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Our professor gave us example code that we would just have to substitute(yes, he said we can copy code) since the class doesn't know SQL, and the official DB course is next semester.The gui and save to file has been finished except for the db part.


I have a List<person>, that has objects of types Student and Faculty (both extend Person). There is a method save() in my Database class that inserts data into SQL. My struggle is figuring out how to add both Faculty and Student to the database, would it require 2 different databases?


The person (faculty or student) is added from another class by invoking a method in this Database class.




Java
public void save() throws SQLException {///SAVE TO SQL
	String checkSql = "select count(*) as count from people where id=?";
	PreparedStatement checkStmt = con.prepareStatement(checkSql);  
        
       String insertSql = "insert into faculty (id, name, address, phone, "
				+ "employment, credits, full_time, fac_category, room, pay) values (?, ?, ?, ?, ?, ?, ?)";
        
        String updateSql = "update faculty set name=?, address=?, phone=?,"
				+ " employment=?, credits=?, full+time=?";

        PreparedStatement updateStatement = con.prepareStatement(updateSql);
        
        for(Person person: people) {
				int id = person.getId();
				String name = person.getName();
				String address = person.getAddress();
				String phone = person.getPhone();
				String emp =               
                                       person.EmploymentType().toString();
				int credits = person.getCredits();
				boolean fullTime = person.isFullTime();
				
                                checkStmt.setInt(1,  id);
				ResultSet checkResult = checkStmt.executeQuery();
				checkResult.next();
				
				int count = checkResult.getInt(1);
				
				if(count == 0) {
		            System.out.println("Inserting person with ID " + id);
					
					int col = 1;
					insertStatement.setInt(col++, id);
					insertStatement.setString(col++, name);
					insertStatement.setString(col++, emp);
				insertStatement.setString(col++, address);
					insertStatement.setString(col++, phone);
					insertStatement.setString(col++, emp);
					insertStatement.setInt(col++, credits);

					insertStatement.executeUpdate();
				} else {
					System.out.println("Updating person with ID " + id);
					int col = 1;
					
					updateStatement.setInt(col++, id);
					updateStatement.setString(col++, name);
					updateStatement.setString(col++, emp);
				       updateStatement.setString(col++, address);
					updateStatement.setString(col++, phone);
					updateStatement.setString(col++, emp);
					updateStatement.setInt(col++, credits);

				        updateStatement.executeUpdate();

                                        updateStatement.close();
		                        insertStatement.close();
		                        checkStmt.close();
	}///CLOSE SAVE METHOD 
				}
			}
Posted
Updated 6-Dec-14 11:10am
v2
Comments
ZurdoDev 7-Dec-14 22:12pm    
faculty appears to be a table so your "insert into faculty" will insert a record. Just create a new table in database to store students.
jchacon28 8-Dec-14 10:10am    
since the List is of type Person, and there are both student and faculty, can i do this
for(i = 0; i < people.size(); i++)
{
people[i] = tempPerson;
empType = tempPerson.getEmployment //student or fac
if(empType = fac)
{
//add faculty table/info to SQL
}
else
{
//add student table & info to SQL
}
.
.
.
and would i have to make different objects of PreparedStatement(insertStatement2, updateStatement2) or would i re-use the originals?
ZurdoDev 8-Dec-14 10:13am    
I don't follow exactly, but yes, that seems like that would work.

It may help to just explain it out in English first and then translate that into steps into code.
jchacon28 8-Dec-14 10:18am    
well i am just wondering since i have to check the list which includes, both Faculty and Student, can i temporarily instantiate the objects as Person?
ZurdoDev 8-Dec-14 10:23am    
Depends on your classes. These are classes that you are building so you can make them work however you need to. There are lots of different approaches.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900