I'm having issues connecting to a Database server with the JDBC driver in Netbeans. I've tried everything, enabling TCP/IP, opening the ports, I've followed tutorials online. It just won't work.
This is the error message I get in the console:
Information: Error: The TCP / IP connection could not be made to the MANUEL-PC host, port 1433. Error: "Connection refused: connect Verify the connection properties, check that there is an instance of SQL Server running on the host and accepting TCP / IP connections on the port and verify that there is no firewall blocking TCP connections on the port. "
Start of the class we're using...
public class DBPosteo
{
private final String URL ="jdbc:sqlserver://MANUEL-PC\\SQLEXPRESS:1433;databaseName=DLC_MotorDeBusqueda;integratedSecurity=true";
private Connection con;
String query = "";
PreparedStatement pstmt;
ResultSet rs;
public void init()
throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL);
}
DB Server name in SQL Management Studio
Thanks in advance for the help... I've never struggled so much with Databases in my life :)
Remove the port number.
Only specify either instance name (SQLEXPRESS) or port number (1433), never both.
Since port 1433 is reserved for the unnamed instance, the SQLEXPRESS named instance would be on a different port, and unless you specifically configured it (unlikely), that port is dynamic and can change on reboots, so you need the named lookup.
Related
I have a question about MySQL/JDBC connections in Java.
I wrote an application that successfully communicates with a database, but the issue that I recently found out was that my DB connection was dropping, and I need the application to have a connection to the DB at all times.
This is a small snipplet of the error I was getting:
com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 89,225,584 milliseconds ago. The last packet sent successfully to the server was 89,225,584 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
This is also a snipplet of the constructor for my DBConnections class:
private final String url = "jdbc:mysql://localhost:3306/", database.....;
private Connection connection;
public DBConnector(){
try {
// Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url+database, username, password);
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
In the errors section, I noticed it's telling me to add autoReconnect=true, I wondered; will my connection still stay up for longer if I structured the connection class like this:
connection = DriverManager.getConnection(url+database+"?autoReconnect=true", username, password);
If not, what else could I do to make sure my connection doesn't drop?
What I would suggest is to use a connection pool (Apache DBCP or HikariCP - the last one is currently having the best performance out of all solutions on the market) with configuration of testing connection before borrowing it from the pool. Depending on the library there should be an option like setTestOnBorrow(true).
In real applications you should always use connection pool instead of manually handling connections.
I have this error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 54,607,614 milliseconds ago.
The last packet sent successfully to the server was 54,607,614 milliseconds ago. is longer than the server configured value of 'wait_timeout'.
You should consider either expiring and/or testing connection validity before use in your application,
increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
But I don't know what's the error in my Programm, I have autoReconnect=true... :
connection = DriverManager.getConnection(
"jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password
);
This is warning message, that no sql commands have been send over specified period to the Mysql server. JDBC can do auto reconnect on such event.
Add also parameter
cmaxReconnets=5
to try to reconnect 5 times before give up.
Also there is parameter for initial timeout before retry
initialTimeout=1
You can add those to properties when obtaining connection.
DriverManager.getConnection(connectionString, properties)
I have a Java program that connects to SQL Server with mssql-jdbc driver. But some users use this program over VPN and the last days we are having issues with that VPN that is causing slowness. When I ping the machine that have the SQL instance returns a 1800ms ping. Last week this ping was 400~600ms.
When the ping is above 800ms Java returns a timeout to me, so the users using VPN can not connect.
I have tried to set up a higher login timeout in Java connection string to 10 minutes for testing, but even with that the connection always return a timeout. This only happens when the ping is above 800ms so I do not think the problem can be ports, firewall or something like that.
//my connection code
DriverManager.setLoginTimeout(60); //setting up my timeout to 60 seconds
private static final String CONNSTRING = "jdbc:sqlserver://server-name\\SQLEXPRESS;databaseName=db_name;"; //my connection string
conn = DriverManager.getConnection(CONNSTRING, USER, PASS);
Maybe there is another property that I can add to my connection string to solve that problem? Or something that I can set up in SQL Server to allow this connection when the VPN is slow?
I appreciate the help.
I've had more problems with my database in the past: It could not always get connection. The database runs on a website (webhosting), and I try to access it from my own PC. Here things go wrong, if I access it from localhost to localhost then it works okay.
Error that I get in Java: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
I got no clue why, I'm using similar structure as ever, namely the following:
public class SQL {
private final static String USERNAME = "";
private final static String PASSWORD = "";
private final static String URL = "jdbc:mysql://www.fvheeswijk.nl:3306/p28004_bf4";
private static Connection cachedConnection;
private static void createConnection() {
cachedConnection = null;
Properties connectionProperties = new Properties();
connectionProperties.put("user", USERNAME);
connectionProperties.put("password", PASSWORD);
try {
cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);
} catch (SQLException ex) {
Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static Connection getConnection() {
if (cachedConnection == null) {
createConnection();
}
return cachedConnection;
}
}
The data is blanked out of course.
I then tried to ping my website, all fine.
Later I tried to ping www.fvheeswijk.nl:3306, the database, but Windows cmd cannot find it. Then I tried visiting it via the browser (does this even make sense?), but I got some message along the lines of "packets received out of order". And I have already (way before) added my PC's (Router/Network's) host name to the allowed host list of the database.
Any clue or suggestions what is going wrong?
EDIT: Now I am getting this, might explain something... java.sql.SQLException: null, message from server: "Host '541DB0AA.cm-5-6c.dynamic.ziggo.nl' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"
The main problem is that you are opening too much connections and probably never closing them or they are being closed by the application server (or wherever you run this application). This can be known from two facts in your post:
private static Connection cachedConnection. The database connection must not be cached manually, instead it should be retrieved only when needed, and closed after being used.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. This error is very explicit, you're trying to use a connection that is closed.
Also, you're naively opening connections manually, this is noted here:
cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);
To solve all these problems, you should move to a database connection pool. In short, the connection pool will open a bunch of physical database connections and keep them alive in sleeping status, and will wake up a connection on demand, then when closing the connection, instead of closing the physical connection, it will be back to the sleeping status, thus saving time for opening a new connection.
More info:
Is it a good idea to put jdbc connection code in servlet class?
How to properly keep a DB connection from a Connection Pool opened in JBoss
Java Mysql query database with connection
Should a database connection stay open all the time or only be opened when needed?
About your last edit, seems that you need to close some connections to your database. You should kill some of them and try to connect again using the database pool instead.
I am facing this problem: I have a program that loops the following:
connect to a postgresql database
print a count number
close the connection
code is as follow:
int i = 0;
while(true){
IConnection conn = ((ConnectionHelper)HelperFactory.getInstance().getHelper("ConnectionHelper")).getConnection("psql");
if(conn != null && conn.connect()){
conn.close();
System.out.println(i++);
}
}
I connect to psql db by jdbc, something like this:
DriverManager.getConnection("jdbc:postgresql://" + host + ":5432/" + database,
user, password);
the conn.connect() returns true if it can successfully connect to the database
(tested, I can retrieve data from it)
But i get the following result:
0
1
2
.
.
.
3919
3920
3921
3922
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at the last loop the program stuck at trying to connect to psql til timeout. Any help?
Related to the exception message host or/and port where postgres is running not equals to the host or/and port what you put to the connect address, check it and you will find problem.
And yes, your server cannot be reachable, some firewals and so on, check it too.