Frage

I am trying to run a JAVA program which inserts values into mySQL database. When I run the program, it says

 Connection refused: connect 
I have included mySQL jars also. What is the problem? Can anyone help me out

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;

 public class DataLogs {
  public static void main(String[] args) throws SQLException,ClassNotFoundException {
      Connection connection = null;
      try {
          //int i=0;
          String strDate="", strTime="";
          // Register MySQL JDBC driver to be known by
          // the DriverManager object.
          Class.forName("com.mysql.jdbc.Driver");
          // Get a connection to database. We prepare the
          // connection information here such as database
          // url, user and password.
          String url = "jdbc:mysql://localhost:8080/sampledatabase";
          String user = "root";
          String password = "root";
          connection = DriverManager.getConnection(url, user, password);
          // Create a statement object instance from the
          // connection
          Statement stmt = connection.createStatement();
          // We are going to execute an insert statement.
          // First you have to create a table that has an
          // ID, NAME and ADDRESS field. For ID you can use
          // an auto number, while NAME and ADDRESS are
          // VARCHAR fields.                   

              for(int i=1;i<=100;i++){
                  Date date = new Date();
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                  String formattedDate = sdf.format(date);
                  strDate=getFormatedDate();
                  strTime=getTime();
                  String mm=strDate.substring(3, 5);
                  String yy=strDate.substring(8, 10);
                  String hh=strTime.substring(0, 2);
                  String mi=strTime.substring(3, 5);
                  String ss=strTime.substring(6, 8);
                  String dd=strDate.substring(0, 2);
                  String date1 = ""+yy+"-"+mm+"-"+dd+" "+hh+":"+mi+":"+ss;
                  String sql= "INSERT INTO `site_values_"+i+"` VALUES("+i+",5316,0,0,130,89,904,171,1006,96,4000,"+ss+","+mi+","+hh+","+dd+","+mm+","+yy+",84753,0,0,0,0,0,0,0,0,0,'Short Circuit Shutdown','Shutdown',1,'"+date1+"')";
                  // Call an execute method in the statement object
                  // and passed the sql or query string to it.
                  stmt.execute(sql);

              }

          // After this statement is executed you'll have a
          // record in your users table.
      } catch (ClassNotFoundException e) {
          System.err.println("Could not load database driver!"+e);
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          if (connection != null) {
              connection.close();
          }
      }
  }
  public static String getFormatedDate() {
      String strDate="";
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdfdate = new SimpleDateFormat("dd/MM/yyyy");
      strDate= sdfdate.format(cal.getTime());
      return strDate;
  }//getFormatedDate

  public static String getTime() {
      DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
      java.util.Date date = new java.util.Date();
      String datetime = dateFormat.format(date);
      return datetime;
  } //getTime

}

This is what I see in the console

            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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at solarnmsstandard.DataLogs.main(DataLogs.java:27)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 15 more
War es hilfreich?

Lösung

Check if your connection parameters are correct (username, password) and especially the port where the MySql server is running.

Andere Tipps

check whether your mysql server is running or not, if it is not running then you will get this exception

java.net.ConnectException: Connection refused: connect

to run server

go to <mysql installation path>/bin/ and run mysqld.. to start the mysql server.

even after if you get the same error, provide the full stack trace.

check your port 8080 is free or not

if you are use linux run the follwing command and restart your server.

sudo kill sudo lsof -t -i:8080 sudo kill sudo lsof -t -i:8005 sudo kill sudo lsof -t -i:8009

I would be very suspicious of port 8080 being used as the port for MySQL; it really should be port 3306. So that would have to be checked properly as 8080 is usually the port number for an Application Server instance (such as GlassFish or Tomcat).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top