Working my way through a chapter of study and I have been given code where there is two databases made, I have compiled the code and it works. I have no tried to tweek the code so its only one database, when it comes to the selecting the information and displaying it i am just getting a blank no output from it, where have i went wrong im guessing its somewhere in the selection of information from the DB?
Thanks
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
public class FootballTeamDataBase
{
Connection connection;
Statement statement;
ResultSet results;
public FootballTeamDataBase()
{
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
}
catch (ClassNotFoundException cnfe)
{
System.err.println("Derby driver not found.");
}
try
{
// What drivers are there?
System.out.println("Available drivers:");
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements())
{
System.out.println(drivers.nextElement());
}
System.out.println();
connection = DriverManager.getConnection
("jdbc:derby://localhost:1527/MusicShop;create=true;user=admin;pass=admin");
statement = connection.createStatement();
try
{
statement.execute("drop table FootballTeam");
}
catch (SQLException e)
{
System.out.println("Table did not previously exist " + e);
}
// Now create the table.
statement.execute("create table " +
"FOOTBALLTEAM(" +
"ID varchar(12) primary key not null, " +
"POSITION varchar(24), " +
"NAME varchar(24),") ;
}
catch (SQLException sqle)
{
System.err.println("Problem:" + sqle);
}
}
public void addData()
{
try
{
statement.execute("insert into FOOTBALLTEAM values " +
"('1',Keeper','Dale'");
statement.execute("insert into FOOTBALLTEAM values " +
"('2',Defender','Lewis'");
statement.execute("insert into FOOTBALLTEAM values " +
"('3','MIDFIELD','Jones'");
}
catch (SQLException sqle)
{
System.err.println("Problem populating data:" + sqle);
}
}
public void showTeam()
{
try
{
// check the contents of the table
System.out.println("DB contents:");
// select all records
statement.execute("select * from FOOTBALLTEAM");
ResultSet results = statement.getResultSet();
while (results.next())
{
System.out.println(results.getString("ID") + " " + results.getString("POSITION") +
" = " + results.getString("NAME") );
}
}
catch (SQLException e)
{
// nothing wrong
}
}
}