[英]League Table - SQL Query/JDBC Issue
I currently have a very basic app that interacts with a database (Using netbeans and the jdbc) so that you can add teams, players and scores. 我目前有一个非常基本的应用程序,可以与数据库进行交互(使用netbeans和jdbc),以便您可以添加球队,球员和得分。 I now need to be able to display items from each table together in a League Table/Team (With players) Table etc etc.
现在,我需要能够在联赛表/团队(有球员)表等中一起显示每个表中的项目。
My question is how do I go about retrieving the information from the tables and how do I display it, I am literally clueless as to how I should go about it. 我的问题是我该如何从表中检索信息以及如何显示它,我对如何处理一无所知。 I'm assuming I need to do a Join or Select statement (I'm a complete SQL novice) and then use a loop to select each table entry and display it in a table somehow?
我假设我需要执行Join或Select语句(我是一个完整的SQL新手),然后使用循环选择每个表条目并以某种方式在表中显示它?
The only current working features I have are adding to the database, IE add a new team add a new player etc, displaying what is in the tables on the form is where I am stumped. 我目前仅有的工作功能是添加到数据库中,IE添加一个新团队,添加一个新玩家,等等,在表单上显示表格中的内容是我很困惑的地方。
Any tips or help is much appreciated. 任何提示或帮助,不胜感激。
The code I am currently using is this; 我当前正在使用的代码是这样; (I still need to implement a score table and adding records to that, I also created the datbase using the GUI and so have no foreign keys set, is there a way to do this WITHIN netbeans as I have no "Create Table" code anywhere.
(我仍然需要实现一个得分表并向其中添加记录,我还使用GUI创建了datbase,因此没有设置外键,有没有办法在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());
}
}
}
The "retrieveQuery" currently returns an error message, any help getting that part to work would be great as printing the records out in a console would add some much needed (If basic) functionality. 当前,“ retrieveQuery”返回一条错误消息,使该部分正常工作的任何帮助都将非常有用,因为在控制台中打印记录会增加一些非常需要的功能(如果是基本功能)。
I also have classes for each form (AddPlayer/AddTeam/Navigation) but I am not using constructors to populate the database I am instead using Methods located in a Main class, is this a bad way to go about things as I am not using "Objects" as such? 我还为每种表单提供了类(AddPlayer / AddTeam / Navigation),但是我没有使用构造函数来填充数据库,而是使用了位于Main类中的Methods,这是一种不好的处理方式,因为我没有使用“对象”这样吗?
Thanks. 谢谢。
I see three tables: PLAYER, TEAM, and LEAGUE. 我看到三个表:PLAYER,TEAM和LEAGUE。
A TEAM has many PLAYERs; 一个团队有许多玩家。 a LEAGUE has many TEAMs.
联盟有很多团队。 These should be one-to-many relationships, so you'll have foreign keys.
这些应该是一对多关系,因此您将拥有外键。 Here's an example:
这是一个例子:
CREATE TABLE PLAYER (
int id not null auto increment,
first_name varchar(80),
last_name varchar(80),
int team_id,
primary key(id),
foreign key(team_id) references TEAM(id)
);
CREATE TABLE TEAM (
int id not null auto increment,
name varchar(80),
primary key(id)
);
So you might have Java classes like this: 因此,您可能会有类似的Java类:
package model;
public class Player {
private Integer id,
private String name;
// ctors, getters, etc.
}
public class Team {
private Integer id,
private String name,
List<Player> players;
// ctors, getters, etc.
}
You'll have a persistence layer that will have all your SQL in it: 您将拥有一个包含所有SQL的持久层:
package persistence;
public interface PlayerDao {
Player find(Integer id);
List<Player> find();
Integer save(Player p);
void update(Player p);
void delete(Player p);
}
Here's a sample implementation for PlayerDao: 这是PlayerDao的示例实现:
package persistence;
public class PlayerDaoImpl implements PlayerDao {
private static final String SELECT_ALL = "SELECT id, name FROM PLAYER ";
private static final String SELECT_BY_ID = SELECT_ALL + "WHERE id = ?";
private Connection connection;
public PlayerDaoImpl(Connection connection) {
this.connection = connection;
}
public Player find(Integer id) {
Player p = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = this.connection.prepareStatement(SELECT_BY_ID);
ps.setInteger(1, id);
rs = ps.executeQuery();
while (rs.next()) {
Integer pid = rs.getInteger("id");
String name = rs.getString("name");
p = new Player(id, name);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DatabaseUtils.close(rs);
DatabaseUtils.close(ps);
}
return p;
}
}
Printing records in consoles or user interfaces would indeed be useful, but that should be done by different classes in different packages. 在控制台或用户界面中打印记录确实很有用,但是应该由不同程序包中的不同类来完成。 Have a view tier that handles that stuff.
有一个可以处理这些内容的视图层。 Classes should do one thing well.
上课应该做得很好。 You should think about layering your applications appropriately.
您应该考虑适当地分层应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.