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
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();
}
}
}