| | |
school task help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
Hi ~ I have done this so far....a entryfield for user to type in the TO and FROM postcode, then I connect to the sql database...but I still cant find out how to READ the X and Y from the sql table then calculate using them..I got the table in database set up...a table of postcodeand its x and y points. Please help~ appreciate alot
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.EventObject;
import java.io.*;
import java.sql.*;
public class EntryCreateDialog extends JDialog
{ private JPanel bottom; /* Contains buttons: */
private JButton okButton, cancelButton;
/* The panel with the entry fields: */
private CreateDialogPanel dialogPanel;
private String defaultFrom = "";
private String defaultTo = "";
private String newFrom;
private String newTo;
public EntryCreateDialog(JFrame owner)
{
super(owner, "Distance Calculating system", true);
/* Create the subcomponents: */
okButton = new JButton("GetDistance");
cancelButton = new JButton("Cancel");
/* Instance of nested
* class for event handling: */
ButtonHandler bHandler = new ButtonHandler();
GetDistanceButton.addActionListener(bHandler);
cancelButton.addActionListener(bHandler);
bottom = new JPanel();
bottom.add(GetDistanceButton); // Uses the implicit flow
bottom.add(cancelButton); // layout of JPanel.
bottom.setBorder(
BorderFactory.createEtchedBorder());
dialogPanel = new CreateDialogPanel();
/* Dialog is given a Border Layout: */
getContentPane().setLayout(new BorderLayout());
/* Put button panel at base of dialog: */
getContentPane().add(bottom,
BorderLayout.SOUTH);
getContentPane().add(dialogPanel,
BorderLayout.CENTER);
}
public void setOldFields(Varchar from, Varchar to)
{ defaultFrom = from;
defaultTo = to;
dialogPanel.setOldFields(from,to);
}
public void setFields(Varchar from, Varchar to)
{ newFrom = from;
newTo = to;
}
public String getFrom()
{ return newFrom; }
public String getTo()
{ return newTo; }
class ButtonHandler
implements ActionListener
{ public void actionPerformed(ActionEvent ev)
{ JButton button = (JButton) ev.getSource();
String label = button.getText();
if ("GetDistance".equals(label))
class customersDB
public customersDb(String customers)
{ try
{ String s = "jdbc:odbc:" + customers;
con = DriverManager.getConnection(s);
System.out.println("Connection established");
entryId = getMaxId();
}
catch(SQLException sqle)
{ handleException(sqle); }
}
private void handleException(SQLException e)
{ e.printStackTrace();
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Code: " + e.getErrorCode());
e.getNextException();
}
}
else /* Cancel pressed */
{ setFields(null,null,null); }
dialogPanel.reset();
setVisible(false); // close dialog.
}
} /* End of nested class */
} /* End of EntryCreateDialog */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.EventObject;
import java.io.*;
import java.sql.*;
public class EntryCreateDialog extends JDialog
{ private JPanel bottom; /* Contains buttons: */
private JButton okButton, cancelButton;
/* The panel with the entry fields: */
private CreateDialogPanel dialogPanel;
private String defaultFrom = "";
private String defaultTo = "";
private String newFrom;
private String newTo;
public EntryCreateDialog(JFrame owner)
{
super(owner, "Distance Calculating system", true);
/* Create the subcomponents: */
okButton = new JButton("GetDistance");
cancelButton = new JButton("Cancel");
/* Instance of nested
* class for event handling: */
ButtonHandler bHandler = new ButtonHandler();
GetDistanceButton.addActionListener(bHandler);
cancelButton.addActionListener(bHandler);
bottom = new JPanel();
bottom.add(GetDistanceButton); // Uses the implicit flow
bottom.add(cancelButton); // layout of JPanel.
bottom.setBorder(
BorderFactory.createEtchedBorder());
dialogPanel = new CreateDialogPanel();
/* Dialog is given a Border Layout: */
getContentPane().setLayout(new BorderLayout());
/* Put button panel at base of dialog: */
getContentPane().add(bottom,
BorderLayout.SOUTH);
getContentPane().add(dialogPanel,
BorderLayout.CENTER);
}
public void setOldFields(Varchar from, Varchar to)
{ defaultFrom = from;
defaultTo = to;
dialogPanel.setOldFields(from,to);
}
public void setFields(Varchar from, Varchar to)
{ newFrom = from;
newTo = to;
}
public String getFrom()
{ return newFrom; }
public String getTo()
{ return newTo; }
class ButtonHandler
implements ActionListener
{ public void actionPerformed(ActionEvent ev)
{ JButton button = (JButton) ev.getSource();
String label = button.getText();
if ("GetDistance".equals(label))
class customersDB
public customersDb(String customers)
{ try
{ String s = "jdbc:odbc:" + customers;
con = DriverManager.getConnection(s);
System.out.println("Connection established");
entryId = getMaxId();
}
catch(SQLException sqle)
{ handleException(sqle); }
}
private void handleException(SQLException e)
{ e.printStackTrace();
while (e != null)
{ System.out.println("SQLState: " + e.getSQLState());
System.out.println("Message: " + e.getMessage());
System.out.println("Code: " + e.getErrorCode());
e.getNextException();
}
}
else /* Cancel pressed */
{ setFields(null,null,null); }
dialogPanel.reset();
setVisible(false); // close dialog.
}
} /* End of nested class */
} /* End of EntryCreateDialog */
You need to check your coding first, you have lot of mistakes there!
and I will give small example of working with database
establish connection with database
Run query (get data grom db)
and close conection
PS: For futhure, can you please use tag for inserting code into post, it is the # (hash) in post toolbar. Thanx
and I will give small example of working with database
establish connection with database
Java Syntax (Toggle Plain Text)
try { conn = DriverManager.getConnection("jdbc:mysql://db_location/" + "database_name?user=user_name&password=user_password"); stmt = conn.createStatement(); } catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); }
Run query (get data grom db)
Java Syntax (Toggle Plain Text)
try { int i=0; String strQuery = "select event_type, ev_date, ev_time, ev_description from calendar_events where userName='" + nUser.getUsername() + "' and cal_id='" + nUser.getCalId() + "'"; rs = stmt.executeQuery( strQuery); while( rs.next()) { eArr[i] = new CalendarData(); eArr[i].setEventType(rs.getString("event_type") ); eArr[i].setEventDate(rs.getString("ev_date") ); eArr[i].setEventTime(rs.getInt("ev_time") ); eArr[i].setEventDescription(rs.getString("ev_description") ); i++; } // end while loop }//end try catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); } // end catch
and close conection
Java Syntax (Toggle Plain Text)
try { if( stmt !=null) stmt.close(); if(conn != null) conn.close(); } catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); }
PS: For futhure, can you please use tag for inserting code into post, it is the # (hash) in post toolbar. Thanx
Last edited by peter_budo; Jan 29th, 2007 at 7:32 pm.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
OK~ so mayb like this? Sorry i seems so stupid...but i really wanna get it working...>.< really appreciate your help ~
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.util.EventObject; import java.io.*; import java.sql.*; public class DistanceCalculationSystem extends JDialog { private JPanel bottom; /* Contains buttons: */ private JButton okButton, cancelButton; /* The panel with the entry fields: */ private CreateDialogPanel dialogPanel; private String defaultFrom = ""; private String defaultTo = ""; private String newFrom; private String newTo; public EntryCreateDialog(JFrame owner) { super(owner, "Distance Calculating system", true); /* Create the subcomponents: */ okButton = new JButton("GetDistance"); cancelButton = new JButton("Cancel"); /* Instance of nested * class for event handling: */ ButtonHandler bHandler = new ButtonHandler(); GetDistanceButton.addActionListener(bHandler); cancelButton.addActionListener(bHandler); bottom = new JPanel(); bottom.add(GetDistanceButton); // Uses the implicit flow bottom.add(cancelButton); // layout of JPanel. bottom.setBorder( BorderFactory.createEtchedBorder()); dialogPanel = new CreateDialogPanel(); /* Dialog is given a Border Layout: */ getContentPane().setLayout(new BorderLayout()); /* Put button panel at base of dialog: */ getContentPane().add(bottom, BorderLayout.SOUTH); getContentPane().add(dialogPanel, BorderLayout.CENTER); } public void setOldFields(Varchar from, Varchar to) { defaultFrom = from; defaultTo = to; dialogPanel.setOldFields(from,to); } public static void main(string[] args) { ConsoleReader in = new ConsoleReader(System.in); var v1 = in.readVar; var v2 = in.readVar; public void setFields(Varchar from, Varchar to) { newFrom = from; newTo = to; } public String getFrom() { return newFrom; } public String getTo() { return newTo; } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ev) { JButton button = (JButton) ev.getSource(); String label = button.getText(); if ("GetDistance".equals(label)) class customersDB { public static void main (String[] args) { try { // Step 1: Load the JDBC driver. Class.forName("com.mysql.jdbc.Driver"); // Step 2: Establish the connection to the database. String url = "jdbc:mysql://127.0.0.1:3306/customers >>postcode_table"; Connection conn = DriverManager.getConnection(url,"crystal","pass"); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } } } try { int i=0; String strQuery = "select post_code, x-co, y-co, from postcode_table where post_code='v1" + nUser.getpost_code() + "' and post_code='v2" + nUser.getpost_code() + "'"; rs = stmt.executeQuery( strQuery); while( rs.next()) { eArr[i] = new data(); eArr[i].setpostcode(rs.getvarchar("postcode") ); eArr[i].setx-co(rs.getdecimal("x1") ); eArr[i].sety-co(rs.getdecimal("y1") ); eArr[n] = new data(); eArr[n].setpostcode(rs.getvarchar("postcode") ); eArr[n].setx-co(rs.getdecimal("x2") ); eArr[n].sety-co(rs.getdecimal("y2") ); i++; } // end while loop a = y2 - y1; b = x2 - x1; diff = Math.sqrt( (a*a) + (b*b) ); System.out.println("Difference: " + df.format(diff)); }//end try catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); } // end catch else /* Cancel pressed */ { setFields(null,null,null); } dialogPanel.reset(); setVisible(false); // close dialog. } } /* End of nested class */ } /* End of EntryCreateDialog */
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
Hi I am the stupid girl again..>< forget about the previous reply..mayb i should make myself abit more clear...this is what i want to have eg
entry field:
From:----------(user enter)
To:-------------(user enter)
then it read what the use typed (eg SW1 and SE1) then it search my DB for SW1 and SE1 and return the x and y co ordinates of SW1 and SE1. then it do the pythagorus on those x and y co ordinates..
is this very wrong?
entry field:
From:----------(user enter)
To:-------------(user enter)
then it read what the use typed (eg SW1 and SE1) then it search my DB for SW1 and SE1 and return the x and y co ordinates of SW1 and SE1. then it do the pythagorus on those x and y co ordinates..
is this very wrong?
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.util.EventObject; public class distancesystem extends JPanel { private JLabel fromLabel; private JTextField fromField; private JLabel toLabel; private JTextField toField; public CreateDialogPanel() { /* Create subcomponents: */ fromLabel = new JLabel("From:"); fromField = new JTextField(); toLabel = new JLabel("To:"); toField = new JTextField(); setBorder( BorderFactory.createTitledBorder("Create fields")); add(fromLabel); // Add the subcomponents in add(fromField); // the order they should appear add(toLabel); // in the dialog. add(toField); } public void setOldFields(Varchar from, Varchar to, ) { fromField.setText(from); toField.setText(to); } public Dimension getPreferredSize() { return new Dimension(400,120); } public Dimension getMinimumSize() { return new Dimension(400,120); } /* Customised layout method, fixed positions (xstart,ystart,width,height) for components: */ public void doLayout() { nameLabel.setBounds(10,10,90,30); nameField.setBounds(100,15,270,20); addressLabel.setBounds(10,40,90,30); addressField.setBounds(100,45,270,20); phoneLabel.setBounds(10,70,90,30); phoneField.setBounds(100,75,270,20); } public void reset() { fromField.setText(""); toField.setText(""); } public String getFrom() { return fromField.getText(); } public String getTo() { return toField.getText(); } } public class DistanceCalculationSystem extends JDialog { private JPanel bottom; /* Contains buttons: */ private JButton okButton, cancelButton; /* The panel with the entry fields: */ private CreateDialogPanel dialogPanel; private String defaultFrom = ""; private String defaultTo = ""; private String newFrom; private String newTo; public EntryCreateDialog(JFrame owner) { super(owner, "Distance Calculating system", true); /* Create the subcomponents: */ okButton = new JButton("GetDistance"); cancelButton = new JButton("Cancel"); /* Instance of nested * class for event handling: */ ButtonHandler bHandler = new ButtonHandler(); GetDistanceButton.addActionListener(bHandler); cancelButton.addActionListener(bHandler); bottom = new JPanel(); bottom.add(GetDistanceButton); // Uses the implicit flow bottom.add(cancelButton); // layout of JPanel. bottom.setBorder( BorderFactory.createEtchedBorder()); dialogPanel = new CreateDialogPanel(); /* Dialog is given a Border Layout: */ getContentPane().setLayout(new BorderLayout()); /* Put button panel at base of dialog: */ getContentPane().add(bottom, BorderLayout.SOUTH); getContentPane().add(dialogPanel, BorderLayout.CENTER); } public void setOldFields(Varchar from, Varchar to) { defaultFrom = from; defaultTo = to; dialogPanel.setOldFields(from,to); } public void setFields(Varchar from, Varchar to) { newFrom = from; newTo = to; } public String getFrom() { return newFrom; } public String getTo() { return newTo; } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent ev) { JButton button = (JButton) ev.getSource(); String label = button.getText(); if ("GetDistance".equals(label)) { setFields(dialogPanel.getFrom(), dialogPanel.getTo(), ); } class customersDB { public static void main (String[] args) { try { // Step 1: Load the JDBC driver. Class.forName("com.mysql.jdbc.Driver"); // Step 2: Establish the connection to the database. String url = "jdbc:mysql://127.0.0.1:3306/customers >>postcode_table"; Connection conn = DriverManager.getConnection(url,"crystal","pass"); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } } } try { int i=0; String strQuery = "select post_code, x-co, y-co, from postcode_table where post_code='to" + nUser.getpost_code() + "' and post_code='from" + nUser.getpost_code() + "'"; rs = stmt.executeQuery( strQuery); while( rs.next()) { eArr[i] = new data(); eArr[i].setpostcode(rs.getvarchar("postcode") ); eArr[i].setx-co(rs.getdecimal("x1") ); eArr[i].sety-co(rs.getdecimal("y1") ); eArr[n] = new data(); eArr[n].setpostcode(rs.getvarchar("postcode") ); eArr[n].setx-co(rs.getdecimal("x2") ); eArr[n].sety-co(rs.getdecimal("y2") ); i++; } // end while loop a = y2 - y1; b = x2 - x1; diff = Math.sqrt( (a*a) + (b*b) ); System.out.println("Difference: " + df.format(diff)); }//end try catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); } // end catch else /* Cancel pressed */ { setFields(null,null,null); } dialogPanel.reset(); setVisible(false); // close dialog. } } /* End of nested class */ } /* End of EntryCreateDialog */
Is there a way to compare two sets of coordinates in a way which will tell you which point is closer to the origin? This is part of an assignment for a comp science class I'm taking.
[The so-and-so trucking company tracks the location of each of its trucks on a grid similar to an (x,y) plane. The home office is at location (0,0)//origin. Each collection contains 4 integers: the x-coordinate and then the y-coordinate of truck A followed by the x&y for truck B.]
I've manipulated the test4.java file to accept keyboard inputs for both sets of coordinates but I'm not sure how to compare the two to each other in relation to the origin.
[The so-and-so trucking company tracks the location of each of its trucks on a grid similar to an (x,y) plane. The home office is at location (0,0)//origin. Each collection contains 4 integers: the x-coordinate and then the y-coordinate of truck A followed by the x&y for truck B.]
I've manipulated the test4.java file to accept keyboard inputs for both sets of coordinates but I'm not sure how to compare the two to each other in relation to the origin.
Last edited by RedQueen; Nov 12th, 2007 at 2:05 pm. Reason: minor typo. add-on
Yes there is.
Now, to find out about it, Start your own thread, rather than hijacking this one, post the code you already have (you have some don't you) and ask a specific question about what is troubling you with it.
Now, to find out about it, Start your own thread, rather than hijacking this one, post the code you already have (you have some don't you) and ask a specific question about what is troubling you with it.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Please help with login page (Community Introductions)
- Argument Not Optional message in VB (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: creating an instant messenger program in java
- Next Thread: LinkedList Implementation Help SOLVED!!!
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






