JSP页面获取SQL数据出现异常对象名 'book' 无效。
packagefile;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputS...
package file;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class ShowImage extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=
DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseNmae=testDatabase","sa","123456");
Statement stmt = conn.createStatement();
String sql="select bookImage from book where bookName='"+codeToString(request.getParameter("bookname"))+
"' and author='"+codeToString(request.getParameter("author"))+
"'and publisher='"+
codeToString(request.getParameter("publisher"))+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int len = 10*1024*1024;
InputStream in = rs.getBinaryStream("bookImage");
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf,0,i);
}
in.close();
toClient.flush();
toClient.close();
}
conn.close();
}catch(Exception e){
System.out.println("出现异常"+e.getMessage());
}
}
//public String codeToString(String str){
//return (new String(s.getBytes("UTF-8")));
//}
private String codeToString(String parameter) {
// TODO Auto-generated method stub
return null;
}
} 展开
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class ShowImage extends HttpServlet {
public void service(ServletRequest request,ServletResponse response)
throws ServletException,IOException {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=
DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseNmae=testDatabase","sa","123456");
Statement stmt = conn.createStatement();
String sql="select bookImage from book where bookName='"+codeToString(request.getParameter("bookname"))+
"' and author='"+codeToString(request.getParameter("author"))+
"'and publisher='"+
codeToString(request.getParameter("publisher"))+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
int len = 10*1024*1024;
InputStream in = rs.getBinaryStream("bookImage");
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf,0,i);
}
in.close();
toClient.flush();
toClient.close();
}
conn.close();
}catch(Exception e){
System.out.println("出现异常"+e.getMessage());
}
}
//public String codeToString(String str){
//return (new String(s.getBytes("UTF-8")));
//}
private String codeToString(String parameter) {
// TODO Auto-generated method stub
return null;
}
} 展开
4个回答
展开全部
数据库有没有book 这张表
book在 sqlserver中是不是保留字, 如果是, book要用单引号包起来: from 'book'
提问的时候, 最好把报错信息一同粘上来
追问
页面没报错就只是控制台出现这个 对象名 'book' 无效。,还有界面上数据提取不出来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
查数据库出错,就要看看 book表 存不存在。把SQL复制到 数据库查下,没问题后 在到 java里面 测试
追问
在数据库下是可以查到的!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把错误问题贴出来,你先看看是不是数据库驱动不对。对象名不对,有可能是没有连上数据库
更多追问追答
追问
数据库是连上的错误问题就是对象名 'book' 无效。
追答
你最好是把错误贴出来,你数据库里有book这张表吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
默认数据库错误。
追问
怎么调?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询