-1

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
4

4 回答 4

0

检查您的连接参数是否正确(用户名、密码),尤其是运行 MySql 服务器的端口。

于 2013-09-16T07:14:51.830 回答
0

检查你的mysql服务器是否正在运行,如果它没有运行,那么你会得到这个异常

java.net.ConnectException:连接被拒绝:连接

运行服务器

转到<mysql installation path>/bin/并运行 mysqld.. 以启动 mysql 服务器。

即使您遇到相同的错误,也请提供完整的堆栈跟踪。

于 2013-09-16T07:11:39.350 回答
0

我会非常怀疑端口 8080 被用作 MySQL 的端口;它确实应该是端口 3306。因此必须正确检查,因为 8080 通常是应用程序服务器实例(例如 GlassFish 或 Tomcat)的端口号。

于 2013-09-16T08:50:41.173 回答
0

检查您的端口 8080 是否免费

如果您使用的是 linux,请运行以下命令并重新启动服务器。

须藤杀sudo lsof -t -i:8080 须藤杀sudo lsof -t -i:8005 须藤杀sudo lsof -t -i:8009

于 2013-09-16T08:47:58.850 回答