facebook

Can not connect to database

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #263492 Reply

    Anouarzh
    Member

    Hello guys

    I have two computers at home

    One linux server which i use as a server with mysql installed and working.
    And my laptop where i have installed eclipse whit Java.

    I want to connect to my database using mysql-connector-java-3.1.14
    I have installed the driver in eclipse ( added the package )
    Als i have installed the sql plugin of eclipse to help met out, but also that does not work.

    Code:
    
       import java.sql.*;
    
       public class Java_db
       {
           public static void main (String[] args)
           {
              Connection connection = null;
              try {
    //           Load the JDBC driver
              String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
              Class.forName(driverName);
    
    //           Create a connection to the database
              String serverName = "192.xxx.xxx.xxx";
              String mydatabase = "test";
              String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
              String username = "xxx;
              String password = "xxxx";
              connection = DriverManager.getConnection(url, username, password);
              } catch (ClassNotFoundException e) {
    //           Could not find the database driver
                 System.out.println("Could not find Db driver");
              } catch (SQLException e) {
    //           Could not connect to the database
                 System.out.println("Could not connect to the db");
              }
           }
       }

    When i Run this program it says

    Quote:
    Can not connect to db

    Can somebody please help

    Thnx in advance

    Anouar

    #263508 Reply

    Haris Peco
    Member

    Anouar,

    It looks that you set wrong url (probably port number; default for mySQL is 3306) or your database is not configured for remote access

    Could you add :
    e.printStackTrace();

    after

    System.out.println(“Could not connect to the db”);

    Regards,

    #263554 Reply

    Anouarzh
    Member

    This is what i get:

    Could not connect to the DataBase:kdc
    com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

    ** BEGIN NESTED EXCEPTION **

    java.net.UnknownHostException
    MESSAGE: null: null

    STACKTRACE:

    java.net.UnknownHostException: null: null
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:137)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2569)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at db_connect.Connect(db_connect.java:30)
    at Apl.main(Apl.java:9)

    ** END NESTED EXCEPTION **

    Last packet sent to the server was 90 ms ago.
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2643)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at db_connect.Connect(db_connect.java:30)
    at Apl.main(Apl.java:9)

    
     
    
    #263558 Reply

    Haris Peco
    Member

    Anouar,

    You have wrong serverName in URL.Try change
    String serverName = “192.xxx.xxx.xxx”;

    with real IP address (or server name).You can use 127.0.0.1 (localhost) if you are working on local machine

    Regards,

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Can not connect to database

You must be logged in to post in the forum log in