数据库中出现的问题,小白求教

EnochRebakah 2017-03-25 03:55:41
首先出现的错误是:


代码如下:
package cn.edu.zucc.booklib.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.booklib.model.BeanPublisher;
import cn.edu.zucc.booklib.model.BeanReaderType;
import cn.edu.zucc.booklib.util.BaseException;
import cn.edu.zucc.booklib.util.BusinessException;
import cn.edu.zucc.booklib.util.DBUtil;
import cn.edu.zucc.booklib.util.DbException;

public class PublisherManager {
public List<BeanPublisher> loadAllPublisher()throws BaseException{
List<BeanPublisher> result=new ArrayList<BeanPublisher>();
Connection conn=null;
try {
conn=DBUtil.getConnection();
String sql="select pubid,publisherName,address from BeanPublisher order by pubid";
java.sql.Statement st=conn.createStatement();
java.sql.ResultSet rs=st.executeQuery(sql);
while(rs.next()){
BeanPublisher p=new BeanPublisher();
p.setPubid(rs.getString(1));
p.setPublisherName(rs.getString(2));
p.setAddress(rs.getString(3));
result.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
throw new DbException(e);
}
finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public void createPublisher(BeanPublisher p) throws BaseException{
if(p.getPubid()==null || "".equals(p.getPubid()) || p.getPubid().length()>20){
throw new BusinessException("出版社编号必须是1-20个字");
}
if(p.getPublisherName()==null || "".equals(p.getPublisherName()) || p.getPublisherName().length()>50){
throw new BusinessException("出版社名称必须是1-50个字");
}
if(p.getAddress()==null || "".equals(p.getAddress()) || p.getAddress().length()>100){
throw new BusinessException("出版地址必须是1-100个字");
}


Connection conn=null;
try {
conn=DBUtil.getConnection();
String sql="select * from BeanPublisher where pubid=?";
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1,p.getPubid());
java.sql.ResultSet rs=pst.executeQuery();
if(rs.next()) throw new BusinessException("出版社编号已经被占用");
rs.close();
pst.close();
sql="select * from BeanPublisher where publisherName=?";
pst=conn.prepareStatement(sql);
pst.setString(1, p.getPublisherName());
rs=pst.executeQuery();
if(rs.next()) throw new BusinessException("出版社名称已经存在");
rs.close();
pst.close();
sql="insert into BeanPublisher(pubid,publisherName,address) values(?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, p.getPubid());
pst.setString(2, p.getPublisherName());
pst.setString(3,p.getAddress());
pst.execute();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
throw new DbException(e);
}
finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void modifyPublisher(BeanPublisher p)throws BaseException{
if(p.getPubid()==null || "".equals(p.getPubid()) || p.getPubid().length()>20){
throw new BusinessException("出版社编号必须是1-20个字");
}
if(p.getPublisherName()==null || "".equals(p.getPublisherName()) || p.getPublisherName().length()>50){
throw new BusinessException("出版社名称必须是1-50个字");
}
if(p.getAddress()==null || "".equals(p.getAddress()) || p.getAddress().length()>100){
throw new BusinessException("出版地址必须是1-100个字");
}

Connection conn=null;
try {
conn=DBUtil.getConnection();
String sql="select * from BeanPublisher where pubid=?";
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, p.getPubid());
java.sql.ResultSet rs=pst.executeQuery();
if(!rs.next()) throw new BusinessException("出版社不存在");
rs.close();
pst.close();
sql="select * from BeanPublisher where publisherName=? and pubid<>?";
pst=conn.prepareStatement(sql);
pst.setString(1, p.getPublisherName());
pst.setString(2, p.getPubid());
rs=pst.executeQuery();
if(rs.next()) throw new BusinessException("同名出版社已经存在");
rs.close();
pst.close();
sql="update BeanPublisher set publisherName=?,address=? where pubid like =?";
pst=conn.prepareStatement(sql);
pst.setString(1, p.getPublisherName());
pst.setString(2,p.getAddress());
pst.setString(3, p.getPubid());
pst.execute();
} catch (SQLException e) {
e.printStackTrace();
throw new DbException(e);
}
finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void deletePublisher(String id)throws BaseException{
if(id==null || "".equals(id) ||id.length()>20){
throw new BusinessException("出版社编号必须是1-20个字");
}
Connection conn=null;
try {
conn=DBUtil.getConnection();
String sql="select publisherName from BeanPublisher where pubid=?";
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, id);
java.sql.ResultSet rs=pst.executeQuery();
if(!rs.next()) throw new BusinessException("出版社不存在");
String publisherName=rs.getString(1);
rs.close();
pst.close();
sql="select count(*) from BeanBook where pubid=?";
pst=conn.prepareStatement(sql);
pst.setString(1, id);
rs=pst.executeQuery();
rs.next();
int n=rs.getInt(1);
pst.close();
if(n>0) throw new BusinessException("已经有"+n+"本图书的出版社是"+publisherName+"了,不能删除");
pst=conn.prepareStatement("delete from BeanPublisher where pubid=?");
pst.setString(1, id);
pst.execute();

} catch (SQLException e) {
e.printStackTrace();
throw new DbException(e);
}
finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public List<BeanPublisher> searchPubsByName(String name)throws BaseException
{
//创建一个数据库连接
String temp1;
temp1="%"+"name"+"%";//
if(name==null || "".equals(name) ||name.length()>20){
throw new BusinessException("出版社编号必须是1-20个字");
}
Connection conn=null;
try {
//调用DBUtil中的创建数据库连接的函数得到具体的conn对象
conn=DBUtil.getConnection();
//创建连接sql语句,用string对象保存
String sql="select publisherName from BeanPublisher where publisherName like ? ";//不能写成‘%?%’
//将sql语句执行成为数据库执行命令
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, temp1);//单引号会自动添加的
java.sql.ResultSet rs=pst.executeQuery();
//if(!rs.next()) return null;
List<BeanPublisher> PL=new ArrayList();
while (rs.next())
{
BeanPublisher temp=new BeanPublisher();
//temp.setPubid(rs.getString(0));
temp.setPublisherName(rs.getString(0));
//temp.setAddress(rs.getString(3));
PL.add(temp);
}
return PL;

} catch (SQLException e) {
e.printStackTrace();
throw new DbException(e);
}
finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public static void main(String[] args){
BeanPublisher P0=new BeanPublisher();
BeanPublisher P1=new BeanPublisher();
BeanPublisher P2=new BeanPublisher();
PublisherManager pm=new PublisherManager();
P1.setAddress("1");
P1.setPubid("1");
P1.setPublisherName("1");
try {
List<BeanPublisher> list=pm.searchPubsByName(P1.getPublisherName());//模糊搜索得到相应的出版社
if (list==null)
System.out.println("出版社不存在");
else
{
for (int i=0;i<=list.size();i++)
{
P0=list.get(i);
System.out.println(P0.getPubid()+","+P0.getPublisherName()+","+P0.getAddress());
}

}
} catch (BaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

P2.setAddress("123");
P2.setPubid("123");
P2.setPublisherName("123");
try {
List<BeanPublisher> list=pm.searchPubsByName(/*P2.getPublisherName()*/"123");
if (list==null)
System.out.println("出版社不存在");
else
{
for (int i=0;i<list.size();i++)//《=还是《
{
P0=list.get(i);
System.out.println(P0.getPubid()+","+P0.getPublisherName()+","+P0.getAddress());
}

}
} catch (BaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...全文
317 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
walkuere 2017-03-31
  • 打赏
  • 举报
回复
引用 8 楼 qq_34309305 的回复:
[quote=引用 7 楼 walkuere 的回复:] [quote=引用 5 楼 qq_34309305 的回复:] 使用list时加个判断: if(list.size()>0){ list.get(0); ......... }
我觉得你这样治标不治本,它取出来null肯定后面的事情做不下去一路报错,还是要确保它得到的不会是null[/quote] list是否为null的判断博主已经加进去了啊!你仔细看看他代码。[/quote] 我的意思是让你get总能得到不是null的结果,而不是一路写if obj!=null来一路规避如果get到null
Fellon9 2017-03-31
  • 打赏
  • 举报
回复
在下看来,貌似是List的值与下面的不对应。 你可以看一下你调用的《 searchPubsByName 》方法 的PL值。 博主可以调试一下,在List下面 输出一下list的值,然后慢慢解。
75闪光雷 2017-03-31
  • 打赏
  • 举报
回复
引用 7 楼 walkuere 的回复:
[quote=引用 5 楼 qq_34309305 的回复:] 使用list时加个判断: if(list.size()>0){ list.get(0); ......... }
我觉得你这样治标不治本,它取出来null肯定后面的事情做不下去一路报错,还是要确保它得到的不会是null[/quote] list是否为null的判断博主已经加进去了啊!你仔细看看他代码。
walkuere 2017-03-31
  • 打赏
  • 举报
回复
引用 5 楼 qq_34309305 的回复:
使用list时加个判断: if(list.size()>0){ list.get(0); ......... }
我觉得你这样治标不治本,它取出来null肯定后面的事情做不下去一路报错,还是要确保它得到的不会是null
110成成 2017-03-30
  • 打赏
  • 举报
回复
list大小位0,使用get方法空异常,应该先判断下。
walkuere 2017-03-30
  • 打赏
  • 举报
回复
你在某个列表get(0)的时候,列表本身长度为空
理太偏 2017-03-30
  • 打赏
  • 举报
回复
下标越界,总长度为0,取的是第1个
75闪光雷 2017-03-30
  • 打赏
  • 举报
回复
使用list时加个判断: if(list.size()>0){ list.get(0); ......... }
我爱娃哈哈 2017-03-25
  • 打赏
  • 举报
回复
251行看看
我爱娃哈哈 2017-03-25
  • 打赏
  • 举报
回复
数组下标越界了

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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