Results 1 to 3 of 3
  1. #1
    raac is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default connection to database not working on Windows

    Hello guys, I'm new here, and I was wondering if I can receive any help.

    I was using linux and i was able to make my java applications connect to the database (localhost). Now, I wanted to work on windows using Eclipse. So I downloaded MySql and installed it. (Although it freezed running the security settings, could not figure out the reason). But, I'm able to manage my database through the command-line. So, I added the database and added the driver "mysql-connector-java-5.1.16-bin" to the java build path. When I run it, it would throw the following exception

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ption: Communications link failure

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
    at com.mysql.jdbc.SQLError.createCommunicationsExcept ion(SQLError.java:1116)


    I know for sure it works on linux, but I can't figure out why is not working for windows 7.

    This is the testing code
    Java Code:
    public class TestingDatabase{
      public static void main(String[] args) {
        System.out.println("MySQL Connect Example.");
       Connection conn = null;
    	Statement statement = null;
    	PreparedStatement preparedStatement = null;
    	ResultSet resultSet = null;
    
    	//Connecting to the database from my system VARIABLES
       String url = "jdbc:mysql://localhost:3306/";
       String dbName = "bedrock";
       String driver = "com.mysql.jdbc.Driver";
       String userName = "user1"; 
       String password = "1234567";
    
    
        try {
    		//CONNECTING....
        	System.out.println("1");
          Class.forName(driver).newInstance();
          System.out.println("2");
          conn = DriverManager.getConnection(url+dbName,userName,password);
          System.out.println("3");
          System.out.println("Connected to the database\n\n");
          System.out.println("4");
    		
    		//ALLOWING TO PERFORM QUERIES (executeQuery)
    		statement = conn.createStatement();
    		System.out.println("5");
    		resultSet = statement.executeQuery("select * from employee");
    		
    		
    		//DISPLAYNIG THE QUERIES RESULT
    		System.out.println("Name\t\tDepartment");
    		while(resultSet.next()){
    			System.out.print(resultSet.getString("Name"));
    			System.out.print("\t\t");
    			System.out.println(resultSet.getString("Dept"));
    		}
    
    
    		//INSERTING DATA IN DATABASE (executeUpdate)
    		statement.executeUpdate("insert into employee (Name, Dept, jobTitle)" + "values"
    																									+ "('Mike', 'Business', 'Assistant'),"
    																									+ "('Jane', 'Teacher', 'Professor');");
    
          //DELETING DATA IN DATABASE
          statement.executeUpdate("delete from employee where name = 'Mike'");
    
    		//DISPLAYNIG THE QUERIES RESULT
          System.out.println("\n\nName\t\tDepartment");
    
    		resultSet = statement.executeQuery("select * from employee");
          while(resultSet.next()){
             System.out.print(resultSet.getString("Name"));
             System.out.print("\t\t");
             System.out.println(resultSet.getString("Dept"));
          }
    
    		
    
    		//CLOSING THE CONNECTION
          conn.close();
          System.out.println("\n\nDisconnected from database");
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

  2. #2
    raac is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    by the way, the exception happens in this line

    Java Code:
          conn = DriverManager.getConnection(url+dbName,userName,password);

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Your host is localhost, so you can ONLY connect to database using the machine that has MySQL installed. So if you are trying to connect from other machine (in network) just change your host to the IP address of the machine where MySQL is installed.
    Java Code:
                String url = "jdbc:mysql://[b]192.168.0.1[/b]:3306/bibo";
    And, Make sure your port is correct.

Similar Threads

  1. AutoSizing of windows in GridBagLayout is not working
    By greenhorn in forum AWT / Swing
    Replies: 8
    Last Post: 04-16-2011, 03:21 PM
  2. Replies: 1
    Last Post: 02-19-2011, 09:36 AM
  3. Replies: 1
    Last Post: 08-19-2010, 01:47 PM
  4. how to use windows keystore to establise a ssl connection.
    By ferdinandfly in forum Java Applets
    Replies: 2
    Last Post: 01-14-2010, 03:36 PM
  5. Eclipse debug not working on Windows Vista
    By mathew.vinay in forum Eclipse
    Replies: 5
    Last Post: 04-04-2009, 02:06 PM

Posting Permissions

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