我正在尝试运行一个将值插入mySQL数据库的JAVA程序。当我运行该程序时,它说
Connection refused: connect我也包含了mySQL jar。问题是什么?任何人都可以帮助我
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
}
这是我在控制台中看到的
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
答案 0 :(得分:0)
检查你的mysql服务器是否正在运行,如果它没有运行,那么你将得到这个异常
java.net.ConnectException:连接被拒绝:连接
运行服务器
转到<mysql installation path>/bin/
并运行mysqld ..启动mysql服务器。
即使你得到相同的错误,也要提供完整的堆栈跟踪。
答案 1 :(得分:0)
检查您的连接参数是否正确(用户名,密码),尤其是运行MySql服务器的端口。
答案 2 :(得分:0)
检查您的端口8080是否免费
如果您使用linux,请运行follwing命令并重新启动服务器。
sudo kill sudo lsof -t -i:8080
sudo kill sudo lsof -t -i:8005
sudo kill sudo lsof -t -i:8009
答案 3 :(得分:0)
我非常怀疑端口8080被用作MySQL的端口;它应该是端口3306.所以必须正确检查,因为8080通常是Application Server实例的端口号(例如GlassFish或Tomcat)。