[英]Make a connection to a MS SQL database in the local machine
I am trying to access a database which is located in my C drive in my local machine. 我正在尝试访问位于本地计算机的C驱动器中的数据库。 I can access the data base through Microsoft SQL server Management Studio.
我可以通过Microsoft SQL Server Management Studio访问数据库。 However as the data base is not in a server my first question is "if even it's possible to access it through my java code."
但是,由于数据库不在服务器中,所以我的第一个问题是“即使可以通过我的Java代码访问它”。
-I have tried my local machine address and also the name of the server however non of them seems to be working. -我尝试了我的本地计算机地址以及服务器名称,但是似乎都不起作用。
String url = "jdbc:sqlserver://DAVE-PC\\SQLEXP/";
String dbName = "STORESQL";
// String driver = "com.mysql.jdbc.Driver";
String userName = "dave-PC\\dave";
String password="";
try {
// Class.forName(driver).newInstance();
Connection con =DriverManager.getConnection(url+dbName,userName,password);
java.sql.Statement stm= con.createStatement();
*DAVE-PC\\SQLEXP/" is the name of my database. The username and password are windows authentication. * DAVE-PC \\ SQLEXP /“是我的数据库的名称。用户名和密码是Windows身份验证。
My second question is if I can't access the database like this, Is there any virtual server (compatible with microsoft SQL) that I can copy the database there and access it from the? 我的第二个问题是,如果我不能像这样访问数据库,是否有任何虚拟服务器(与Microsoft SQL兼容)可以复制数据库并从中访问数据库?
I think your mistake is in userName. 我认为您的错误是在userName中。 for your PC you can try LOCALHOST or 127.0.0.1 Perhaps you would try this method to connect:
对于您的PC,您可以尝试LOCALHOST或127.0.0.1也许您可以尝试使用以下方法进行连接:
try{ 尝试{
server = "jdbc:sqlserver://127.0.0.1:1433;databaseName=STORESQL;selectMethod=cursor"; server =“ jdbc:sqlserver://127.0.0.1:1433; databaseName = STORESQL; selectMethod = cursor”; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Class.forName(“ com.microsoft.sqlserver.jdbc.SQLServerDriver”); Connection con = DriverManager.getConnection(server,"dave",password);
连接con = DriverManager.getConnection(server,“ dave”,password);
}catch(SQLException e){
System.out.println(e);
}catch(ClassNotFoundException e){
System.out.println(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.