Java Help Please. Project due in soon!!?

Can someone please look at this piece of code here. I cant seem to find the problem.. I am too good at programming but am sure i can get someones help out there thanks.

import java.awt.EventQueue;

public class GUI {

private JFrame frmResturant;

public JTextField firstNameField;

// Global Fields

Connection con;

Statement st;

ResultSet rs;

// Constructor for the class

/**

* Create the application.

*/

public void connection() {

/**

* The Code Below allows me connect to the sql database Login is the

* name of the database

*

*/

try {

String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // driver string

// stored in the

// database

Class.forName(driver);

String db = "jdbc:odbc:Login"; // db = database string stored in the

// database

con = DriverManager.getConnection(db);

st = con.createStatement();

} catch (Exception ex) {

}

}

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

GUI window = new GUI();

window.frmResturant.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public GUI() {

initialize();

connection();

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frmResturant = new JFrame();

frmResturant.getContentPane().setForeground(Color.WHITE);

frmResturant.setForeground(Color.WHITE);

frmResturant

.setIconImage(Toolkit

.getDefaultToolkit()

.getImage(

GUI.class

.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));

frmResturant.setTitle(" Resturant");

frmResturant.setResizable(false);

frmResturant.setBounds(100, 100, 722, 341);

frmResturant.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frmResturant.getContentPane().setLayout(null);

JLabel lblPleaseCompleteThe = new JLabel("Please Complete The Form ");

lblPleaseCompleteThe.setForeground(new Color(0, 0, 255));

lblPleaseCompleteThe.setBounds(290, 11, 158, 14);

frmResturant.getContentPane().add(lblPleaseCompleteThe);

JLabel lblFirstName = new JLabel("First Name :");

lblFirstName.setBounds(10, 45, 72, 20);

frmResturant.getContentPane().add(lblFirstName);

firstNameField = new JTextField();

firstNameField.setBounds(90, 45, 160, 20);

frmResturant.getContentPane().add(firstNameField);

firstNameField.setColumns(10);

JButton btnButton = new JButton("button");

btnButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

try {

String firstname = firstNameField.getText().trim();

String sql = "insert into login values UserName (" +firstname + "')";

String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // driver

// string

// stored in

// the

// database

Class.forName(driver);

String db = "jdbc:odbc:Login"; // db = database string

// stored in the database

con = DriverManager.getConnection(db);

st = con.createStatement();

rs = st.executeQuery(sql);

} catch (Exception ex) {

}

finally {

try {

st.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

con.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

firstNameField.setText(" this is working");

}

}

});

btnButton.setBounds(114, 99, 89, 23);

frmResturant.getContentPane().add(btnButton);

}

}

Update:

Sorry guys i was meant to say i am not good at programming

2 Answers

Relevance
  • Anonymous
    8 years ago
    Favorite Answer

    You never said what the problem with the code is. Just saying "their is something wrong with my code" is not much to work with. Is it a logic error or syntax error, if it's a logic error what did you meant for it to do, etc. The first obvious thing is that you are missing libraries for many of the objects and functions you use. Much of the code Yahoo cuts out, leaving my having to guess that you used objects and syntax properly. Also there is an error here:

    String sql = "insert into login values UserName (" +firstname + "')";

    Needs to be:

    String sql = "INSERT INTO login VALUES UserName (' " +firstname + " ')";

    Their is a single quote missing on the left side of the string.

    Among other things it's possible their could be other syntax errors that are hard to see like that one. If you have any error messages please provide them so we know where to look.

  • 8 years ago

    lol your too good at programming? dont think i have ever heard some one say that

Still have questions? Get your answers by asking now.