很明顯啊,你的jstl的c標(biāo)簽引用的有問題。
創(chuàng)新互聯(lián)-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、服務(wù)器機(jī)柜租賃、等保安全、私有云建設(shè)等企業(yè)級互聯(lián)網(wǎng)基礎(chǔ)服務(wù),聯(lián)系熱線:18982081108
可能是jar包版本不同導(dǎo)致路徑不對。
簡單的增刪改查功能
package?EBookDao;
import?java.sql.Connection;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.util.ArrayList;
import?java.util.List;
import?DBUtil.DBUtil;
import?DBUtil.EBook;
/*
*查詢書集信息
*?
*?*/
public?class?EBookCZ?{
public?ListEBook?find()?{
Connection?conn?=?null;
PreparedStatement?ps?=?null;
ResultSet?rs?=?null;
ListEBook?list?=?new?ArrayListEBook();
try?{
conn?=?DBUtil.getConnection();
String?sql?=?"SELECT?*?FROM?ebook";
ps?=?conn.prepareStatement(sql);
rs?=?ps.executeQuery();
while?(rs.next())?{
EBook?eBook?=?new?EBook();
eBook.setId(rs.getInt("ID"));
eBook.setBname(rs.getString("bname"));
eBook.setPrice(rs.getString("price"));
list.add(eBook);
System.out.println(rs.getInt("ID")+rs.getString("bname")+rs.getString("price"));
}
}?catch?(SQLException?sqle)?{
sqle.printStackTrace();
}?finally?{
DBUtil.close(conn,?ps,?rs);
}
return?list;
}
/*
?*通過id刪除書集信息
?*?
?*?*/
public?static?EBook?DelById(int?id)?{
Connection?conn?=?null;
PreparedStatement?ps?=?null;
ResultSet?rs?=?null;
EBook?book?=?null;
try?{
conn?=?DBUtil.getConnection();
String?sql?=?"delete?ebook?";
sql?+=?"WHERE?ID=??";
ps?=?conn.prepareStatement(sql);
ps.setInt(1,?id);
rs?=?ps.executeQuery();
if?(rs.next())?{
book?=?new?EBook();
book.setId(rs.getInt("ID"));
book.setBname(rs.getString("bname"));
book.setPrice(rs.getString("price"));
}
}?catch?(SQLException?sqle)?{
sqle.printStackTrace();
}?finally?{
DBUtil.close(conn,?ps,?rs);
}
return?book;
}
}
package?EBookServlet;
import?java.io.IOException;
import?java.io.PrintWriter;
import?java.util.List;
import?javax.servlet.ServletException;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?DBUtil.EBook;
import?EBookDao.EBookCZ;
public?class?EBookServlet?extends?HttpServlet?{
/**
?*?
?*/
private?static?final?long?serialVersionUID?=?1L;
/**
?*?Constructor?of?the?object.
?*/
public?EBookServlet()?{
super();
}
/**
?*?Destruction?of?the?servlet.?br
?*/
public?void?destroy()?{
super.destroy();?//?Just?puts?"destroy"?string?in?log
//?Put?your?code?here
}
/**
?*?The?doGet?method?of?the?servlet.?br
?*?
?*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?get.
?*?
?*?@param?request
?*????????????the?request?send?by?the?client?to?the?server
?*?@param?response
?*????????????the?response?send?by?the?server?to?the?client
?*?@throws?ServletException
?*?????????????if?an?error?occurred
?*?@throws?IOException
?*?????????????if?an?error?occurred
?*/
public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
this.doPost(request,?response);
}
/**
?*?The?doPost?method?of?the?servlet.?br
?*?
?*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to
?*?post.
?*?
?*?@param?request
?*????????????the?request?send?by?the?client?to?the?server
?*?@param?response
?*????????????the?response?send?by?the?server?to?the?client
?*?@throws?ServletException
?*?????????????if?an?error?occurred
?*?@throws?IOException
?*?????????????if?an?error?occurred
?*/
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
response.setContentType("text/html;charset=utf-8");
PrintWriter?out?=?response.getWriter();
String?method?=?request.getParameter("method");
String?url?=?"";
EBookCZ?bookCZ=new?EBookCZ();
?if?(method?!=?null??method.equals("find"))?{
?System.out.println("find做完了");
??url?=?"EBookMNG.jsp";
??ListEBook?list?=?bookCZ.find();
??request.setAttribute("list",?list);
}?else?if?(method?!=?null??method.equals("findById"))?{
?System.out.println("findbyid做完了");
??url?=?"OK.jsp";
??int?id?=?Integer.parseInt(request.getParameter("id"));
??EBook?book?=?EBookCZ.DelById(id);
??request.setAttribute("book",?book);
}
request.getRequestDispatcher(url).forward(request,?response);
out.flush();
out.close();
}
/**
?*?Initialization?of?the?servlet.?br
?*?
?*?@throws?ServletException
?*?????????????if?an?error?occurs
?*/
public?void?init()?throws?ServletException?{
//?Put?your?code?here
}
}
明顯是路徑?jīng)]找到,你調(diào)試下程序,在java代碼加下斷點(diǎn),看看有沒有進(jìn)斷點(diǎn)。
連接數(shù)據(jù)庫的典型步驟
第一步:加載正確的數(shù)據(jù)庫驅(qū)動(dòng)程序
第二步,定義所要連接數(shù)據(jù)庫的地址
String ODBCURL=“jdbc:odbc:dbName”;
String oracleURL= "jdbc:oracle:thin:@host:port:dbName“;
String mysqlURL= "jdbc:mysql:// host:port/dbName“;
String sqlURL=“jdbc:microsoft:sqlserver://host:1433;DatabaseName=dbName”;
第三步,建立與數(shù)據(jù)庫的連接
DriverManager.getConnection(oracleURL,
username,
password);
第四步,創(chuàng)建語句對象
Statement statement = connection.createStatement();
第五步,聲明SQL語句,并將該語句通過Statement對象提交給服務(wù)器進(jìn)行執(zhí)行。
Stringsql= “SELECT * FROM ONTACT_TABLE”;
ResultSet resultSet=statement.executeQuery(sql);
該語句將sql命令提交給數(shù)據(jù)庫服務(wù)器進(jìn)行執(zhí)行,并將執(zhí)行結(jié)果存儲在ResultSet對象中進(jìn)行執(zhí)行。
Statement接口主要有如下三個(gè)方法:
1. ResultSet executeQuery(String sql)
用于執(zhí)行查詢語句,返回 ResultSet結(jié)果集(一個(gè)二維表)
2. int executeUpdate(String sql)
用于執(zhí)行添加、刪除或修改操作,返回被更新記錄的條數(shù)
3. boolean execute(String sql)
執(zhí)行參數(shù)部分的SQL語句;
當(dāng)SQL語句的執(zhí)行結(jié)果是一個(gè)ResultSet結(jié)果集時(shí),本方法返回true;并可以通過Statement?getResultSet()方法得到返回的結(jié)果集
當(dāng)SQL語句執(zhí)行后沒有返回的結(jié)果集時(shí),該方法返回false
第六步:對查詢結(jié)果進(jìn)行分析
while(resultSet.next()){
name =resultSet.getString(1);
phone =resultSet.getString(2);
System.out.println(name+","+phone);
}
第七步,關(guān)閉打開的資源
resultSet.close();
statement.close();
connection.close();
將打開的資源關(guān)閉。
注意:資源關(guān)閉的順序和打開的順序恰恰相反。