The Artima Developer Community
Sponsored Link

Java Answers Forum
Help need integrating program with MS Access

3 replies on 1 page. Most recent reply: Apr 15, 2005 12:17 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
chris

Posts: 6
Nickname: chriskq
Registered: Mar, 2005

Help need integrating program with MS Access Posted: Apr 14, 2005 5:36 PM
Reply to this message Reply
Advertisement
I have completed a java Assignment at uni early, so the teacher asked me to modify my program made to use MS Access for storing secondary memory (as all details where hardcoded in orginal)

Was wondering if someone wanted to help me out as i have no idea of what im really doing now.

The design should also incorporate changes to reflect the need for object oriented programming constructs like inheritance, interfaces and polymorphism (again, im not up to this level at all)

The task is exactly the same but just to use MS Access and inheritance basically.
Below i will leave my working orginal program:::::::::::


chris

Posts: 6
Nickname: chriskq
Registered: Mar, 2005

Re: Help need integrating program with MS Access Posted: Apr 14, 2005 5:36 PM
Reply to this message Reply
/*
* Main.java
*
* Created on 8 March 2005, 10:03
*/


import java.util.*;
/**
*
* @author Owner
*/
import javax.swing.*;

// Class Team
public class Team {
public String teamName;
public String managerName;
public String managerAddress;
Player[] allPlayers = new Player[5];

// Creating player array
public void createPlayers(){
for (int i=0; i < allPlayers.length; i++)
{
allPlayers = new Player();
}
}

//*********MAIN BODY OF PROGRAM**********


public static void main(String[] args) {
int menu = 0, select = 0, temp = 2;
String searchTeam = null;
String search = null;

//create object for each class
League myInit = new League();
Menu myMenu = new Menu(myInit);
Tour myTour = new Tour();

//CALL METHOD "mainmenu" from class menu
JOptionPane.showMessageDialog(null, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n********************************\n\n Welcome to Group A's\n\nSoccer Tournament System\n\n********************************\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
menu = myMenu.mainmenu(menu);
while (menu > 0 && menu < 5){
switch (menu)
{
case 1: JOptionPane.showMessageDialog(null, "The Teams are: \n\n1. " + myInit.team[0].teamName + "\n2. " + myInit.team[1].teamName + "\n3. " + myInit.team[2].teamName + "\n4. " + myInit.team[3].teamName);
menu = myMenu.mainmenu(menu);
break;

case 2: myMenu.selectTeam();
myMenu.teamMenu();
menu = myMenu.mainmenu(menu);
break;

case 3:
JOptionPane.showMessageDialog(null, "Day 1: " + myInit.team[0].teamName + " vs " + myInit.team[1].teamName +
"\nDay 2: " + myInit.team[2].teamName + " vs " + myInit.team[3].teamName +
"\nDay 3: " + myInit.team[0].teamName + " vs " + myInit.team[2].teamName +
"\nDay 4: " + myInit.team[1].teamName + " vs " + myInit.team[3].teamName +
"\nDay 5: " + myInit.team[1].teamName + " vs " + myInit.team[2].teamName +
"\nDay 6: " + myInit.team[0].teamName + " vs " + myInit.team[3].teamName);
menu = myMenu.mainmenu(menu);
break;

case 4: search = myMenu.searchkey(search);
for ( int y = 0 ; y < 4 ; ++y){
for ( int x = 0 ; x < 5 ; ++x)
if (search.equalsIgnoreCase(myInit.team[y].allPlayers[x].name))
searchTeam = myInit.team[y].teamName;
}
if (searchTeam != null)
{
JOptionPane.showMessageDialog(null, "This player is in team: " + searchTeam);
searchTeam = null;
}
else
JOptionPane.showMessageDialog(null, "Player not found. Please enter an existing valid player name");

menu = myMenu.mainmenu(menu);
break;
}


}
}
}
//********END OF MAIN********
class Player
{
String name, address;
}

class Menu{
int choice = 0, temp = 0;
League myIni = new League();

public Menu(League aLeague)
{
myIni = aLeague;
}

public League getLeague()
{
return myIni;
}

public int mainmenu(int option){

String a = JOptionPane.showInputDialog("TEAM MENU \n\nPlease select an option by entering " +
"the corresponding number: \n\n1. Display Teams \n2. Edit a Team \n3. Tournament Schedule \n4. Search \n ");
option = Integer.parseInt(a);

return option;
}

public void selectTeam(){

String b = JOptionPane.showInputDialog("Please choose a team to edit: \n\n1. " + myIni.team[0].teamName + "\n2. " + myIni.team[1].teamName + "\n3. " + myIni.team[2].teamName + "\n4. " + myIni.team[3].teamName);
choice = Integer.parseInt(b);
}

public void teamMenu(){
int option;
String a = JOptionPane.showInputDialog( myIni.team[choice - 1].teamName + " Team Menu" + "\n\nPlease choose an option: \n1. Edit Team Name \n2. Show Players \n3. Edit a player \n4. Edit Roster \n*******************************\n5. Return to Main Menu\n ");
option = Integer.parseInt(a);
switch (option)
{
case 1: editTeamName();
teamMenu();
break;
case 2: showPlayers();
teamMenu();
break;
case 3: selectPlayer();
playerMenu();
break;
case 4: editRoster();
editAddress();
teamMenu();
break;
case 5: break;
}
}
public void editTeamName(){
myIni.team[choice - 1].teamName = JOptionPane.showInputDialog ("Enter a new name");
JOptionPane.showMessageDialog(null, "Name confirmed: \n" + myIni.team[choice - 1].teamName);
}

public void showPlayers(){
JOptionPane.showMessageDialog(null, "The players are: \n\n1. " + myIni.team[choice - 1].allPlayers[0].name + " - " + myIni.team[choice - 1].allPlayers[0].address + "\n2. "
+ myIni.team[choice - 1].allPlayers[1].name + " - " + myIni.team[choice - 1].allPlayers[1].address + "\n3. " + myIni.team[choice - 1].allPlayers[2].name + " - " + myIni.team[choice - 1].allPlayers[2].address + "\n4. " + myIni.team[choice - 1].allPlayers[3].name + " - " + myIni.team[choice - 1].allPlayers[3].address + "\n5. " + myIni.team[choice - 1].allPlayers[4].name + " - " + myIni.team[choice - 1].allPlayers[4].address + "\nManager: " + myIni.team[choice - 1].managerName + " - " + myIni.team[choice - 1].managerAddress);
}
public void selectPlayer(){
String a = JOptionPane.showInputDialog("Which player would u like to edit: \n\n1. " + myIni.team[choice - 1].allPlayers[0].name + "\n2. "
+ myIni.team[choice - 1].allPlayers[1].name + "\n3. " + myIni.team[choice - 1].allPlayers[2].name + "\n4. " + myIni.team[choice - 1].allPlayers[3].name + "\n5. " + myIni.team[choice - 1].allPlayers[4].name);
temp = Integer.parseInt(a) - 1;
}
public void playerMenu(){
String a = JOptionPane.showInputDialog( "NAME: " + myIni.team[choice - 1].allPlayers[temp].name +"\nADDRESS: " + myIni.team[choice - 1].allPlayers[temp].address + "\n\nMenu Options: \n1. Edit Name \n2. Edit Address " +
"\n*******************************\n3. Return to Team Menu \n4. Return to Main Menu\n ");
int b = Integer.parseInt(a);
switch (b){
case 1: editName();
playerMenu();
break;
case 2: editAddress();
playerMenu();
break;
case 3: teamMenu();
break;
case 4: break;
}
}
public void editName(){
myIni.team[choice - 1].allPlayers[temp].name = JOptionPane.showInputDialog ("Enter player's new name");
}
public void editAddress(){
myIni.team[choice - 1].allPlayers[temp].address = JOptionPane.showInputDialog ("Enter new address");
}
public void editRoster(){
String a = JOptionPane.showInputDialog("Which player would u like to replace?: \n\n1. " + myIni.team[choice - 1].allPlayers[0].name + "\n2. "
+ myIni.team[choice - 1].allPlayers[1].name + "\n3. " + myIni.team[choice - 1].allPlayers[2].name + "\n4. " + myIni.team[choice - 1].allPlayers[3].name + "\n5. " + myIni.team[choice - 1].allPlayers[4].name);
temp = Integer.parseInt(a) - 1;
myIni.team[choice - 1].allPlayers[temp].name = JOptionPane.showInputDialog ("Enter new Player's name");
}

public String searchkey(String s){
s = JOptionPane.showInputDialog("Enter search Key:");
return s;
}
}

class League {
Team[] team = new Team[4];

public League()
{
setLeague();
}

public void setLeague()
{
// Team1 creation
team[0] = new Team();
team[0].createPlayers();

team[0].teamName = "Panthers";
team[0].managerName = "Jim";
team[0].managerAddress = "45 Penny st";
team[0].allPlayers[0].name = "Jason";
team[0].allPlayers[1].name = "Frank";
team[0].allPlayers[2].name = "Tom";
team[0].allPlayers[3].name = "AJ";
team[0].allPlayers[4].name = "Clark";

team[0].allPlayers[0].address = "1 clarence st.";
team[0].allPlayers[1].address = "35 lilly st.";
team[0].allPlayers[2].address = "14 watson st.";
team[0].allPlayers[3].address = "62 mack st.";
team[0].allPlayers[4].address = "23 hill st.";

// Team2 Creation

team[1] = new Team();
team[1].createPlayers();

team[1].teamName = "Ducks";
team[1].managerName = "Rob";
team[1].managerAddress = "14 Fern st";
team[1].allPlayers[0].name = "Steve";
team[1].allPlayers[1].name = "Craig";
team[1].allPlayers[2].name = "Jack";
team[1].allPlayers[3].name = "Kaz";
team[1].allPlayers[4].name = "Rich";

team[1].allPlayers[0].address = "1 clarence st.";
team[1].allPlayers[1].address = "35 lilly st.";
team[1].allPlayers[2].address = "14 watson st.";
team[1].allPlayers[3].address = "62 mack st.";
team[1].allPlayers[4].address = "23 hill st.";

// Team3 creation
team[2] = new Team();
team[2].createPlayers();
team[2].teamName = "Man's U's greatest side";

team[2].managerName = "Beven Mahoney";
team[2].managerAddress = "234 Miller St";
team[2].allPlayers[0].name = "Beckhams";
team[2].allPlayers[1].name = "C scholes 4 goals";
team[2].allPlayers[2].name = "Rudde Head Van Nistilroy";
team[2].allPlayers[3].name = "Ronaldo";
team[2].allPlayers[4].name = "Rooney";

team[2].allPlayers[0].address = "1 elegant lane.";
team[2].allPlayers[1].address = "2 yobbo lane";
team[2].allPlayers[2].address = "'station' 42 white st";
team[2].allPlayers[3].address = "62 mackcrap st.";
team[2].allPlayers[4].address = "23 hilleinstein st.";

// Team4 Creation

team[3] = new Team();
team[3].createPlayers();
team[3].teamName = "Nevils";

team[3].managerName = "Micko";
team[3].managerAddress = "34 Barrack st";
team[3].allPlayers[0].name = "chris";
team[3].allPlayers[1].name = "is creating";
team[3].allPlayers[2].name = "these";
team[3].allPlayers[3].name = "new";
team[3].allPlayers[4].name = "teams";

team[3].allPlayers[0].address = "1 clarence st.";
team[3].allPlayers[1].address = "35 lilly st.";
team[3].allPlayers[2].address = "14 watson st.";
team[3].allPlayers[3].address = "62 mack st.";
team[3].allPlayers[4].address = "23 hill st.";
}
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help need integrating program with MS Access Posted: Apr 14, 2005 11:49 PM
Reply to this message Reply
This is a big topic, I can just give you the basics. Don't expect anyone to update your program, because as long as you don't fully understand it, you won't get far.
You have to try it for yourself.


First you need a JDBC-Access Bridge.
I purchased one to connect to the MSDB via ADO, but I know there are free bridges for accessing via ODBC.

You need to study the basics of the SQL language and the API of the "java.sql" package, espescially of Connection, Statement, ResultSet, maybe also SQLException.

Reading data
============
1. You get the Connection object from the bridge.
2. Once you have that, create a Statement,
3. then a ResultSet
Connection con = [driver].createConnection or whatever.
Statement stmt = con.createStatement (optionalparameters);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //as long as there is more data.
  //readdata
}
stmt.close();
con.close();


this is an example of a sql select command:
select name, id, numfield, somethingelse from tablename where name = 'noname' or numfield = 14;

It's allway the same sceme:
SELECT fields
FROM table
WHERE condition
ORDER BY fields;

If you pass the string through jdbc, you must elimiate the last character (';')

You can select from multiple tables by JOINing them on specific fields, you can GROUP if you want t use SUM or MAX and much more, but for a beginner this should be enough for now.

Allmost every jdbc-command throws a SQLException, so you must catch it all the times.

Adding data
===========
The Sql Syntax is like:
insert into tablename(fieldlist) values (valuelist)
Connection con = [driver].createConnection or whatever.
Statement stmt = con.createStatement (optionalparameters);
int nUpdates = stmt.executeUpdate(...);
stmt.close();
con.close();



Updating data
=============
Just like adding data, with a different syntax:
update tablename set field1 = value, field2 = value where id = 16
Don't use where if you want to update all rows.


Hints: Create the connection object when your program starts and close it at program exit, because it takes some time to create.
Creating a statement is also slow. Try to use it as long as possible before closing it.

I created for my personal use a class wich does all those things.
It offers a readSql and a writeSql method. Behind those two methods there are 2 different statements, one is optimized for reading.
I create the statements at program start and destroy them at program end.
If the connection breaks up during runtime, these methods recreate the connection and statements.
So in my program code you won't find stmt.close(), con.close() or
But don't forget to close the resultsets after reading all data.

There exists preparedstatement wich works great if you need to update a lot lof lines.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help need integrating program with MS Access Posted: Apr 15, 2005 12:17 AM
Reply to this message Reply
Something else: if you post code, put java tags before and after it.

Look at the right side of the input field, the tags are listed there.

Flat View: This topic has 3 replies on 1 page
Topic: I want to run this program... Previous Topic   Next Topic Topic: Accessing the function of a thread/runnable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use