use master
go
if exists (select * from sysdatabases where name='epet')
drop database epet
create database epet
on 
(
  name ='epet_data',
  filename='d:\java\epet_data.mdf',
  size =5mb,
  filegrowth=15%
)
go if exists (select * from sysobjects where name='pet')
drop table pet
------------------------宠物表pet-------------------------
create table pet
(
  id int primary key,
  name varchar(20) not null,
  typename varchar(20) not null,
  price int not null,
  health int not null,
  love int ,
  birthday datetime not null,
  owner_id int ,
  store_id int 
)
-------------------------宠物主人表 PETOWNER-----------------
if exists (select * from sysobjects where name='petowner')
drop table petowner
create table petowner
(
  id int primary key,
  name varchar(20) not null,
  password varchar(20) not null,
  money int default 100  
)
------------------------宠物商店表PETSTORE----------------
if exists (select * from sysobjects where name='petstore')
drop table petstore
create table petstore
(
  id int not null,
  name varchar(20) primary key,
  password varchar(20)not null,
  balance int default 0
)
-------------------------账目表ACCOUNT-----------------------------
if exists (select * from sysobjects where name='acount')
drop table acount
create table acount 
(
  id int primary key,
  deal_type int not null,   -------交易类型 2-->用户卖给商店,1-->用户从商店购买
  pet_id int not null,
  owner_id int not null,
  store_id int not null,
  price int not null,
  deal_date datetime not null
)INSERT INTO PET VALUES(1,'楠楠','雪纳瑞',9,0,99,SYSDATETIME(),NULL,NULL);
INSERT INTO PET VALUES(2,'西西','雪纳瑞',7,0,76,SYSDATETIME(),4,NULL);
INSERT INTO PET VALUES(3,'迪迪','八哥犬',8,0,99,'2011-11-1',NULL,6);
INSERT INTO PET VALUES(4,'雪弗莱','藏獒',14,0,23,'2011-11-5',NULL,33);
go
select * from pet建了一个这样的数据库,在数据库中可以运行查询pet表,但是用jdbc连接到数据库查询就报错:对象名 'pet' 无效。我是不是还丢了什么东西没写?总感觉数据似乎没提交。因为在oracle中commit后就可以很好的解决类似问题。

解决方案 »

  1.   

    在数据库中可以对pet表进行查询,说明数据库中表是存在的。
    这个就看你数据库写对了没?是不是master数据库呢?
      

  2.   

    我建表的时候是,首先在master库中查有没有想要建的库名,有就删除没有就建库。还指定了存放空间。建表也是如此。贴出来的就是整个建库建表过程。你可以直接复制黏贴运行,不想要了直接删除就可以了。
      

  3.   

    用的三层开发本来写的很复杂不想贴的,但是问题解决不了。就在测试类里写了下,也是说对象名无效。。 public static void main(String[] args) {     final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=epet";
        final String USER="sa";
        final String PWD="123";
        Connection conn=null;
    Statement st=null;
    ResultSet rs=null;
    try {
    Class.forName(DRIVER);
    conn=DriverManager.getConnection(URL,USER,PWD);
    st=conn.createStatement();
    String sql="select * from pet";
    System.out.println(sql);
    rs=st.executeQuery(sql);//因为语句到此就报错说表名无效后面输出的代码就不贴了。
                            } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
            } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  4.   

    select * from master.pet试试
      

  5.   

    首先你这代码有问题,你当前的用户是
    final String USER="sa";
    final String PWD="123";
    而你要查的表在 master里面。不在当前用户中,所以查不到。
      

  6.   


    经你这一说直接把databasename换成master问题终于解决了。但是我不明白表为什么在master库里面呢?虽然是在master里创建的库,但不是自己创建了一个库吗?
      

  7.   

    系统用户是可以查的,但是要加路径,不然怎么知道,你建的表在那里,如果把全用户的表拿出来匹配不太现实,而且不同用户的表名可以相同。权限够高的话 要查是可以,不知道sqlserver怎么表示路径。这种资料应该会介绍啊,楼主自己去翻吧。