java 连接MS SQL Server 2000出问题
我编了个java程序,在java中连接MS SQL Server 2000数据库
程序如下:
import java.sql.*;
public class JDBCTest
{
public static void main(String userPwd =ing[] args)
{
String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
String dbURL = “jdbc:microsoft:sqlserver://192.168.0.97:1433; DatabaseName=wtest”;
String userName = "sa ";
String userPwd = "sa ";
try
{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println(”Connection Successful!”);
}
Catch (Exception e) {
e.printStackTrace();
}
dbConn.close();
}
}
注:192.168.0.97是我的服务器,其上有一数据库wtest,wtest中有一表book。
程序运行结果为:
Connection Successful!
说明连接数据库成功了。
然后我在程序中加了一部分内容,想把表book中各记录的bookID字段(varchar类型)输出。
程序如下:
import java.sql.*;
public class JDBCTest
{
public static void main(String[] args)
{
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String dbURL = "jdbc:microsoft:sqlserver://192.168.0.97:1433; DatabaseName=wtest ";
String userName = "sa ";
String userPwd = "sa ";
Connection dbConn;
try
{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
if(dbConn!=null)
System.out.println( "Connection Successful! ");
Statement stmt=dbConn.createStatement();
ResultSet rst=stmt.executeQuery( "select * from book; ");
while(rst.next())
{
String id=rst.getString( "bookID ");
System.out.println(id);
}
//关闭连接、释放资源
rst.close();
stmt.close();
dbConn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
此时程序运行结果为:
Connection Successful!
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'book ' 无效。