Results 1 to 4 of 4
  1. #1
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default Java connecting to sql database

    Alri lads

    Having to use a database, pretty sure I have it connected ok, but getting a error or two.

    Decided to start with the INSERT PART since I thought it would be handiest, rather than working on logging in or anything.

    Java Code:
    public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(submit)) {
                try {
                    Connection con = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/assignment", "root", "");
                    String addressString = address.getText();
                    String phoneNum = phNum.getText();
                    PreparedStatement statement = con
                            .prepareStatement("INSERT INTO 'Order' ('Address', 'PhoneNumber') VALUES ('"
                                    + addressString + "', '" + phoneNum + "');");
                } catch (ClassNotFoundException e1) {
                    JOptionPane.showMessageDialog(null, "Cant Connect to database");
                } catch (SQLException e2) {
                    JOptionPane.showMessageDialog(null, "SQL Problem");
    Theres the code snippet, the try catch is saying its the SQLException thats the problem, but can't see where. I compared it to another INSERT INTO bit of code I got from phpMyAdmin and it looks ok.

    BTW Using eclipse to write the code(have the JDBC Jar in the library) and using wamp for the database.

    Anyone reckon they can see where I went wrong ??

    Cheers for any help provided



    I just want to clarify that YES, this is for a college project, HOWEVER, we were never thought how to use java to connect to a database.. So no smart comments about paying attention in class and stuff please..

    This is just something I genuinely need help in and am willing to research a lot of the stuff myself

    Thanks again
    Last edited by markious; 04-12-2010 at 12:19 PM. Reason: Added Important statement

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Uhm, SQLException does not, necessarily, mean the SQL is the problem. All of the JDBC interfaces can throw an SQLException for a variety of reasons. print the stacktrace and post that.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    In addition, take this code out of the display stuff...take it out of Swing.
    It shouldn't be there.

    It also means you can then test the db code in isolation.

    And there's no point using a PreparedStatement if you're not actually going to bind data to it...that INSERT should be "INSERT into Order (Address, PhoneNumber) VALUES (?,?)", and setString()s should be used for the two parameters.

  4. #4
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Rep Power


    Did the stack trace and got it figured out.. Was using the wrong Syntax.. Also table was order and it didnt like the table name.

    Anyway solved, thanks Guys

Similar Threads

  1. Connecting to Teradata Database from java
    By drkpradeep in forum Advanced Java
    Replies: 5
    Last Post: 02-18-2010, 09:16 AM
  2. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 08:39 AM
  3. connecting to a database
    By kswiss in forum NetBeans
    Replies: 4
    Last Post: 06-25-2009, 06:22 PM
  4. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 02:28 PM
  5. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts