servlet部分,可能用得到的复用的代码:
1、dopost设置字符
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
2、SQL语句:
增加:
String SQL_ADD="INSERT INTO book(bookid,bookname,bookauthor,pressname,availablenum) VALUE(?,?,?,?,?);";
preparedStatement=connection.prepareStatement(SQL_ADD);
connection.setAutoCommit(false);
preparedStatement.setString(1,bookid);
preparedStatement.setString(2,bookname);
preparedStatement.setString(3,bookauthor);
preparedStatement.setString(4,pressname);
preparedStatement.setString(5,availablenum);
int flag=preparedStatement.executeUpdate();
connection.commit();
删除:
String SQL_DELETE="DELETE FROM book where bookid=?";
preparedStatement=connection.prepareStatement(SQL_DELETE);
preparedStatement.setString(1, bookid);
int flag=preparedStatement.executeUpdate();
if(flag>0)
{
System.out.println("成功删除"+flag+"条图书信息!");
}else {
System.out.println("遇到问题,删除图书信息失败!");
}
修改:
SQL="UPDATE book SET availablenum=? WHERE bookid=?;";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1,strnum);
preparedStatement.setString(2, bookid);
查询:
1、查询全部:
String SQL_SELECTALL = "SELECT *FROM book";
preparedStatement = connection.prepareStatement(SQL_SELECTALL);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
String bookid=resultSet.getString("bookid");
String bookname=resultSet.getString("bookname");
String bookauthor=resultSet.getString("bookauthor");
String pressname=resultSet.getString("pressname");
String availablenum=resultSet.getString("availablenum");
newbook=new NewBook(bookid,bookname,bookauthor,pressname,availablenum);
list.add(newbook);
}
2、条件查询(模糊查询)
String SQL="SELECT * FROM book where bookname LIKE ?";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1, "%"+bookname+"%");
connection.commit();
resultSet = preparedStatement.executeQuery();
前台jsp页面
1、使用jstl标签
2、默认界面
前台调用servlet:
删除
${pageContext.request.contextPath}/
3、验证(servlet传一个message)
String message = (String)request.getAttribute("message");
if(message!=null){
%>