Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用eclipse连接到mySQL_Java_Mysql_Jdbc Odbc - Fatal编程技术网

Java 使用eclipse连接到mySQL

Java 使用eclipse连接到mySQL,java,mysql,jdbc-odbc,Java,Mysql,Jdbc Odbc,我试图运行一个JAVA程序,将值插入mySQL数据库。当我运行程序时,它说连接被拒绝:连接我也包括了mySQL JAR。有什么问题?有人能帮我吗 Connection refused: connect import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat;

我试图运行一个JAVA程序,将值插入mySQL数据库。当我运行程序时,它说连接被拒绝:连接我也包括了mySQL JAR。有什么问题?有人能帮我吗

Connection refused: connect
 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

}
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
导入java.util.Date;
公共类数据日志{
公共静态void main(字符串[]args)抛出SQLException、ClassNotFoundException{
连接=空;
试一试{
//int i=0;
字符串strDate=“”,strTime=“”;
//注册MySQL JDBC驱动程序,以便
//DriverManager对象。
Class.forName(“com.mysql.jdbc.Driver”);
//获取到数据库的连接。我们准备
//此处的连接信息,如数据库
//url、用户和密码。
String url=“jdbc:mysql://localhost:8080/sampledatabase";
字符串user=“root”;
字符串password=“root”;
connection=DriverManager.getConnection(url、用户、密码);
//从中创建语句对象实例
//联系
语句stmt=connection.createStatement();
//我们将执行一个insert语句。
//首先,您必须创建一个具有
//ID、名称和地址字段。对于ID,您可以使用
//自动编号,而姓名和地址是
//VARCHAR字段。

对于(inti=1;i检查您的mysql服务器是否正在运行,如果它没有运行,您将得到此异常

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

运行服务器

转到
/bin/
并运行mysqld..以启动mysql服务器


即使出现相同错误,也要提供完整的堆栈跟踪。

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

检查8080端口是否可用

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

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

sudo kill
sudo lsof-t-i:8009

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

您能给我们看一下代码吗?这意味着您无法连接到mysql服务器。请确保mysql服务器主机名或其他连接变量没有输入错误,服务器已启动并正在运行,没有防火墙阻止访问等。
            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