Reputation: 41
Why I can't will be connected to the MS SQL server?
Dependency:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.5.jre8-preview</version>
</dependency>
Java Code:
import java.sql.*;
try {
// Load the SQLServerDriver class, build the
// connection string, and get a connection
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.1.35\\SERVER-SQL;" +
"database=MainDataBase;" +
"user=user;" +
"password=rdthnb137";
Connection con = DriverManager.getConnection(connectionUrl);
System.out.println("Connected.");
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * from history";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
}
catch(Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
The connection to the host 192.168.1.35, named instance server-sql failed. Error : "java.net.SocketTimeoutException: Receive timed out". Verify the server and in stance names and check that no firewall is blocking UDP traffic to port 1434. F or SQL Server 2005 or later, verify that the SQL Server Browser Service is runni ng on the host.
I am connected from the local machine. The firewall is switched off.
The MS SQL server 2000 with the Service Pack 4 updating is installed.
When using Connection con = DriverManager.getConnection( "jdbc:sqlserver://192.168.1.35;database=MainDataBase;", "user", "rdthnb137");
error:
22, 2017 1:49:58 PM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogi n WARNING: ConnectionID:1 ClientConnectionId: 6684e1dc-9950-4082-8fa9-73d5bf4b38b6 Server major version:8 is not supported by this driver. SQL Server version 8 is not supported by this driver. ClientConnectionId:6684e1d c-9950-4082-8fa9-73d5bf4b38b6
Upvotes: 2
Views: 2371
Reputation: 41
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
Connection conn = null;
ResultSet rs = null;
String url = "jdbc:jtds:sqlserver://192.168.1.20;instance=SERVER-SQL;DatabaseName=MainDataBase";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "sa";
String password = "sa";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected to the database!!! Getting table list...");
DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, "%", new String[] { "TABLE" });
while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); }
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
rs.close();
}
Upvotes: 0
Reputation: 330
Instead of Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Try :
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
And Instead of
"jdbc:sqlserver://192.168.1.35\\SERVER-SQL;" +"database=MainDataBase;"
try :
jdbc:microsoft:sqlserver://192.168.1.35\\SERVER-SQL:1433;DatabaseName=MainDataBase
Upvotes: 1
Reputation: 891
I know 1433
is default port but can you specify this port number in your connection url after ip addr 192.168.1.35:1433
and give it a try?
Upvotes: 0
Reputation: 107
This works for me:
try {
Class.forName("com.mysql.jdbc.Driver");
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Raumfahrt", "root", "");
String insertStrM ="INSERT INTO Raumfahrt.Mitarbeiter VALUES(?,?,?,?)";
PreparedStatement prestmt = con.prepareStatement(insertStrM);
prestmt.setInt(1, m.getmID());
prestmt.setString(2, m.getMname());
prestmt.setString(3, m.getMname());
prestmt.setInt(4, m.getMabt());
prestmt.executeUpdate();
}catch (SQLException e) {
System.out.print("error");
e.printStackTrace();
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
Upvotes: 0