我正在努力,但我现在迷失了。我知道这是一个可怕的代码,但如果你真的雄心勃勃,有人可以尝试帮助我解决语法和整体问题。谢谢。
这是SQL架构:
create table Person(
id int primary key,
name varchar(255) not null
);
create table Band(
id int primary key,
name varchar(255),
style varchar(255)
);
create table memberOf(
person int references Person(id),
band int references Band(id),
primary key(person, band)
);
到目前为止,这是我编写的可怕程序:
import java.sql.*;
public class DegreeNumber {
public static void degreesOfSeparation(int origin) throws Exception {
//Loads driver.
Class.forName("com.mysql.jdbc.Driver");
// Makes connection to server
Connection c = DriverManager.getConnection
("jdbc:mysql://cursa.ccs.neu.edu/test");
//Create a new prepared Statement that makes a
temporary table that can hold people in bands
PreparedStatement temp = c.prepareStatement
("create temporary table CurrentBand (
id int primary key);
");
//Execute query, creates table above
temp.execute();
//Create prepared statement to insert people of band into table
PreparedStatement insert = c.prepareStatement
("insert into CurrentBand (id)
select m.musician
from memberOf m
where ? = m.band;");
//Get Bands in table
PreparedStatement getBands = c.prepareStatement
("select b.id
from Bands b");
ResultSet bands = getBands.executeQuery();
int bandCount = 1;
//Sets parameter to first band on list
insert.setInt(1, bands.getInt(bandCount));
//Execute bands being inserted
insert.execute();
//Gives back ResultSet with band listed
PreparedStatement returnCurrentBand = c.prepareStatement
("select * from CurrentBand");
//Execute to give back CurrentBand records
ResultSet currentBand = returnCurrentBand.executeQuery();
//Creates table to hold musicians
PreparedStatement createDegree = c.prepareStatement
("create temporary table Degrees(
id int,
name varchar(255),
degree int,
primary key (id))");
//Execute to create table
createDegree.execute();
//Insert original Person into table Degrees
PreparedStatement insertOrig = c.prepareStement
("insert into Degrees (id, name, degree)
select p.id, p.name, 0
from Person p
where p.id = ?");
insertOrig.setInt(1, origin);
insertOrig.execute();
int count = 1;
//If there are still bands left on list,
while {(!bands.isAfterLast()){
PreparedStatement thisRound = c.prepareStatement
("insert into Degrees (id, name, degree)
select p.id, p.name, ?
from Person p, memberOf m, currentBand c
where p.id = m.musician
and m.band = c.id");
thisRound.setInt (1, count);
count++;
PreparedStatement truncate = c.prepareStatement("truncate table currentBand");
truncate.execute();
bandCount++;
insert.execute();
bands.next();
}
//means all bands have been gone through, find unique people, make new table & sort and print out
PreparedStatement createFinal = c.prepareStatement
("create table Final (
name varchar(255),
degree int,
primary key (name, degree))");
createFinal.execute();
PreparedStatement makeFinal = c.prepareStatement(
("insert into Final (name, degree)
select unique (d.name, d.degree)
from Degrees d
sort by degree asc, name asc");
makeFinal.execute();
ResultSet final = c.prepareStatement("select * from Final").executeQuery();
while (final.next()) {
System.out.println("Musician Name " + final.getString("name") +
" Degree " + rs.getInt("degree"));
答案 0 :(得分:1)
你非常需要DAO。另请查看this thread。
答案 1 :(得分:1)
您遇到的问题主要是由于抽象和设计不佳造成的。你意识到这不是一段很棒的代码,这很好。为了给你一些关于为什么这是坏事的煽动,让我们考虑你所提到的课程的以下问题。
我觉得如果你解决这些关键领域,你可以重构一个更好的解决方案。