java连数据库的时候显示链接成功了 但是显示executeQuery:对象名 'Course' 无效。

pp1098 2016-10-16 02:55:51
初学者求大神指教,数据库用的是sql server 2005
封装数据库操作的类代码是

import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
public class SqlManager {
private static SqlManager p=null;
private PropertyResourceBundle bundle;
private static String jdbcDriver=null;
private static String split=null;
private String DBType=null;
private String DBhost="localhost";
private String DBname="StudentManager";
private String DBport="1433";
private String DBuser="sa";
private String DBpassword="pp1098";
private Connection Sqlconn=null;
private Statement Sqlstmt=null;
private String strCon=null;
private SqlManager(){
try{
bundle=new PropertyResourceBundle(SqlManager.class.
getResourceAsStream("/sysConfig.properties"));
this.DBhost=getString("DBhost");
this.DBname=getString("DBname");
this.DBport=getString("DBport");
this.DBuser=getString("DBuser");
this.DBpassword=getString("DBpassword");
String system_type=getString("system-type");
if(system_type!=null){
if(system_type!=null){
if(system_type.toLowerCase().equals("widows"))
split=";";
else if(system_type.toLowerCase().equals("unix"))
split=":";
}
String database_type=getString("database-type");
this.DBType=database_type;
if(database_type!=null){
if(database_type.toLowerCase().equals("mysql")){
jdbcDriver="com.mysql.jdbc.Driver";
strCon="jdbc:mysql://"+DBhost+":"+DBport+"/"+DBname;
}
else if(database_type.toLowerCase().equals("oracle")){
jdbcDriver="oracle.jdbc.driver.OracleDriver";
strCon="jdbc:oracle:thin:@"+DBhost+":"+DBport+":"+DBname;
}
else if(database_type.toLowerCase().equals("sqlserver")){
jdbcDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
strCon="jdbc:sqlserver://"+DBhost+":"+DBport+";DatabaseName="+DBname+";DatabaseUser="+DBuser+";Databasepassword="+DBpassword;
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static SqlManager createInstance(){
if(p==null)
{
p=new SqlManager();
p.initDB();
}
return p;
}
private String getString(String s)
{
return this.bundle.getString(s);
}

public void initDB(){
System.out.println(strCon);
System.out.println(jdbcDriver);
try{
Class.forName(jdbcDriver);
}catch(Exception ex){
System.err.println("Can't Find Database Driver.");
}
}
public void connectDB(){
try{
System.out.println("SqlManager:Connecting to database...");
Sqlconn=DriverManager.getConnection(strCon,DBuser,DBpassword);
Sqlstmt=Sqlconn.createStatement();
}catch(SQLException ex){
System.err.println("connectDB"+ex.getMessage());
}
System.out.println("SqlManager:Connect to database successful.");
}
public void closeDB(){
try{
System.out.println("SqlManager:Close connection to database...");
Sqlstmt.close();
Sqlconn.close();
}catch(SQLException ex){
System.err.println("closeDB:"+ex.getMessage());
}
System.out.println("Sqlmanager:Close connection successful.");
}
public int executeUpdate(String sql){
int ret=0;
try{
ret=Sqlstmt.executeUpdate(sql);
}catch(SQLException ex)
{
System.out.println("executeUpdate:"+ex.getMessage());
}
return ret;
}
public ResultSet executeQuery(String sql){
ResultSet rs=null;
try{
rs=Sqlstmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("executeQuery:"+ex.getMessage());
}
return rs;
}

报错是
SqlManager:Connecting to database...
SqlManager:Connect to database successful.
executeQuery:对象名 'Course' 无效。
Exception in thread "main" java.lang.NullPointerException
at CourseList.initTable(CourseList.java:51)
at CourseList.<init>(CourseList.java:38)
at StuPanel.<init>(StuPanel.java:46)
at MyFrame.<init>(MyFrame.java:22)
at StudentMain.main(StudentMain.java:5)
...全文
467 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2016-10-16
  • 打赏
  • 举报
回复
Course 这个应该是一张表,检查一下,这个名称对吗?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