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后就可以很好的解决类似问题。
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后就可以很好的解决类似问题。
这个就看你数据库写对了没?是不是master数据库呢?
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();
}
final String USER="sa";
final String PWD="123";
而你要查的表在 master里面。不在当前用户中,所以查不到。
经你这一说直接把databasename换成master问题终于解决了。但是我不明白表为什么在master库里面呢?虽然是在master里创建的库,但不是自己创建了一个库吗?