我目前有一個非常基本的應用程序與數據庫交互(使用netbeans和jdbc),以便您可以添加團隊,球員和分數。我現在需要能夠在聯盟表/團隊(與球員)表等中一起顯示來自每個表的項目聯盟表 - SQL查詢/ JDBC問題
我的問題是我該如何去從表格中檢索信息,以及如何顯示它,我完全無能爲力,應該如何去做。我假設我需要做一個Join或Select語句(我是一個完整的SQL新手),然後使用循環選擇每個表項並以某種方式將其顯示在表中?
我唯一的當前工作特徵是添加到數據庫中,IE添加一個新的團隊添加一個新的球員等等,顯示錶格中的內容是我難倒的地方。
任何提示或幫助非常感謝。
我目前使用的代碼是這樣的; (我仍然需要實現分數表並添加記錄,我還使用GUI創建了數據庫,因此沒有設置外鍵,是否有辦法在netbeans中執行此操作,因爲我沒有任何「創建表」代碼。
package football.game;
/*import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;*/
import football.game.DBconnection;
import java.sql.*;
/**
*
* @author Steffan Caine
*/
public class SportsConnection extends DBconnection {
public SportsConnection(final String dbName)
{
this.connectDatabase(dbName);
}
public void insertPlayer(final Integer PLAYERNUM,
final String PLAYERNAME, final String PLAYERPOS, final Integer TEAMNUM)
{
final String insertStmt = "INSERT INTO APP.PLAYERS (PLAYERNUM, PLAYERNAME, PLAYER_POS, TEAM_ID) VALUES (?,?, ?, ?)";
try
{
PreparedStatement pstmt = getConnection().prepareStatement(insertStmt);
pstmt.setInt(1, PLAYERNUM);
pstmt.setString(2, PLAYERNAME);
pstmt.setString(3, PLAYERPOS);
pstmt.setInt(4, TEAMNUM);
pstmt.executeUpdate();
}
catch (SQLException sqle)
{
System.out.println("Exception when inserting player record: " + sqle.toString());
}
}
public void insertTeam(final String NAME, final String MANAGER, final int ID)
{
final String insertStmt = "INSERT INTO APP.TEAMS (TEAMNAME, MANAGER, TEAM_ID) VALUES (?,?, ?)";
try
{
PreparedStatement pstmt = getConnection().prepareStatement(insertStmt);
pstmt.setString(1, NAME);
pstmt.setString(2, MANAGER);
pstmt.setInt(3, ID);
pstmt.executeUpdate();
}
catch (SQLException sqle)
{
System.out.println("Exception when inserting team record: " + sqle.toString());
}
}
public void printAllRecords()
{
this.setQuery(retrieveQuery);
this.runQuery();
ResultSet output = this.getResultSet();
try
{
if (null != output)
{
while(output.next())
{
String PLAYERNUM = output.getString(1);
String PLAYERNAME = output.getString(2);
System.out.println (PLAYERNUM + "\n" + PLAYERNAME + "\n");
}
}
}
catch (SQLException sqle)
{
System.out.println("Exception when printing all students: " + sqle.toString());
}
}
}
的「retrieveQuery」目前返回錯誤信息,任何得到的那部分工作幫助將是巨大的打印記錄了在一個控制檯將增加一些急需的(如基本)功能。
我也有類爲每個窗體(AddPlayer/AddTeam /導航),但我沒有使用構造函數來填充數據庫我是使用方法位於一個Main類,這是一個壞方法去事情,因爲我沒有使用「對象「 因此?
謝謝。
非常感謝您的回答,我不是一個有經驗的程序員,所以您的一些答案對我來說沒什麼意義,我已經擴展了我的問題,如果您可以嘗試幫助更多,我會很感激。 – 2012-02-12 22:06:33
幫助你解決更多問題?你有沒有看到我爲你寫的代碼?我應該寫整個應用程序?我是志願者,而不是付費顧問。你是新的;投入一些努力去學習。 – duffymo 2012-02-12 22:11:26
我的理解你的答案的問題來自於你正在使用類來填充數據庫,並在我原來的應用程序中,我只是在類中使用方法。 在這裏你有: '包持久性; public interface PlayerDao {0找不到(Integer id); List find(); 整數保存(播放器p); void update(Player p); void delete(Player p); }' 這些只是我寫的方法還是這些現有的命令? 我並不是想表現出忘恩負義,我只是在理解你所寫的內容,因爲它與我之前所學的內容不同而苦苦掙扎。 –
2012-02-12 22:19:48