Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2007, 08:01 AM
Member
 
Join Date: Dec 2007
Posts: 2
SilentCodingOne is on a distinguished road
Help needed with updating mysql database
I have an application that is designed to manage the flights for an airline. I'm trying to store the flight info in a mysql database. I'm able to establish a connection to the database and send the info from my GUI to the appropriate method to add flight, delete flight, and search for a flight. I have tried a number of things and I must be doing something wrong because it is not working. I was getting a null pointer exception which I have corrected via the updated code below in the 2nd code block. However the records on the database are still not updating. Any help would be appreciated.

Thanks

Code:
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.color.*; import java.io.*; import java.util.*; import java.text.*; import javax.swing.border.*; import java.sql.*; import javax.swing.*; public class FlightManager extends JFrame implements ActionListener{ // JDBC driver name and database url static final String DRIVER = "com.mysql.jdbc.Driver"; static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager"; // jtextfield components private JTextField fn = new JTextField(15); private JTextField oc = new JTextField(15); private JTextField dc = new JTextField(15); private JTextField dt = new JTextField(15); // labels telling user what each jtextfield is for private JLabel fnl = new JLabel("Flight Number: "); private JLabel ocl = new JLabel("City of Origin: "); private JLabel dcl = new JLabel("Destination City: "); private JLabel dtl = new JLabel("Departure Time: "); // variables that hold data added to jtextfield private int flightNum; private String originCity; private String destinationCity; private String departure; // jbutton components private JButton add; private JButton remove; private JButton search; private JButton exit; private DatabaseUpdate du = new DatabaseUpdate(); public static void main(String[] args) throws SQLException { try { Connection connection = null; Statement statement = null; ResultSet resultSet = null; Class.forName(DRIVER); connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } FlightManager fm = new FlightManager(); Container pc = new Container(); pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS)); JPanel blank = new JPanel(); pc.add(blank); pc.add(fm.getFlightInputPanel()); pc.add(fm.getOriginPanel()); pc.add(fm.getDestinationPanel()); pc.add(fm.getDepartureTimePanel()); JFrame f = new JFrame(); f.setTitle("Flight Manager"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(pc, "First"); f.add(fm.getButtonPanel(), "Last"); // adds button panel to JFrame f.setSize(500, 200); f.setLocation(200,200); f.setVisible(true); } private JPanel getFlightInputPanel(){ FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER); JPanel fnp = new JPanel(); fnp.add(fnl); fnp.add(fn); return fnp; } private JPanel getOriginPanel(){ FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER); JPanel ocp = new JPanel(); ocp.add(ocl); ocp.add(oc); return ocp; } private JPanel getDestinationPanel(){ FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER); JPanel dcp = new JPanel(); dcp.add(dcl); dcp.add(dc); return dcp; } private JPanel getDepartureTimePanel(){ FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER); JPanel dtp = new JPanel(); dtp.add(dtl); dtp.add(dt); return dtp; } private JPanel getButtonPanel(){ FlowLayout buttonLayout = new FlowLayout(FlowLayout.CENTER); JPanel buttonPanel = new JPanel(); add = new JButton("Add Flight"); add.addActionListener(this); remove = new JButton("Delete Flight"); remove.addActionListener(this); search = new JButton("Search For Flight"); search.addActionListener(this); exit = new JButton("Exit"); exit.addActionListener(this); buttonPanel.add(add); buttonPanel.add(remove); buttonPanel.add(search); buttonPanel.add(exit); return buttonPanel; } // processes actions for when buttons are pushed public void actionPerformed(ActionEvent e) { Object ac = e.getSource(); // action to take if add flight button is pushed if(ac == add){ flightNum = Integer.parseInt(fn.getText()); originCity = oc.getText(); destinationCity = dc.getText(); departure = dt.getText(); du.addFlight(flightNum, originCity, destinationCity, departure); // clears out current contents of JTextFields fn.setText(""); oc.setText(""); dc.setText(""); dt.setText(""); } // action to take if search for flight is pushed if(ac == search){ originCity = oc.getText(); destinationCity = dc.getText(); du.searchFlight(originCity, destinationCity); oc.setText(""); dc.setText(""); } if(ac == remove){ flightNum = Integer.parseInt(fn.getText()); originCity = oc.getText(); destinationCity = dc.getText(); departure = dt.getText(); du.removeFlight(flightNum, originCity, destinationCity, departure); } // action to take if exit button is pushed if(ac == exit){ System.exit(0); } } }
Code:
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.color.*; import java.io.*; import java.util.*; import java.text.*; import javax.swing.border.*; import java.sql.*; import javax.swing.*; public class DatabaseUpdate { // JDBC driver name and database url static final String DRIVER = "com.mysql.jdbc.Driver"; static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager"; Connection connection = null; Statement statement = null; ResultSet resultSet = null; public DatabaseUpdate(){ try { Class.forName(DRIVER); connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); }catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } public void addFlight(int flightNum, String originCity, String destinationCity, String departure) { int flight = flightNum; String origin = originCity; String destination = destinationCity; String departTime = departure; try{ statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); connection.setAutoCommit(false); statement.addBatch("INSERT INTO flightinfo(flightnumber, origincity, destinationcity, departuretime) VALUES(flight, 'origincity', 'destinationcity', 'departuretime')"); int[] updateCounts = statement.executeBatch(); connection.commit(); resultSet = statement.executeQuery("SELECT * FROM flightinfo"); } catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } public void removeFlight(int flightNum, String originCity, String destinationCity, String departure) { int flight = flightNum; String origin = originCity; String destination = destinationCity; String departTime = departure; try{ statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); connection.setAutoCommit(false); statement.addBatch("DELETE FROM flightinfo WHERE FlightNumber = 'flight'"); int[] updateCounts = statement.executeBatch(); connection.commit(); // String sql = "DELETE FROM flightinfo WHERE FlightNumber = flight"; // int delete = statement.executeUpdate(sql); // if(delete == 1){ System.out.println("Row is deleted."); // } // else{ // System.out.println("Row is not deleted."); // } } catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } void searchFlight(String originCity, String destinationCity) { String origin = originCity; String destination = destinationCity; try{ }catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } }

Last edited by SilentCodingOne : 12-11-2007 at 05:55 PM. Reason: updated code due to figuring out part of issue
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-12-2007, 12:23 AM
Member
 
Join Date: Dec 2007
Posts: 2
SilentCodingOne is on a distinguished road
Everyone can disregard as I was finally able to resolve all issues
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java program that transfers stock prices to MySQL database naipulb New To Java 2 04-17-2008 07:02 PM
problem in connecting to mysql database nancyv Java Servlet 6 04-02-2008 01:33 PM
Problem with jTable that is binded with a table in MySQL Database rajkenneth NetBeans 0 03-29-2008 05:36 PM
connecting to mysql database javagal NetBeans 2 08-04-2007 02:36 PM
Updating Graphics Greedful Java 2D 2 07-20-2007 09:12 PM


All times are GMT +3. The time now is 03:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org