需要帮助使用SQL&amp ;; java程序

时间:2011-02-13 17:12:25

标签: java sql

我正在努力,但我现在迷失了。我知道这是一个可怕的代码,但如果你真的雄心勃勃,有人可以尝试帮助我解决语法和整体问题。谢谢。

这是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"));

2 个答案:

答案 0 :(得分:1)

你非常需要DAO。另请查看this thread

答案 1 :(得分:1)

您遇到的问题主要是由于抽象和设计不佳造成的。你意识到这不是一段很棒的代码,这很好。为了给你一些关于为什么这是坏事的煽动,让我们考虑你所提到的课程的以下问题。

  • 它不是面向对象的。这只是 一个静态隐藏的程序 类。
  • 抛出通用异常。我们不会有太多的机会 处理其范围之外的错误。
  • 每次使用此功能(我甚至不称它为方法), 你建立了一个新的连接 数据库。这非常昂贵。
  • 数据库中的表未充分表示为Java程序中的第一类对象。他们遭受弱抽象,无法封装他们的细节。这主要是因为感觉导致这是糟糕的代码。
  • 这不是一个非常可测试的功能。将此函数分解为许多方法,并将参数传递给每个方法以完成其工作。
  • 这很长,让它变得难以理解。目标是5-15行的方法。
  • 它使用临时表。当你能够很好地工作时,这会扼杀效率。
  • 问题没有详细记录。我从代码中了解到它与乐队和人有关,但这与自由度有什么关系?使问题和解决方案显而易见。

我觉得如果你解决这些关键领域,你可以重构一个更好的解决方案。