school task help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: school task help

 
0
  #11
Jan 29th, 2007
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 */
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: school task help

 
0
  #12
Jan 29th, 2007
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
  1. try
  2. {
  3. conn = DriverManager.getConnection("jdbc:mysql://db_location/"
  4. + "database_name?user=user_name&password=user_password");
  5. stmt = conn.createStatement();
  6. }
  7. catch(SQLException ex)
  8. {
  9. System.out.println("SQLException : " + ex.getMessage() );
  10. }

Run query (get data grom db)
  1. try
  2. {
  3. int i=0;
  4. String strQuery = "select event_type, ev_date, ev_time,
  5. ev_description from calendar_events where userName='"
  6. + nUser.getUsername() + "' and cal_id='" + nUser.getCalId() + "'";
  7. rs = stmt.executeQuery( strQuery);
  8. while( rs.next())
  9. {
  10. eArr[i] = new CalendarData();
  11. eArr[i].setEventType(rs.getString("event_type") );
  12. eArr[i].setEventDate(rs.getString("ev_date") );
  13. eArr[i].setEventTime(rs.getInt("ev_time") );
  14. eArr[i].setEventDescription(rs.getString("ev_description") );
  15. i++;
  16. } // end while loop
  17. }//end try
  18. catch(SQLException ex)
  19. {
  20. System.out.println("SQLException : " + ex.getMessage() );
  21. } // end catch

and close conection
  1. try
  2. {
  3. if( stmt !=null) stmt.close();
  4. if(conn != null) conn.close();
  5. }
  6. catch(SQLException ex)
  7. {
  8. System.out.println("SQLException : " + ex.getMessage() );
  9. }

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: school task help

 
0
  #13
Jan 30th, 2007
and make sure your database resources are ALWAYS returned by closing them in the finally block of the method in which you created them!
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: school task help

 
0
  #14
