Skip to main content

Problem with getting results from Access databae using applet

2 replies [Last post]
maltina1
Offline
Joined: 2006-02-17
Points: 0

hi,please help as soon as possible.I had the application that was communicating with the database,but now that I tried to convert it to an Applet,it doesn't show results anymore.please take a look at my code and tell me where I went wrong.

thank you in advance
// PMC_AutoJ team
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.sql.*;
import java.io.*;
import java.util.*;

public class PMC_AutoJ extends Applet {

static PMC_AutoJ theApp;
final static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
final static String url = "jdbc:odbc:Personnel";
final static String error = "ERROR";
final static String genexception = "GENERAL EXCEPTION";
final static String sqlexception = "SQL EXCEPTION";
final static String Search = "Search";

String sql = ""; // Used to store sql statements

Label PersonIDLabel = new Label("PersonID:");
TextField PersonIDField;
Label TimeTakenLabel = new Label("TimeTaken:");
TextField TimeTakenField;
int PersonID;
int TimeTaken;
Font smallArialFont = new Font("Arial", Font.PLAIN, 15);
// Font for for month/year caption above calendar.
Font largeArialFont = new Font("Arial", Font.BOLD, 30);

public static void main(String [] args){
theApp=new PMC_AutoJ();
theApp.init();
}
public void init()
{
Button SearchButton = new Button("Search");

setBackground(Color.pink);
//TimeTakenField.addActionListener(this);
PersonIDLabel.setFont(smallArialFont);
add(PersonIDLabel);
PersonIDField = new TextField(30);
// PersonIDField.setText("");
add(PersonIDField);
//PersonIDField.addActionListener(this);
TimeTakenLabel.setFont(smallArialFont);
add(TimeTakenLabel);
TimeTakenField = new TextField(30);
TimeTakenField .setEditable(false);
//TimeTakenField.setText("");
add(TimeTakenField);

// "New calendar" button.
SearchButton.setFont(smallArialFont);
add(SearchButton);
SearchButton.addActionListener(new ButtonHandler());

try{ // load MS Access driver
Class.forName(driver);
}catch(Exception ex){}

}
//--------------------Start calculation--------------------------------------------->>>
//Search the student record using teamname as the search key
/* public void CalculateTeamTotalsPerSemester(){

String studioTeam = tfTeamName12.getText();

sql = "SELECT * FROM persons WHERE StudioTeamName='"+studioTeam+"'";

try{

// load MS Access driver
Class.forName(driver);
}catch(java.lang.ClassNotFoundException ex){

JOptionPane.showMessageDialog(null,ex.getMessage(), error ,
JOptionPane.PLAIN_MESSAGE);
}

try{
// setup connection to DBMS
Connection conn = DriverManager.getConnection(url);

// create statement
Statement stmt = conn.createStatement();

boolean hasResults = stmt.execute(sql);

if(hasResults){

ResultSet rs = stmt.getResultSet();
//method to calculate
addTeamTimeTaken(rs);
}

}catch(Exception ex){

JOptionPane.showMessageDialog(null,ex.getMessage(), genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End search()----------------------------------------------->>>*/

//--------------------Start search()--------------------------------------------->>>
//Search the student record using both Person's id and weeknumber as the search key
public void CalculateIndividualSemesterTotal(){

PersonID = Integer.parseInt(PersonIDField.getText(),10);

sql = "SELECT * FROM persons WHERE person_id="+PersonID;

/* try{

// load MS Access driver
Class.forName(driver);
}catch(java.lang.ClassNotFoundException ex){

JOptionPane.showMessageDialog(null,ex.getMessage(), error ,
JOptionPane.PLAIN_MESSAGE);
}*/

try{
// setup connection to DBMS
Connection conn = DriverManager.getConnection(url);

// create statement
Statement stmt = conn.createStatement();

boolean hasResults = stmt.execute(sql);

if(hasResults){
ResultSet rs = stmt.getResultSet();

//method to calculate
addStudentTimeTaken(rs);
repaint();
}

}catch(Exception ex){

JOptionPane.showMessageDialog(null,ex.getMessage(), genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End search()----------------------------------------------->>>

//------------------------Start times----------------------------------------
////////////////////////////////////////////////////////////////////////////////
//--------------------Start addTimes()------------------------------>>>
void addStudentTimeTaken(ResultSet rs) throws SQLException{
double totaltime=0;
double timetakenperweek;
while(rs.next()){

double num0=Double.parseDouble(rs.getString("MSD"));
double num1=Double.parseDouble(rs.getString("methods"));

double num2=Double.parseDouble(rs.getString("analysis"));
double num3=Double.parseDouble(rs.getString("architectures"));

double num4=Double.parseDouble(rs.getString("models"));
double num5=Double.parseDouble(rs.getString("studio"));

timetakenperweek=num0+num1+num2+num3+num4+num5;

totaltime = totaltime+timetakenperweek;
}

TimeTakenField.setText(""+totaltime);
repaint();
}

//----------------------End addindividual----------------------------//
//////////////////////////////////////////////////////////////////////
//--------------------Start addTimes()----------------------------->>>
/*void addTeamTimeTaken(ResultSet rs) throws SQLException{
double totaltime=0;

while(rs.next()){

double num1=Double.parseDouble(rs.getString("studio"));
totaltime = totaltime+num1;
}
tfTeamTotalPerSemester.setText(""+totaltime);

}*/

/////////////////////////////////////////////////////////////////////////////////////
//--------------------Start ButtonHandler--------------------------------------->>>

// Action listener for the buttons on each screen
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// get the label of the button
String action = e.getActionCommand();

if (action!=null){
if(action==Search){
CalculateIndividualSemesterTotal();

}

}

}
}
//--------------------End ButtonHandler----------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13
Points: 0

Questions like this have a (very) low change of getting an answer; Please read: http://www.catb.org/~esr/faqs/smart-questions.html

brinkley
Offline
Joined: 2003-06-06
Points: 0

Question doesn't belong in JavaHelp forum. It isn't a general help forum.