2010-01-20 50 views
5

Tôi gặp vấn đề cố gắng để có được một kết nối cơ sở dữ liệu bằng cách sử dụng mã bên dưới:Không thể thực hiện kết nối JDBC để MySQL (sử dụng Java, IntelliJ, và Linux)

try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Properties p = new Properties(); 
     p.put("user", user_name); 
     p.put("password", password); 
     connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jsp_test", p); 
    } catch (SQLException ex) { 
     // handle any errors 
     ex.printStackTrace(); 
     System.out.println("SQLException: " + ex.getMessage()); 
     System.out.println("SQLState: " + ex.getSQLState()); 
     System.out.println("VendorError: " + ex.getErrorCode()); 
     return false; 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

Các thông báo lỗi được outputted là:

/usr/lib/jvm/java-6-openjdk/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/usr/bin/idea/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/about.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar:/home/bedtimes/Java Projects/db_demo/out/production/db_demo:/opt/java/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar:/usr/bin/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain Main com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284) at java.sql.DriverManager.getConnection(DriverManager.java:620) at java.sql.DriverManager.getConnection(DriverManager.java:169) at database.Database.connect(Database.java:80) at Main.main(Main.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110) Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:343) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137) ... 18 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384) at java.net.Socket.connect(Socket.java:542) at java.net.Socket.connect(Socket.java:492) at java.net.Socket.(Socket.java:389) at java.net.Socket.(Socket.java:232) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:292) ... 19 more SQLException: 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. SQLState: 08S01 VendorError: 0

Process finished with exit code 0

Tôi, theo nghĩa đen, không biết cách khắc phục sự cố thông báo lỗi này. Cơ sở dữ liệu tồn tại. Tên người dùng và mật khẩu tồn tại. Tôi hiện không thêm bất kỳ bảng nào vào cơ sở dữ liệu nhưng tôi không nghĩ đó có thể là vấn đề vì tôi chỉ tạo một kết nối sau khi tất cả ...

Tôi có thể cung cấp thêm thông tin nếu cần. Tôi cảm thấy như tôi đã thử rất nhiều. Có ai biết bất kỳ cách nào để có thêm thông tin về cách thức và lý do tại sao nó không thành công?

Cảm ơn sự giúp đỡ của bạn! :)

+0

Bạn có thể kết nối trực tiếp với nó (sử dụng ứng dụng khách mysql) không? Bit hữu ích duy nhất của traceback là '08S01' và tài liệu mysql đề cập đến một số thứ về nó (http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-troubleshooting.html#qandaitem- 20-3-5-3-1-3). – shylent

+0

Bạn đã thử sử dụng url "jdbc: mysql: // localhost/jsp_test" chưa? Chủ yếu chỉ là một phỏng đoán ngẫu nhiên. –

+0

Tôi có thể kết nối trực tiếp bằng cách sử dụng mysql client và tên người dùng của tôi 'jsp_test'. Tôi vẫn không thể kết nối nếu tôi thay đổi 127.0.0.1 thành localhost. Cảm ơn bạn đã liên kết, tôi sẽ xem xét. Tôi tự hỏi nếu ai đó khác có thể thử làm điều tương tự với máy riêng của họ và mysql-connector-java-5.1.10-bin.jar – olive

Trả lời

6

Từ chuỗi kết nối của bạn, cơ sở dữ liệu này phải nằm trên máy cục bộ. Bạn có thể thử chạy lệnh này để đảm bảo ổ cắm được mở cho các kết nối không?

telnet 127.0.0.1 3306

và đảm bảo rằng nó kết nối? Có thể bạn chưa cấu hình cá thể mysql của mình để nghe các kết nối từ máy này hoặc trên địa chỉ giao diện này. Nếu kết nối thất bại, bạn cần phải sửa đổi cấu hình mysql của bạn, ví dụ:

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

+0

[bedtimes @ seb ~] $ telnet 127.0.0.1 3306 Đang thử 127.0.0.1. .. telnet: Không thể kết nối với máy chủ từ xa: Kết nối bị từ chối ... Có vẻ như bạn có thể đúng!Tôi sẽ đi và thử hướng dẫn đó và sau đó lấy lại cho bạn nếu nó sửa vấn đề. C: – olive

+1

Tôi hiện đang nhận: [bedtimes @ seb /] $ telnet localhost 3306 Đang thử 127.0.0.1 ... Được kết nối với localhost. Nhân vật thoát là '^]'. Kết nối do máy chủ nước ngoài đóng. ... Không ai biết điều này có nghĩa là gì? – olive

+0

Sửa lỗi cuối cùng là bằng cách thay đổi hosts.allow như được chỉ định trên trang này: http://bbs.archlinux.org/viewtopic.php?pid=346836#p346836 Tổng số PITA. Cảm ơn! C: – olive

1

tôi đã có triệu chứng tương tự nhưng đối với tôi là giải pháp là để thay đổi bind-address để 0.0.0.0 trong/etc/mysql/my.cnf

(Chỉ cần gửi bài này cho những người khác tìm kiếm một câu trả lời cho câu hỏi này)

+0

Tôi hoàn toàn loại bỏ đường liên kết địa chỉ và nó cố định cùng một vấn đề cho tôi. – jgritty

1

Bạn có thể thử này để thay thế:

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_test","user_name","password"); 
1

yo u có thể thử một trong những cách sau

"jdbc:mysql://localhost:3306/jsp_test","username","password" 

hoặc

"jdbc:mysql://'your machines ip (without quote)':3306/jsp_test","username","password" 

hoặc

"jdbc:mysql://127.0.0.1:3306/jsp_test","username","password" 

như là một chuỗi kết nối.

Các vấn đề liên quan