Write a java program to query the StockTracker database for all users and all stocks
held by each user. List the user's ID, first name, and last name followed by their stocks,
including both the stock symbol and stock description (name). This must be a console
application that produces output to the screen.
The following demonstrates my table layout(Top label is actual name of table, bottom labels are actual column names)
//////////
//Stocks//
////////////////
//symbol//name//
////////////////
/////////
//Users/////////////////////////////////////
//userID//lastName//firstName//pswd//admin//
////////////////////////////////////////////
//////////////
//UserStocks//
//////////////////
//userID//symbol//
//////////////////
The code i have attempted to write compiles, but there is an error when I run the actual program. This is the error I recieve: Exception in thread "main" java.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
Here is the code to the program I have created:
public class UserQuery
{
public static void main(String[] args) throws Exception
{
String userID;
String firstName;
String lastName;
String symbol;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:StockTracker";
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
//Headings etc
System.out.println("Stock holdings by user\n");
System.out.println("User ID User Name\n Stock - Description");
System.out.println("-----------------------------------");
//Retrieve data
ResultSet rs = stmt.executeQuery("SELECT s.* FROM Users u, UserStocks us, Stocks s WHERE u.userID=us.userID AND us.stockID=s.name;");
while(rs.next())
{
userID = rs.getString("us.userID");
firstName = rs.getString("u.firstName");
lastName = rs.getString("u.lastName");
symbol = rs.getString("us.symbol");
System.out.println(userID +"\t"+ firstName +"\t"+ lastName +"\t"+ symbol);
}
}
}
I know my error is somewhere with in line 22 so any advice would be greatly appreciated.
Please be patient with me as I like to ask questions so I can learn as much as possible, I have only started with SQL
in my course so my attempts may look silly.

New Topic/Question
Reply




MultiQuote




|