我正在使用Windows身份验证(请记住)。
由于没有用户名和密码。
然后
为什么以下代码会给我错误?
public class Conection
{
public static void main(String a[]) throws ClassNotFoundException, SQLException
{
try
{
String url = "jdbc:sqlserver://localhost\\MALIKUSMANNAWAZ:1433;databaseName=ali";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
System.out.println("connection created");
Statement st=conn.createStatement();
String sql="select * from Login_System";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
System.out.println("Name: "+rs.getString(1));
//System.out.println("Address : "+rs.getString(2));
}
if(st!=null)
st.close();
if(conn!=null)
conn.close();
}
catch(SQLException sqle)
{
System.out.println("Sql exception "+sqle);
}
}
}
请注意我的电脑名称是:
MALIKUSMANNAWAZ
使用:
Windows Authentication
数据库名称:
ali
IDE:
SQL Server 2012
答案 0 :(得分:2)
将;integratedSecurity=true
添加到您的连接字符串中。如果您使用Windows身份验证,则不需要提交凭据,但仍需要告知您的连接使用Windows登录。
这是微软关于jdbc连接字符串的文章。 https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx
答案 1 :(得分:0)
按照给定完成:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\MALIKUSMANNAWAZ:1433;databaseName=ali","sa","dbase");
System.out.println("connection created");
Statement st=conn.createStatement();
String sql="select * from Login_System";
ResultSet rs=st.executeQuery(sql);