关于PraparedSatement的问题
2016-05-25 09:24
387 查看
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"如下:
经过测试:
出现错误的可能原因:只能使用默认数据库master进行查询
需要解决的问题是:
如何才能访问自己指定的数据库??
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"如下:
经过测试:
出现错误的可能原因:只能使用默认数据库master进行查询
需要解决的问题是:
如何才能访问自己指定的数据库??
相关文章推荐
- MySQL数据库连接基本流程
- 一台电脑同时安装不同的python、django版本
- 赋值号和printf
- Android中处理崩溃异常和记录日志
- vue.js+boostrap
- initWithCoder: 与initWithFrame:的区别
- 非常无力
- inux下rz、sz的简单安装
- iOS UIScrollView 你可能不知道的奇技淫巧
- git fetch与git pull的区别--多用git fetch
- Preferences.sublime-settings
- 【网络】HTTP2.0新特性
- Python学习笔记-day0
- 使用jdbc操作mysql数据库
- React-TodoList
- 常见的数字证书格式与格式转换
- 源码解析之–网络层YTKNetwork
- Android手机开发 使用线性布局和相对布局实现Button垂直水平居中
- POJ1050 To the Max (最大子段和,最大子矩阵)
- 关于文件上传功能的思考