Results 1 to 3 of 3
  1. #1
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default Trouble linking up to MySql database

    Can anyone check this code and maybe see what I am doing wrong? I have an sql database called MyProfile and I am trying to experiment with it by selecting a field of the client's Name, Email, and amount paid. I want to get those values and system.out to the screen. All that happens is it gives me the path of the jdbc driver. Here's the code, and I welcome any suggestions :

    import javax.swing.*;
    import java.sql.*;

    public class MyProfile
    {
    public static void main(String[] args)
    {
    new MyProfile();



    }
    public MyProfile()
    {
    try
    {
    Statement s = getConnection().createStatement();
    String select = "Select name, email, paid from profile order by name";
    ResultSet rows = s.executeQuery(select);

    while (rows.next())
    {
    String name = rows.getString(1);
    String email = rows.getString(2);
    double paid = rows.getDouble(3);

    System.out.println(name + email + paid);
    }
    }
    catch (SQLException e)
    {
    System.out.println("Exception caught");

    }

    }

    private static Connection getConnection()
    {
    Connection con = null;
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url;
    String user;
    String pw;

    url = "jdbc.mysql://localhost/MyProfile";
    user = "root";
    pw = "joel";
    con = DriverManager.getConnection(url, user, pw);
    System.out.println("Opened successfully");
    }
    catch (ClassNotFoundException e)
    {
    System.out.println(e.getMessage());
    System.exit(0);
    }
    catch (SQLException e)
    {
    System.out.println(e.getMessage());
    System.exit(0);

    }
    return con;
    }



    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,429
    Rep Power
    25

    Default

    it gives me the path of the jdbc driver.
    What statement gives you that?
    Can you show all the output from your program?

    In your catch statements add e.printStackTrace() to get full details on the exception

  3. #3
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I know, great question. It appears on the console, which is weird considering there is no output statement. I also dont think the Class.forName(string) is supposed to output anything either? The only output is on the console : com.mysql.jdbc.Driver

    BTW thanks for the tips on adding the printStackTrace(), although in this case there are no exceptions thrown as it compiles just fine. I also made sure I added MySQL Connector/J (the .jar file) to the classpath.

    Thanks for your help Norm. I welcome any other thoughts or suggestions.

Similar Threads

  1. Trouble using JSP for MySQL database
    By killasaurus tex in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-13-2011, 06:39 AM
  2. JDBC and mySQL database trouble
    By ichwar in forum JDBC
    Replies: 17
    Last Post: 08-16-2010, 11:21 PM
  3. Trouble with jtables from a database
    By mitty in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2010, 03:05 PM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. database with mysql using the netbeans 6.0
    By kwesiaryee in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 04:27 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
  •