...

View Full Version : Database connection issue



naveendk.55
03-29-2012, 03:38 PM
Hi below code is only printing (3) and not the remaining results. The program in eclipse is not executing from the link "Class.forName". Please let me know if any mistakes in this code. Thank you.





import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBConnection {

public static void main(String s[]) throws SQLException
{


String query = "Select * from project";
String dbtime;
try {
System.out.println(3);
Class.forName("com.mysql.jdbc.Driver");
System.out.println(2);
Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/temp", "root", "12345");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println(1);
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while


} //end try

catch(ClassNotFoundException e) {
e.getStackTrace();
}

catch(SQLException e) {
e.getStackTrace();
}

}

}

Fou-Lu
03-29-2012, 08:55 PM
You haven't done anything with the stacktrace. What does it show was caught in error?
com.mysql.jdbc.Driver is an external entity as well. I assume this resource has been provided during compilation for the DBConnection class?

naveendk.55
03-30-2012, 11:04 AM
hi, I'm using the Eclipse. Now, It is printing 3 and 2 followed by below error.





com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at DBConnection.main(DBConnection.java:18)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
... 12 more

Fou-Lu
03-30-2012, 03:24 PM
That looks to me like you have no service listening. Although the line number in the error does show its on Class.forName, the error itself appears to come from line 20 during the getConnection.
Are you sure mysql is installed and listening on 3306? And that your username and password are valid?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum