实现该功能的代码如下package com.dao;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 com.domain.Book;
import com.util.DBCon;public class BookDao {
private Connection conn;
//查询所有图书信息
public List<Book> findAllBooks() {
conn = DBCon.getConnection();
String listSQL = "select * from books";
List <Book>list = new ArrayList();
try {
PreparedStatement psmt = conn.prepareStatement(listSQL);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getLong(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
book.setAuthor(rs.getString(4));
book.setBookConcern(rs.getString(5));
book.setCounts(rs.getInt(6));
list.add(book);
}
conn.commit();
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
} public boolean saveUser(Book book) throws Exception {
conn = DBCon.getConnection();
/*
System.out.println(book.getName());
System.out.println(book.getPrice());
System.out.println(book.getAuthor());
System.out.println(book.getBookConcern());
System.out.println(book.getCounts());
*/
//String listSQL = "INSERT INTO books VALUES(?,?,?,?,?)";
String listSQL="insert into books(name,price,author,bookConcern,counts)values(?,?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(listSQL);
try {
pstmt.setString(1, book.getName());
pstmt.setDouble(2, book.getPrice());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getBookConcern());
pstmt.setInt(5, book.getCounts());
pstmt.executeUpdate();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
} finally {
conn.close();
}
return false;
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.domain.Book;
import com.util.DBCon;public class BookDao {
private Connection conn;
//查询所有图书信息
public List<Book> findAllBooks() {
conn = DBCon.getConnection();
String listSQL = "select * from books";
List <Book>list = new ArrayList();
try {
PreparedStatement psmt = conn.prepareStatement(listSQL);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getLong(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
book.setAuthor(rs.getString(4));
book.setBookConcern(rs.getString(5));
book.setCounts(rs.getInt(6));
list.add(book);
}
conn.commit();
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
} public boolean saveUser(Book book) throws Exception {
conn = DBCon.getConnection();
/*
System.out.println(book.getName());
System.out.println(book.getPrice());
System.out.println(book.getAuthor());
System.out.println(book.getBookConcern());
System.out.println(book.getCounts());
*/
//String listSQL = "INSERT INTO books VALUES(?,?,?,?,?)";
String listSQL="insert into books(name,price,author,bookConcern,counts)values(?,?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(listSQL);
try {
pstmt.setString(1, book.getName());
pstmt.setDouble(2, book.getPrice());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getBookConcern());
pstmt.setInt(5, book.getCounts());
pstmt.executeUpdate();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
} finally {
conn.close();
}
return false;
}
}
这里这个路径下没有此savaBook.jsp
第二个问题可以通过这里调出来
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>bookinfo</servlet-name>
<servlet-class>com.controller.BookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bookinfo</servlet-name>
<url-pattern>/bookshop.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<html>
<head>
<title>保存图书信息</title>
</head>
<body>
<form action="bookshop.html?arg=2" method="post">
<table>
<tr>
<td>
图书名称:
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
图书价格:
</td>
<td>
<input type="text" name="price" />
</td>
</tr>
<tr>
<td>
图书作者:
</td>
<td>
<input type="text" name="author" />
</td>
</tr>
<tr>
<td>
出版社:
</td>
<td>
<input type="text" name="concern" />
</td>
</tr>
<tr>
<td>
存放数量:
</td>
<td>
<input type="text" name="counts" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>