Results 1 to 3 of 3
- 06-05-2011, 08:38 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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(); } } }
- 06-05-2011, 08:40 PM #2
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
by the way, the exception happens in this line
Java Code:conn = DriverManager.getConnection(url+dbName,userName,password);
- 06-06-2011, 03:52 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
And, Make sure your port is correct.Java Code:String url = "jdbc:mysql://[b]192.168.0.1[/b]:3306/bibo";
Similar Threads
-
AutoSizing of windows in GridBagLayout is not working
By greenhorn in forum AWT / SwingReplies: 8Last Post: 04-16-2011, 02:21 PM -
Applet is not working properly on windows 7 with java update 23
By Sagar tandale in forum Java AppletsReplies: 1Last Post: 02-19-2011, 08:36 AM -
Under windows, java2d applet is working in eclipse but not in web browser
By tametick in forum Java AppletsReplies: 1Last Post: 08-19-2010, 12:47 PM -
how to use windows keystore to establise a ssl connection.
By ferdinandfly in forum Java AppletsReplies: 2Last Post: 01-14-2010, 02:36 PM -
Eclipse debug not working on Windows Vista
By mathew.vinay in forum EclipseReplies: 5Last Post: 04-04-2009, 01:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks