[英]Error in connect to SQL Server 2008 R2 using JDBC
I am using Java to connect Microsoft SQL Server 2008 R2 and sqljdbc4.jar
, but there is a problem with the initial connection. 我正在使用Java连接Microsoft SQL Server 2008 R2和
sqljdbc4.jar
,但是初始连接存在问题。 After running, 3 is displayed in the browser only. 运行后,仅在浏览器中显示3。
This is my code: 这是我的代码:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class Database {
static Connection con = null;
public static int dbConnect() {
int x = 3;
try {
// Establish the connection.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setIntegratedSecurity(true);
ds.setServerName("172.18.16.10");
ds.setPortNumber(1433);
ds.setDatabaseName("my-database");
ds.setUser("my-user");
ds.setPassword("my-pass");
con = ds.getConnection();
x = 5;
if (con != null) {
x = 1;
}else{
x = 0;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return x;
}}
and this is the error I receive: 这是我收到的错误:
Login failed.
登录失败。 The login is from an untrusted domain and cannot be used with Windows authentication.
该登录名来自不受信任的域,不能与Windows身份验证一起使用。 ClientConnectionId:af711e9c-941f-4535-9ccb-b6ef31a42fdf
ClientConnectionId:af711e9c-941f-4535-9ccb-b6ef31a42fdf
The older questions about my problem in SO were not helpful for me. 关于我在SO中的问题的旧问题对我没有帮助。 I added
sqljdbc-auth.jar
into /java/bin
and path
also in /java/lib
我将
sqljdbc-auth.jar
添加到/java/bin
,并且在/java/lib
也添加了path
I add sqljdbc.jar and import that : 我添加sqljdbc.jar并将其导入:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.*;
I define all need variable in Global ans Static : 我在Global ans Static中定义了所有需要的变量:
private static SQLServerDataSource ds = null;
private static Connection con = null;
and define a method for connect: 并定义连接方法:
public static boolean getConnection() {
try {
System.out.println(ServerName);
ds = new SQLServerDataSource();
ds.setServerName(ServerName);
ds.setPortNumber(1433);
ds.setDatabaseName(DatabaseName);
ds.setUser(UserName);
ds.setPassword(Password);
con = ds.getConnection();
if (con != null) {
System.out.println("Success");
}
stmt = con.createStatement();
return true;
} // Handle any errors that may have occurred.
catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
Try doing something like this. 尝试做这样的事情。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
static Connection con = null
public static int dbConnect() {
int x = 3;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://172.18.16.10:1433;databaseName=my-database;user=my-user;password=my-pass;");
x = 5;
if (con != null) {
x = 1;
}else{
x = 0;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return x;
}
}
You will get a proper connection using the above code. 使用上面的代码,您将获得正确的连接。
Morever, the driver Name ("com.microsoft.sqlserver.jdbc.SQLServerDriver") and the connection url should always be read from a config file. 此外,应始终从配置文件中读取驱动程序名称(“ com.microsoft.sqlserver.jdbc.SQLServerDriver”)和连接URL。 Try taking the above two things out of your code into a config file.
尝试将以上两件事从代码中提取到配置文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.