2
votes

SQL statement is not executedcom.mysql.jdbc.CommunicationsException: Communications link >failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException MESSAGE: Connection refused

STACKTRACE:

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:546) at java.net.Socket.connect(Socket.java:495) at java.net.Socket.(Socket.java:392) at java.net.Socket.(Socket.java:235) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) at com.mysql.jdbc.Connection.(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:620) at java.sql.DriverManager.getConnection(DriverManager.java:200) at org.jtdemo.preparedst.main(preparedst.java:18)

** END NESTED EXCEPTION **

Last packet sent to the server was 1 ms ago. at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847) at com.mysql.jdbc.Connection.(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:620) at java.sql.DriverManager.getConnection(DriverManager.java:200) at org.jtdemo.preparedst.main(preparedst.java:18)

My program is


package org.jtdemo;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class preparedst
{
 //private static final String y = null;

 public static void main(String arg[])throws Exception
 {
 try
 {
  Class.forName("com.mysql.jdbc.Driver");
  String url="jdbc:mysql://localhost:3036/mylib_db";
  Connection con=DriverManager.getConnection(url,"nikki","dkm007");
  String query = " select c.sub_category, b.title,b.author,b.b_key,ta.available_copies" +
    "from Book_dim b,Category_list c,item_availablity_fact ta" +
   " where sub_category = 'Mathematics' and " + 
    " c.category_id=b.category_id and " + 
   " b.b_key=ta.b_key " ;
  /*ps = con.prepareStatement(" select c.sub_category, b.title,b.author,b.b_key,ta.available_copies" +
     "from Book_dim b,Category_list c,item_availablity_fact ta" +
    " where sub_category = ? and " + 
     " c.category_id=b.category_id and " + 
    " b.b_key=ta.b_key ");*///pass sql query ,no parameter passing
  //ps.String(1, "Mathematics"); // set input parameter
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery(query) ;
  while(rs.next())
  {

  String scat = rs.getString(1);
  String ttl = rs.getString(2);
  String auth = rs.getString(3);
  int bkey = rs.getInt(4);
  int avcop = rs.getInt(5);
  //String b_key;
  System.out.println("subcategory:"+scat+"title:"+ttl+"author:"+auth+"bookkey:"+bkey+"availcopies:"+avcop);

  }

  con.close();
  //ps.close();
 } 
  catch(Exception e)
        {
         System.out.println("SQL statement is not executed");
         e.printStackTrace();
        }

   }
 }

i am using jdk 1.6,mysql-connector-java-5.0.8-bin.jar,Eclipse Version: 3.5.2.

Please help.................

2

2 Answers

2
votes

The exception tells you the problem. If your connection was refused, then there is something wrong with either the configuration of the client or the server. So the first thing to check is the JDBC URL. You need to verify the following:

  • The server is at localhost
  • The server is serving on port 3036 (the default port for MySQL is 3306, so this may be your problem)
  • You have a database on that server named 'mylib_db'
  • Your user name is correct
  • Your password is correct (in the future you may want to use placeholder text like 'password')

If all that information is correct, you need to examine your server to ensure that 'nikki' has permission to connect to the database over the network.

0
votes

It looks like a network problem. It seems the database is not listening on the port you're telling java to connect to.

I would check that the port is indeed listening (via netstat), check any anti-virus, firewall, security programs, etc. If this all comes back good then perhaps a restart of your database program (myssql) would do the trick.