Jan 31st, 2007
OK~ so mayb like this? Sorry i seems so stupid...but i really wanna get it working...>.< really appreciate your help
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import javax.swing.border.*;
  6. import java.util.EventObject;
  7. import java.io.*;
  8. import java.sql.*;
  9. public class DistanceCalculationSystem extends JDialog
  10. { private JPanel bottom; /* Contains buttons: */
  11. private JButton okButton, cancelButton;
  12. /* The panel with the entry fields: */
  13. private CreateDialogPanel dialogPanel;
  14. private String defaultFrom = "";
  15. private String defaultTo = "";
  16.  
  17. private String newFrom;
  18. private String newTo;
  19.  
  20. public EntryCreateDialog(JFrame owner)
  21. {
  22. super(owner, "Distance Calculating system", true);
  23. /* Create the subcomponents: */
  24. okButton = new JButton("GetDistance");
  25. cancelButton = new JButton("Cancel");
  26. /* Instance of nested
  27.   * class for event handling: */
  28. ButtonHandler bHandler = new ButtonHandler();
  29. GetDistanceButton.addActionListener(bHandler);
  30. cancelButton.addActionListener(bHandler);
  31.  
  32. bottom = new JPanel();
  33. bottom.add(GetDistanceButton); // Uses the implicit flow
  34. bottom.add(cancelButton); // layout of JPanel.
  35.  
  36. bottom.setBorder(
  37. BorderFactory.createEtchedBorder());
  38. dialogPanel = new CreateDialogPanel();
  39. /* Dialog is given a Border Layout: */
  40. getContentPane().setLayout(new BorderLayout());
  41. /* Put button panel at base of dialog: */
  42. getContentPane().add(bottom,
  43. BorderLayout.SOUTH);
  44. getContentPane().add(dialogPanel,
  45. BorderLayout.CENTER);
  46. }
  47. public void setOldFields(Varchar from, Varchar to)
  48. { defaultFrom = from;
  49. defaultTo = to;
  50. dialogPanel.setOldFields(from,to);
  51.  
  52. }
  53. public static void main(string[] args)
  54. { ConsoleReader in = new ConsoleReader(System.in);
  55.  
  56. var v1 = in.readVar;
  57. var v2 = in.readVar;
  58. public void setFields(Varchar from, Varchar to)
  59. { newFrom = from;
  60. newTo = to;
  61.  
  62. }
  63.  
  64. public String getFrom()
  65. { return newFrom; }
  66. public String getTo()
  67. { return newTo; }
  68.  
  69. class ButtonHandler
  70. implements ActionListener
  71. { public void actionPerformed(ActionEvent ev)
  72. { JButton button = (JButton) ev.getSource();
  73. String label = button.getText();
  74. if ("GetDistance".equals(label))
  75.  
  76. class customersDB {
  77.  
  78. public static void main (String[] args) {
  79. try {
  80. // Step 1: Load the JDBC driver.
  81. Class.forName("com.mysql.jdbc.Driver");
  82. // Step 2: Establish the connection to the database.
  83. String url = "jdbc:mysql://127.0.0.1:3306/customers >>postcode_table";
  84. Connection conn = DriverManager.getConnection(url,"crystal","pass");
  85. } catch (Exception e) {
  86. System.err.println("Got an exception! ");
  87. System.err.println(e.getMessage());
  88. }
  89. }
  90. }
  91. try
  92. {
  93. int i=0;
  94. String strQuery = "select post_code, x-co, y-co,
  95. from postcode_table where post_code='v1"
  96. + nUser.getpost_code() + "' and post_code='v2" + nUser.getpost_code() + "'";
  97. rs = stmt.executeQuery( strQuery);
  98. while( rs.next())
  99. {
  100. eArr[i] = new data();
  101. eArr[i].setpostcode(rs.getvarchar("postcode") );
  102. eArr[i].setx-co(rs.getdecimal("x1") );
  103. eArr[i].sety-co(rs.getdecimal("y1") );
  104.  
  105. eArr[n] = new data();
  106. eArr[n].setpostcode(rs.getvarchar("postcode") );
  107. eArr[n].setx-co(rs.getdecimal("x2") );
  108. eArr[n].sety-co(rs.getdecimal("y2") );
  109.  
  110. i++;
  111. } // end while loop
  112. a = y2 - y1;
  113. b = x2 - x1;
  114. diff = Math.sqrt( (a*a) + (b*b) );
  115. System.out.println("Difference: " + df.format(diff));
  116.  
  117. }//end try
  118. catch(SQLException ex)
  119. {
  120. System.out.println("SQLException : " + ex.getMessage() );
  121. } // end catch
  122.  
  123.  
  124.  
  125.  
  126.  
  127. else /* Cancel pressed */
  128. { setFields(null,null,null); }
  129. dialogPanel.reset();
  130. setVisible(false); // close dialog.
  131. }
  132. } /* End of nested class */
  133. } /* End of EntryCreateDialog */
~
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: school task help

 
0
  #15
Jan 31st, 2007
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?

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import javax.swing.border.*;
  6. import java.util.EventObject;
  7. public class distancesystem extends JPanel
  8. { private JLabel fromLabel;
  9. private JTextField fromField;
  10. private JLabel toLabel;
  11. private JTextField toField;
  12.  
  13. public CreateDialogPanel()
  14. { /* Create subcomponents: */
  15. fromLabel = new JLabel("From:");
  16. fromField = new JTextField();
  17. toLabel = new JLabel("To:");
  18. toField = new JTextField();
  19.  
  20. setBorder(
  21. BorderFactory.createTitledBorder("Create fields"));
  22.  
  23. add(fromLabel); // Add the subcomponents in
  24. add(fromField); // the order they should appear
  25. add(toLabel); // in the dialog.
  26. add(toField);
  27.  
  28. }
  29. public void setOldFields(Varchar from, Varchar to,
  30. )
  31. { fromField.setText(from);
  32. toField.setText(to);
  33.  
  34. }
  35. public Dimension getPreferredSize()
  36. { return new Dimension(400,120); }
  37. public Dimension getMinimumSize()
  38. { return new Dimension(400,120); }
  39. /* Customised layout method, fixed positions
  40.   (xstart,ystart,width,height) for components: */
  41. public void doLayout()
  42. { nameLabel.setBounds(10,10,90,30);
  43. nameField.setBounds(100,15,270,20);
  44. addressLabel.setBounds(10,40,90,30);
  45. addressField.setBounds(100,45,270,20);
  46. phoneLabel.setBounds(10,70,90,30);
  47. phoneField.setBounds(100,75,270,20);
  48. }
  49. public void reset()
  50. { fromField.setText("");
  51. toField.setText("");
  52.  
  53. }
  54. public String getFrom()
  55. { return fromField.getText(); }
  56. public String getTo()
  57. { return toField.getText(); }
  58. }
  59. public class DistanceCalculationSystem extends JDialog
  60. { private JPanel bottom; /* Contains buttons: */
  61. private JButton okButton, cancelButton;
  62. /* The panel with the entry fields: */
  63. private CreateDialogPanel dialogPanel;
  64. private String defaultFrom = "";
  65. private String defaultTo = "";
  66.  
  67. private String newFrom;
  68. private String newTo;
  69.  
  70. public EntryCreateDialog(JFrame owner)
  71. {
  72. super(owner, "Distance Calculating system", true);
  73. /* Create the subcomponents: */
  74. okButton = new JButton("GetDistance");
  75. cancelButton = new JButton("Cancel");
  76. /* Instance of nested
  77.   * class for event handling: */
  78. ButtonHandler bHandler = new ButtonHandler();
  79. GetDistanceButton.addActionListener(bHandler);
  80. cancelButton.addActionListener(bHandler);
  81.  
  82. bottom = new JPanel();
  83. bottom.add(GetDistanceButton); // Uses the implicit flow
  84. bottom.add(cancelButton); // layout of JPanel.
  85.  
  86. bottom.setBorder(
  87. BorderFactory.createEtchedBorder());
  88. dialogPanel = new CreateDialogPanel();
  89. /* Dialog is given a Border Layout: */
  90. getContentPane().setLayout(new BorderLayout());
  91. /* Put button panel at base of dialog: */
  92. getContentPane().add(bottom,
  93. BorderLayout.SOUTH);
  94. getContentPane().add(dialogPanel,
  95. BorderLayout.CENTER);
  96. }
  97. public void setOldFields(Varchar from, Varchar to)
  98. { defaultFrom = from;
  99. defaultTo = to;
  100. dialogPanel.setOldFields(from,to);
  101.  
  102. }
  103. public void setFields(Varchar from, Varchar to)
  104. { newFrom = from;
  105. newTo = to;
  106.  
  107. }
  108.  
  109. public String getFrom()
  110. { return newFrom; }
  111. public String getTo()
  112. { return newTo; }
  113.  
  114. class ButtonHandler
  115. implements ActionListener
  116. { public void actionPerformed(ActionEvent ev)
  117. { JButton button = (JButton) ev.getSource();
  118. String label = button.getText();
  119. if ("GetDistance".equals(label))
  120.  
  121. { setFields(dialogPanel.getFrom(),
  122. dialogPanel.getTo(),
  123. );
  124. }
  125.  
  126. class customersDB {
  127.  
  128. public static void main (String[] args) {
  129. try {
  130. // Step 1: Load the JDBC driver.
  131. Class.forName("com.mysql.jdbc.Driver");
  132. // Step 2: Establish the connection to the database.
  133. String url = "jdbc:mysql://127.0.0.1:3306/customers >>postcode_table";
  134. Connection conn = DriverManager.getConnection(url,"crystal","pass");
  135. } catch (Exception e) {
  136. System.err.println("Got an exception! ");
  137. System.err.println(e.getMessage());
  138. }
  139. }
  140. }
  141. try
  142. {
  143. int i=0;
  144. String strQuery = "select post_code, x-co, y-co,
  145. from postcode_table where post_code='to"
  146. + nUser.getpost_code() + "' and post_code='from" + nUser.getpost_code() + "'";
  147. rs = stmt.executeQuery( strQuery);
  148. while( rs.next())
  149. {
  150. eArr[i] = new data();
  151. eArr[i].setpostcode(rs.getvarchar("postcode") );
  152. eArr[i].setx-co(rs.getdecimal("x1") );
  153. eArr[i].sety-co(rs.getdecimal("y1") );
  154.  
  155. eArr[n] = new data();
  156. eArr[n].setpostcode(rs.getvarchar("postcode") );
  157. eArr[n].setx-co(rs.getdecimal("x2") );
  158. eArr[n].sety-co(rs.getdecimal("y2") );
  159.  
  160. i++;
  161. } // end while loop
  162. a = y2 - y1;
  163. b = x2 - x1;
  164. diff = Math.sqrt( (a*a) + (b*b) );
  165. System.out.println("Difference: " + df.format(diff));
  166.  
  167. }//end try
  168. catch(SQLException ex)
  169. {
  170. System.out.println("SQLException : " + ex.getMessage() );
  171. } // end catch
  172.  
  173.  
  174.  
  175.  
  176.  
  177. else /* Cancel pressed */
  178. { setFields(null,null,null); }
  179. dialogPanel.reset();
  180. setVisible(false); // close dialog.
  181. }
  182. } /* End of nested class */
  183. } /* End of EntryCreateDialog */
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: RedQueen is an unknown quantity at this point 
Solved Threads: 0
RedQueen's Avatar
RedQueen RedQueen is offline Offline
Newbie Poster

Re: school task help

 
0
  #16
Nov 12th, 2007
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.
Last edited by RedQueen; Nov 12th, 2007 at 2:05 pm. Reason: minor typo. add-on
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: school task help

 
0
  #17
Nov 12th, 2007
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC