[置顶] 关于PraparedSatement的问题

时间:2021-03-08 15:37:26
package com.test4;


import java.sql.*;
public class TestJDBC {


public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps = null;
Connection ct = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:60956;databaseName = stucoursedase","sa","123456");
ps = ct.prepareStatement("select * from SC1");
rs = ps.executeQuery();
while(rs.next()){
System.out.println("测试");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(ct != null){
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}


源代码出现下面的异常

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'SC1' 无效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at com.test4.TestJDBC.main(TestJDBC.java:15)


其中数据库stucoursedase"如下:

[置顶]        关于PraparedSatement的问题

经过测试:

    出现错误的可能原因:只能使用默认数据库master进行查询


需要解决的问题是:

  如何才能访问自己指定的数据库??