I have downloaded JDK 6 and also I have sqljdb4.jar and I have database.properties file that content the following data
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true;
database.username=sa
database.password=admin
B.N : I'm installing the server on my machine and the server name = . , also I'm using Windows Authontication
My problem now is when I try to create connection I have the following error
com.microsoft.sqlserver.jdbc.SQLServerException:
The TCP/IP connection to the host
localhost, port 1433 has failed.
Error: Connection refused: connect.
Please verify the connection
properties and check that a SQL Server
instance is running on the host and
accepting TCP/IP connections at the
port, and that no firewall is blocking
TCP connections to the port. at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
I don't know what is the exact problem here
If any one can help I will be appreciated
Thanks in Advance
That's caused by many probabilities like
1- IP is worong
2- Port is wrong
3- There is firewall prevent machine to go out and connect to another IP
4- SQL server down .
try to use
public class JdbcSQLServerDriverUrlExample
{
public static void main(String[] args)
{
Connection connection = null;
try
{
// the sql server driver string
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// the sql server url
String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
// get the sql server database connection
connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
// now do whatever you want to do with the connection
// ...
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
What i need to explain is there is very good technology called " Persistence " is better than JDBC and is more than brilliant and easy to use .
The problem is that your SQL server is either
not installed,
not running or
not accepting TCP/IP connections.
Particularly the last one is nasty, as I remember that some versions of SQL Server have not configured the TCP/IP connector to run by default.
Well first and foremost we need to see your code. Second looking at the error message the database is A)not running
B) on a different port
or C) the code is incorrect.
Related
public class RDF2Connection {
static Connection connection=null;
final static String connectionUrl = "jdbc:sqlserver://DESKTOP-Q5K9FE6:1433;" +
"databaseName=RDFDB;";
public static Connection getRdf2Connected(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(connectionUrl, "sa", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host DESKTOP-Q5K9FE6, port 1433 has failed. Error: "Connection
refused: no further information. Verify the connection properties.
Make sure that an instance of SQL Server is running on the host and
accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:257)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2385)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:567)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1955)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1616)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1447)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:788)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1187)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.rdf2.databaseconnection.RDF2Connection.getRdf2Connected(RDF2Connection.java:22)
at MainClass.main(MainClass.java:53)
java.lang.NullPointerException
at MainClass.main(MainClass.java:54)
Process finished with exit code 0
Can you see if the SQL Server is only listening on an IPv6 port? If so you can use the following
System.setProperty("java.net.preferIPv6Addresses", "true");
That would be my only suggestion as far as code changes. Possibly try and use the IP as opposed to the name. If that doesn't work you need to go to your SQL Server and verify that it accepts TCP/IP connections, or check your local firewall settings. At that point the question would be more appropriate for server exchange.
just make sure this the the correct port
this is the true answer
Thanks.
I am trying to connect to Azure SQL Data Warehouse through JDBC. I am getting the following exception.
*
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host tcsqldatawh.database.windows.net, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2280)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:493)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1387)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at testsqldw.SQLDatabaseConnection.main(SQLDatabaseConnection.java:30)
*
I have seen similar questions asked about connecting to the SQLServer DB here.
I configured the database to allow access to my IP using the process here.
Please check the code below:
package testsqldw;
import java.sql.*;
public class SQLDatabaseConnection {
// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionString =
"jdbc:sqlserver://databaseserver.database.windows.net:1433;"
+"database=databaseserver;"
+"user=username#databaseserver;"
+ "password=password;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
System.out.println("Total connection string is---\n"+connectionString);
// Declare the JDBC objects.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(connectionString);
// Create and execute a SELECT SQL statement.
String createSql = "create table employee(employee_id varchar(20));";
statement = connection.createStatement();
boolean status=statement.execute(createSql);
System.out.println(status);
// Print results from select statement
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connections after the data has been handled.
if (statement != null) try { statement.close(); } catch(Exception e) {}
if (connection != null) try { connection.close(); } catch(Exception e) {}
}
}
}
Total connection string is
jdbc:sqlserver://databaseserver.database.windows.net:1433;database=databaseserver;user=username#databaseserver;password=password;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
Please help me in resolving the issue.
Per my experience, the issue may be caused by the following reasons:
The SQL Server which you have created may not work fine.
You may use incorrect JDBC connection string.
For the first reason, you could use some client tools to connect to the SQL server. If you could connect to the server, that indicates the SQL server is ok.
If not ,you could create a new SQL Server. After then, you could create a new SQL Data Warehouse according to this URL https://azure.microsoft.com/en-us/documentation/articles/sql-data-warehouse-get-started-provision/.
The URL also includes the firewall config method.
I use the SQL Server InTouch to connect to the SQL Server. The followings are some description images.
You could get the parameters by the Azure Portal. The port number is 1433.
The following picture indicates that your server is ok.
For the second reason, you could copy the connection string from the azure portal and modify the password only.
Hope it helps. Any concerns, please feel free to let me know.
Full error I'm getting:
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
I have already checked that TCP/IP is enabled, using port 1433, and TCP dynamic ports is empty. I have disabled windows firewall.
Here is my code:
import java.sql.*;
public class DBConnect {
public static void main(String[] args) {
// TODO Auto-generated method stub
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB1;instance=SQLSERVER;encrypt=true;TrustServerCertificate=true;";
String user = "sa";
String pass = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myConn = DriverManager.getConnection(dbURL, user, pass);
try {
Statement myStmt = myConn.createStatement();
try {
ResultSet myRs = myStmt.executeQuery("Select * from Login");
while (myRs.next())
{
System.out.println(myRs.getString("Username"));
System.out.println(myRs.getString("Password"));
}
}
catch (Exception e)
{
System.out.println("Error with query");
}
}
catch (Exception e)
{
System.out.println("Error connecting to database");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Have you enabled 'Named Pipes' and 'TCP/IP'?
Open the 'Sql Server Configuration Manager' application.
In the left pane, go to 'SQL Server Network Configuration' -> 'Protocols for [instance-name]'
Right-click on both 'Named Pipes' and 'TCP/IP' and select 'enable'.
Have you used the correct port?
Double-click on 'TCP/IP'
Select 'IP Addresses' tab
Scroll to IPAII. Your port number is here.
Restart the 'SQL Server ([instance-name])' windows service.
This error usually come when SQL server not accepting TCP/IP Connection, pls try below steps it will work for sure.
1)open run and add command SQLServerManager15.msc
2)click on network configuration then "protocols for MSSQLSERVER"
3)Select protocol name - "TCP\IP" and make sure that it is enable if not then pls make it enable.
4)Check the property and find port in IP address tab.
Restart the server, it should work
And also make sure that on the same page TCP/IP is enabled
My solution:
Client: DBeaver
Auth: Windows Authentication
After taking the steps:
Enable tcp/ip
Enabling named pipes
Connection string: localhost\SQLEXPRESS (that backslash made all the difference).
I can't connect to a mysql database on my "live-server" but it works just fine on my local computer.
In my main class I am doing this:
try {
main.connection.open();
} catch (SQLException e1) {
log.fatal(e1.getMessage());
System.exit(0);
}
And the open method looks like this
public void open() throws SQLException{
if(con != null) close();
con = DriverManager.getConnection(url, user, password);
}
After a couple of minutes running I am getting this:
14:35:47.434 [main] FATAL se.mypack.Server - Could not create connection to database server. Attempted reconnect 3 times. Giving up.
How do I debug this? The url, server and password variables is correct. What might be the problem here?
Using mysql-connector-java-5.1.29-bin.jar
First you have to make sure you have loaded your DB-driver class.
It goes as the following:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
This is for DERBY DB.
There is a different driver class for each DB, but the idea is the same.
If you already did it, check if you have a tcp connection to the DB server from the client machine
that tries to establish the connection. You can use : ping -t your.server.ip.
If you get a response, try to establish telet connection to that url + port number (the port that the DB listener listens to, for Oracle it will be 1521, for Derby it is 1527..) and see that you don't get any connection refuse messages.
If your connection attempt failed, see if the DB listener is up, or whether you have a fire wall issue.
Good Luck,
Yosi Lev
I am trying to create a JSF application using the Eclipse IDE. I am using a remote mySQL server as my database. How do I connect to this remote database for creating tables and accessing them?
Just supply the IP / hostname of the remote machine in your database connection string, instead of localhost. For example:
jdbc:mysql://192.168.15.25:3306/yourdatabase
Make sure there is no firewall blocking the access to port 3306
Also, make sure the user you are connecting with is allowed to connect from this particular hostname. For development environments it is safe to do this by 'username'#'%'. Check the user creation manual and the GRANT manual.
You need to pass IP/hostname of the rempote machine in the connection string.
import java.sql.*;
import javax.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String url = "jdbc:mysql://localhost:3306/mydb";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"root"," ");
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
to access database from remote machine , you need to give grant all privileges to you data base.
run the following script to give permissions:
GRANT ALL PRIVILEGES ON . TO user#'%' IDENTIFIED BY 'password';
Plus, you should make sure the MySQL server's config (/etc/mysql/my.cnf, /etc/default/mysql on Debian) doesn't have "skip-networking" activated and is not binded exclusively to the loopback interface (127.0.0.1) but also to the interface/IP address you want connect to.
Create a new user in the schema ‘mysql’ (mysql.user)
Run this code in your mysql work space
“GRANT ALL ON . to user#'%'IDENTIFIED BY '';
Open the ‘3306’ port at the machine which is having the Data Base.
Control Panel ->
Windows Firewall ->
Advance Settings ->
Inbound Rules ->
New Rule ->
Port ->
Next ->
TCP & set port as 3306 ->
Next ->
Next ->
Next ->
Fill Name and Description ->
Finish ->
Try to check by a telnet msg on cmd including DB server's IP
Close all the connection which is open & connected to the server listen port, whatever it is from application or client side tool (navicat) or on running server (apache or weblogic). First close all connection then restart all tools MySQL,apache etc.
in my.cnf file , please change the following
## Instead of skip-networking the default is now to listen only on
## localhost which is more compatible and is not less secure.
## bind-address = 127.0.0.1
On Ubuntu, after creating localhost and '%' versions of the user, and granting appropriate access to database.tables for both, I had to comment out the 'bind-address' in /etc/mysql/mysql.conf.d/mysql.cnf and restart mysql as sudo.
bind-address = 127.0.0.1
Change the IP / hostname of the vps in your database connection string, instead of localhost. For example if my IP is 193.23.127.130:
jdbc:mysql://193.23.127.130:3306/yourdatabase
then make sure your vps firewall allow port 3306 for MySQL.
Then go to MySQL workbench, Server–Users and Privileges, create an account (in this case account: remoteUser, password:password), make sure Limit to Hosts Matching is % (means you can access from any other IP)
Then set password and grant rights then apply
then you can add the following code in Java to access remotely
Connection connection = DriverManager.getConnection("jdbc:mysql://193.23.127.130:3306/swing_demo", "remoteUser", "password");