Results 1 to 16 of 16

Thread: no connection

  1. #1
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default no connection

    Hi,i'm trying to extabilish a connection with a Mysql database.
    The line to load the driver should be:

    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    Once you loaded the driver the line to connect should be:

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB1?","", "password");

    it doesn't work at all.
    Does anyone know the reason? I don't have a username since Mysql doesn't require it,could the "blank space" argument be the problem?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default

    What the error message that you get when executing the code? What is you database name? is "DB1?" or just "DB1"?

  3. #3
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default no connection

    ..the DB name is DB1 and the exception message is NullPointerException...

  4. #4
    Cazrin is offline Member
    Join Date
    Dec 2007
    Posts
    16
    Rep Power
    0

    Default

    Looking at your code your database name is wrong, like wsaryada said, you've got "DB1?" written in, whereas it should be "DB1". Everything else seems fine, although you could try setting up a user and seeing if that's causing the problem.

    Also, maybe you don't need to include the port. The slice of code I found doesn't, and I'm not sure if it's required

    Java Code:
    String userName = "testuser";
    String password = "testpass";
    String url = "jdbc:mysql://localhost/test";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);

  5. #5
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default ...no connection

    nothing to do,here's the whole code...it's about a small DB table to menage phone numbers...i've used "PreparedStatement" to implement the three op of the application...

    package accessidb;
    import java.lang.Exception;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class LoadDriver // this is an external class just to load the driver
    {
    public LoadDriver() { }
    public void load()
    {
    try {
    // The newInstance() call is a work around for some
    // broken Java implementations

    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    } catch (Exception ex) {
    // handle the error
    }
    }
    }

    public class Sql {


    private Connection con;



    /** Creates a new instance of Sql */
    public Sql()
    {
    try {
    LoadDriver LD = new LoadDriver();
    LD.load();
    String URL = "jdbc:mysql://localhost/db1";
    String user = " ";
    String pass = "wordpass";
    con = DriverManager.getConnection(URL,user,pass);


    }
    catch (SQLException ecc) { }


    }

    public void insert (String name,String surname,int phone)
    {

    try {
    PreparedStatement ins = con.prepareStatement("INSERT INTO Rubrica (Name,Surname,Phone) VALUES (?, ?, ?)");
    ins.setString(1,name);
    ins.setString(2,surname);
    ins.setInt(3,phone);

    }
    catch (SQLException ecc) { }
    }

    public void update (String name,String surname,int newphone)
    {
    try {
    PreparedStatement update = con.prepareStatement("UPDATE Rubrica SET Phone = ? WHERE Name = ? AND Surname = ? ");
    update.setInt(1,newphone);
    update.setString(2,name);
    update.setString(3,surname);

    }
    catch (SQLException ecc) { }

    }

    public void delete (String name,String surname)
    {
    try {

    Statement delete = con.createStatement();
    delete.execute("DELETE FROM Rubrica WHERE Name = '" + name + "' AND Surname = '" + surname + "'" );


    }
    catch (SQLException ecc) { }

    }


    }

    ...nothing to do...it's like it can't find the DB at all!

  6. #6
    Cazrin is offline Member
    Join Date
    Dec 2007
    Posts
    16
    Rep Power
    0

    Default

    Try printing out the error in your catch() :

    Java Code:
    }
    catch (SQLException ecc) { System.out.println(ecc); }
    See what it outputs and you can test that at each point to see where it's failing.

  7. #7
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default ...no connection

    ...it fails when it's trying to read the DB entries...i tried to debug it and it fails there...it compile the statement

    con = DriverManager.getConnection(URL,user,pass);

    but it finds an error at the line

    Statement delete = con.createStatement();

    it's like the connection done at the first line didn't exist...bye

  8. #8
    Cazrin is offline Member
    Join Date
    Dec 2007
    Posts
    16
    Rep Power
    0

    Default

    Should Statement delete = con.createStatement(); not be PreparedStatement delete = con.createStatement(); like your other lines?

    I'm no JDBC or SQL expert, nor novice, but if your code is failing at that line, that's the only difference I can see.

  9. #9
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default ...no connection

    ...oh no the syntax is right...i've done a test just to delete a row of the table...but the problem is the connection...

  10. #10
    Cazrin is offline Member
    Join Date
    Dec 2007
    Posts
    16
    Rep Power
    0

    Default

    I wish I could help more but this seems to be out of my knowledge, I haven't started to touch JDBC yet. Although I've found this URL, which seems to use a different method for executing queries:

    How to use JDBC

    Hopefully that can help you where I can't!

  11. #11
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default ..no connection

    ...thanks a lot,anyway...

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default

    Hello, have you solve the problem?

    As cazrin said, you can put something like e.printStackTrace() in your catch block, so it will print the error message when something is happening.

    The NPE could be caused by the connection, but prior to that what make the connection null in the first place. It could be the database name is wrong, the user name or password to connection to database might be invalid, or event the database is not running.

  13. #13
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default ...no connection

    ...i tried to print the StuckTrace,it seems to be the missing username,but when it configures mysql username it's not required,it can leave "root administrator" and insert the password only...that's the reason why i inserted an empty string for username...i've to configure mysql again...

  14. #14
    Cazrin is offline Member
    Join Date
    Dec 2007
    Posts
    16
    Rep Power
    0

    Default

    Have you tried inserting "root" as the username? That's what I use on my localhost.

  15. #15
    even is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default

    yeah,i've already tried with "root" but nothing to do...

  16. #16
    anki1234 is offline Member
    Join Date
    Dec 2007
    Posts
    28
    Rep Power
    0

Similar Threads

  1. connection problem
    By subash in forum JDBC
    Replies: 5
    Last Post: 04-22-2008, 09:17 AM
  2. JSP - using connection cache
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 09:54 AM
  3. Database connection
    By oaklander in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 11:11 PM
  4. JDBC connection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:39 PM
  5. connection pool for db2
    By paty in forum JDBC
    Replies: 1
    Last Post: 08-06-2007, 02:43 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
  •