用下面这个程序测试JAVA和数据库的连接,出现了这个问题:对象名"chengjibiao"无效~
为什么呀?我在Test这个数据库下面已经建立了一张chengjibiao的表,所有的权限都大了勾,怎么访问不了?
我的系统是XP,用的是SQL2000个人版。
我是个菜鸟,搞了半天也弄不明白,请各位高手帮忙解答一下,不胜感激!
import java.sql.*;
class Test{//本程序在jdk1.4.2下调试通过.
public static void main(String args[])
{
//String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
// String user ="sa";//这里替换成你自已的数据库用户名
// String password = "as";//这里替换成你自已的数据库用户密码
String sqlStr = "select * from chengjibiao";
try{ //这里的异常处理语句是必需的.否则不能通过编译!
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("类实例化成功!" );
String url="jdbc:odbc:Test";
//mydb为数据库
String user="sa";
String password="sa";
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("创建连接对像成功!" );
Statement st = con.createStatement();
System.out.println( "创建Statement成功!" );
ResultSet rs = st.executeQuery( sqlStr );
System.out.println( "操作数据表成功!" );
System.out.println( "----------------!" );
/* while(rs.next())
{
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}*/
rs.close();
st.close();
con.close();
}
catch(Exception err)
{
err.printStackTrace(System.out);
}
}
}
----------------解决方案--------------------------------------------------------
你的chengjibiao 是在你的Northwind的数据库里面吗?
----------------解决方案--------------------------------------------------------
在Test中~
那个是老师给的调试程序的数据库名,我改了的
----------------解决方案--------------------------------------------------------
是不是没有添加数据源?
----------------解决方案--------------------------------------------------------
楼主你把代码都贴出来吧!如果这是你全部的代码的话是不是还少引入一个异常类的包啊??
----------------解决方案--------------------------------------------------------
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Test";
----------------解决方案--------------------------------------------------------
检查一下数据源和驱动有没有问题。
----------------解决方案--------------------------------------------------------
你Northwind这个数据库有chengjibiao这个表吗?
应该没有吧,好像这个数据库是自带的吧
自己好好检查一下吧
----------------解决方案--------------------------------------------------------
在ODBC数据源管理器添加了系统DSN 名字叫Test
这个就是我的完整程序,那个被注解下去的是我们老师给的源代码
重贴一边,原来的那个有点乱~
import java.sql.*;
class Test{
public static void main(String args[])
{
String sqlStr = "select * from chengjibiao";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("类实例化成功!" );
String url="jdbc:odbc:Test";
String user="sa";
String password="sa";
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("创建连接对像成功!" );
Statement st = con.createStatement();
System.out.println( "创建Statement成功!" );
ResultSet rs = st.executeQuery( sqlStr );
System.out.println( "操作数据表成功!" );
System.out.println( "----------------!" );
rs.close();
st.close();
con.close();
}
catch(Exception err)
{
err.printStackTrace(System.out);
}
}
}
这个是清晰版的,显示的结果是前三个都同通过了最后一个对象名的"chengjibiao"无效~
那位高手再帮忙看一看吧!
----------------解决方案--------------------------------------------------------
你的数据库里面有没有一个叫chengjibiao"的表
----------------解决方案--------------------------------------------------------