一道编写Java程序的题 很急 谢谢了
已知Dbtool类,该类中有getconnection()方法可以返回和数据库相连的connection对象的实例。数据库中有图书表book,字段(id图书编号为整数型,...
已知Dbtool类,该类中有getconnection()方法可以返回和数据库相连的connection对象的实例。数据库中有图书表book,字段(id 图书编号为整数型,rale图书为、标题为文本型,price图书价格为整型)
1,编写java程序,向数据表中添加一本编号为1 书名为Java 价格是25的图书信息
2,用JSP实现向数据表中添加图书信息,写出添加图书的HTLM代码。
3,写出2中调用的JSP码
可能有不准的 如图 很急 谢谢了!!
不是我的作业 有人在考试...我对这个真的什么也不懂的 也没有搜到 谁搜到什么或者做出来的帮帮忙吧 谢谢啦 展开
1,编写java程序,向数据表中添加一本编号为1 书名为Java 价格是25的图书信息
2,用JSP实现向数据表中添加图书信息,写出添加图书的HTLM代码。
3,写出2中调用的JSP码
可能有不准的 如图 很急 谢谢了!!
不是我的作业 有人在考试...我对这个真的什么也不懂的 也没有搜到 谁搜到什么或者做出来的帮帮忙吧 谢谢啦 展开
3个回答
展开全部
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BookBean{
Dbtool db = null;
public BookBean(){
db = new Dbtool();
}
public int saveBook(Book book){
Connection con = null;
PreparedStatement ps = null;
int result = 0;
try{
con = db.getconnection();
ps = con.prepareStatement("INSERT INTO book (id,rale,price) VALUES(?,?,?)");
ps.setInt(1,book.bookId);
ps.setString(2,book.bookName);
ps.setInt(3,book.bookPrice);
ps.executeUpdate();
}finally{
if(pstmt!=null){
try{pstmt.close();}catch(SQLException e){}
}
if(con != null){
try{con.close();}catch(SQLException e){}
}
}
}
}
public class Book {
public int bookId;
public String bookName;
public int bookPrice;
}
-------------HTML
<script>
var valiadate = function(){
var bookId = document.getElementById(bookId).value;
var bookName = document.getElementById(bookName)value;
var bookPrice= document.getElementById(bookPrice).value;
if(bookId == null || bookName == null || bookPrice == null){
return false;
}
return true;
}
</script>
<form action="saveBook.jsp" onsubmit="validate();">
图书ID<input id="bookId" /><br/>
图书名称<input id="bookName" /><br/>
图书价格<input id="bookPrice" /><br/>
<input type="submit" value="保存"/>
</form>
-----------JSP
<%
int bookId = (int)request.getParameter("bookId");
String bookName = request.getParameter("bookName");
int bookPrice = (int)request.getParameter("bookPrice");
Book newBook = new Book();
newBook.bookId = bookId;
newBook.bookName = bookName;
newBook.bookPrice = bookPrice;
BookBean bookBean = new BookBean();
bookBean.saveBook(book);
%>
----------------------------
写完了,应该没什么问题. 还特地注册了个帮你回答,要请吃饭的啊.
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BookBean{
Dbtool db = null;
public BookBean(){
db = new Dbtool();
}
public int saveBook(Book book){
Connection con = null;
PreparedStatement ps = null;
int result = 0;
try{
con = db.getconnection();
ps = con.prepareStatement("INSERT INTO book (id,rale,price) VALUES(?,?,?)");
ps.setInt(1,book.bookId);
ps.setString(2,book.bookName);
ps.setInt(3,book.bookPrice);
ps.executeUpdate();
}finally{
if(pstmt!=null){
try{pstmt.close();}catch(SQLException e){}
}
if(con != null){
try{con.close();}catch(SQLException e){}
}
}
}
}
public class Book {
public int bookId;
public String bookName;
public int bookPrice;
}
-------------HTML
<script>
var valiadate = function(){
var bookId = document.getElementById(bookId).value;
var bookName = document.getElementById(bookName)value;
var bookPrice= document.getElementById(bookPrice).value;
if(bookId == null || bookName == null || bookPrice == null){
return false;
}
return true;
}
</script>
<form action="saveBook.jsp" onsubmit="validate();">
图书ID<input id="bookId" /><br/>
图书名称<input id="bookName" /><br/>
图书价格<input id="bookPrice" /><br/>
<input type="submit" value="保存"/>
</form>
-----------JSP
<%
int bookId = (int)request.getParameter("bookId");
String bookName = request.getParameter("bookName");
int bookPrice = (int)request.getParameter("bookPrice");
Book newBook = new Book();
newBook.bookId = bookId;
newBook.bookName = bookName;
newBook.bookPrice = bookPrice;
BookBean bookBean = new BookBean();
bookBean.saveBook(book);
%>
----------------------------
写完了,应该没什么问题. 还特地注册了个帮你回答,要请吃饭的啊.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询