Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Database connection issue

    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.


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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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?

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    hi, I'm using the Eclipse. Now, It is printing 3 and 2 followed by below error.


    Code:
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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?


  •  

    Posting Permissions

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