当前位置: 技术问答>java相关
一定得帮我看看sql server2000通过sql server2000jdbc driver的代码
来源: 互联网 发布时间:2015-10-31
本文导语: 谁能帮我看看下面的代码有什么错误,我是初学者。用的是本地机子上的数据库,是sql自带的。用户名和密码是系统的,我的用户名是qzss,密码是33221。 import java.sql.*; public class trySQLServer{ public static void mai...
谁能帮我看看下面的代码有什么错误,我是初学者。用的是本地机子上的数据库,是sql自带的。用户名和密码是系统的,我的用户名是qzss,密码是33221。
import java.sql.*;
public class trySQLServer{
public static void main(String[] arg){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sourceURL=new String("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=e:/Program Files/Microsoft SQL Server/MSSQL/Data/pubs");
Connection databaseConnection=DriverManager.getConnection(sourceURL,"sa","");
System.out.println("Successfully connected!!");
Statement statement=databaseConnection.createStatement();
ResultSet artistNames=statement.executeQuery("SELECT au_id,au_name "+"FROM authors");
while (artistNames.next())
{
System.out.println(artistNames.getString("au_id")+" "+artistNames.getString("au_name"));
}
databaseConnection.close();
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch (SQLException sqle)
{
System.err.println(sqle);
}
}
}
调试的结果显示:
H:begjavadb>javac trySQLServer.java
H:begjavadb>java trySQLServer
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Log
in failed for user 'sa'. Reason: Not associated with a trusted SQL Server connec
tion.
问题在哪里呢?是不是username和password错了?可是数据库的这个user里是sa没密码,我不懂到底用哪个?另外我疑惑的地方是databasename的制定方式,高手一定要帮我啊,关系到我的一门设计过不过的问题啊,
import java.sql.*;
public class trySQLServer{
public static void main(String[] arg){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sourceURL=new String("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=e:/Program Files/Microsoft SQL Server/MSSQL/Data/pubs");
Connection databaseConnection=DriverManager.getConnection(sourceURL,"sa","");
System.out.println("Successfully connected!!");
Statement statement=databaseConnection.createStatement();
ResultSet artistNames=statement.executeQuery("SELECT au_id,au_name "+"FROM authors");
while (artistNames.next())
{
System.out.println(artistNames.getString("au_id")+" "+artistNames.getString("au_name"));
}
databaseConnection.close();
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch (SQLException sqle)
{
System.err.println(sqle);
}
}
}
调试的结果显示:
H:begjavadb>javac trySQLServer.java
H:begjavadb>java trySQLServer
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Log
in failed for user 'sa'. Reason: Not associated with a trusted SQL Server connec
tion.
问题在哪里呢?是不是username和password错了?可是数据库的这个user里是sa没密码,我不懂到底用哪个?另外我疑惑的地方是databasename的制定方式,高手一定要帮我啊,关系到我的一门设计过不过的问题啊,
|
参考:
http://www.csdn.net/develop/read_article.asp?id=15283
http://www.csdn.net/develop/read_article.asp?id=15283