JDBC example for java

I downloaded JDK 6 and also I have sqljdb4.jar and I have a database.properties file that contains the following data

  database.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
 database.url = jdbc: sqlserver: //.; databaseName = UserInfo; integratedSecurity = true; 
 database.username = sa
 database.password = admin

BN: I install the server on my computer and the server name =., I also use Windows Authontication

My problem now, when I try to create a connection, I have the following error

com.microsoft.sqlserver.jdbc.SQLServerException: TCP / IP connection to localhost, port 1433 failed. Error: connection rejected: connect. Check the properties connection and verify that the SQL Server instance is running on the host and accepting TCP / IP connections on the port, and that the firewall is not blocking TCP connections to the port. in com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError (SQLServerException.java:130)

I don’t know what the problem is.

If anyone can help, I will appreciate

Thanks at Advance

+6
source share
3 answers

This is caused by many probabilities, such as 1- IP - this is 2- The port is incorrect 3. The firewall prevents the computer from exiting and connects to a different IP address. 4- SQL server.

try using

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 a very good technology called "Persistence", better than JDBC, and more than brilliant and easy to use.

+5
source

The problem is that your SQL server is

  • not installed,
  • not working or
  • Do not accept TCP / IP connections.

In particular, the latter is unpleasant, since I remember that some versions of SQL Server did not configure the TCP / IP connector to start by default.

+5
source

Well, first of all, we need to see your code. Secondly, looking at the error message, database A) does not work
B) on another port
or C) wrong code.

+1
source

Source: https://habr.com/ru/post/887287/


All Articles