Topic: 救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~

  Print this page

1.救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ Copy to clipboard
Posted by: naughty026
Posted on: 2005-10-21 08:55

servlet代码如下:
package InBookData;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InBookData extends HttpServlet
{
connBean cb = new connBean();
String sql1 = "select * from book_total";
ResultSet rs = cb.selectLog(sql1);
int id;
public void getData()
{
try
{
while(rs.next())
{
if(rs.last())
{
id = rs.getInt("id");
id++;
}
}
}
catch(Exception e)
{e.printStackTrace();}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException
{
getData();
String name = request.getParameter("name");
String author = request.getParameter("author");
String publish = request.getParameter("publish");

if(name!=null&&author!=null&&publish!=null)
{
cb.setId(id);
cb.setName(name);
cb.setAuthor(author);
cb.setPublish(publish);
cb.updateLog();
cb.close();
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/web/success1.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/web/fail1.jsp");
rd.forward(request, response);
}
}
}

connBean代码如下:

package InBookData;

import java.sql.*;

public class connBean
{
private String DbName = "book";
private String pass = "1456987";
private String url = "jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+"&useUnicode=true&characterEncoding=8859_1";
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
private int id = 0;
private String name = null;
private String author = null;
private String publish = null;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getPublish()
{
return publish;
}
public void setPublish(String publish)
{
this.publish = publish;
}

public void BulidCon()
{
try
{
String url="jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+ "&useUnicode=true&characterEncoding=8859_1";

Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Success loading Mysql Driver!");
con=DriverManager.getConnection(url);
stmt=con.createStatement();
System.out.println("连接成功");
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void updateLog()
{
String sql = "insert into book_total value(" + id + ",'" + name + "','" + author + "','" + publish + "')" ;
try
{
BulidCon();
stmt.executeUpdate(sql);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public ResultSet selectLog(String sql)
{
try
{
BulidCon();
rs = stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return rs;
}

public void close()
{
try
{
con.close();
stmt.close();
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}

InBookData.jsp如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>入库系统</title>
</head>
<%@include file="head.jsp"%>
<body>
<table width="760" height="307" border="0">
<tr>
<th width="754" height="303" scope="col"><form name="form1" method="post" action="/myjsp2/InBookData/InBookData">
<p align="center">
<div align="center">
<pre> </pre>
<table width="521" height="101" border="0">
<tr>
<th height="31" scope="col"><div align="center">
<div align="left">
<pre> 图书名称: <input name="name" type="text" id="name">
</pre>
</div>
</div></th>
</tr>
<tr>
<td height="31"><pre> 作者: <input name="author" type="text" id="author">
</pre></td>
</tr>
<tr>
<td height="31"><pre> 出版社: <input name="publish" type="text" id="publish">
</pre></td>
</tr>
</table>
</p>
</div>
<table width="200" border="0">
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="入库">
</div></td>
</tr>
</table>
</form></th>
</tr>
</table>
</body>
<%@include file="foot.htm"%>
</html>
问题是:入库时只能插入一个记录~~想插入第二个记录~!就要重启TOMCAT才能插入~
插入第三个记录也是要这样`~请问大虾们~~~~帮帮我啊~1有什么方法可以改正我的问题啊?谢谢啊~!感动ING~

2.Re:救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ [Re: naughty026] Copy to clipboard
Posted by: beyond1984
Posted on: 2005-10-21 10:55

我也是刚学servlet和jsp,有点心得和你分享一下吧
首先你把要处理的逻辑放在一个Bean里面,也就是MVC中的ActionModel,他就是一个行为,例如你要doPost等等。起个名字叫DoSqlAction.java吧,继承于HttpServlet.
doPost方法中可以检查request中的操作请求,是"addNew","deleteOne"等等。包含BookDao的一个实例。

然后建一个数据库操作BookDao.java,里面包括你要执行的具体操作。add(Book book); delete(Book book);等等。返回的数据存在ArrayList或者HashMap中,方便返回给页面进行显示,同时也做到了一定的封装。

然后建立一个DataModel,叫做Book.java.专门存放书籍有关的信息,书名,作者等。

建立JSP页面MVC的View,可以包含1个或多个隐藏域,赋值"addNew","deleteOne"等等,用来支持数据库操作的类型。

调用关系:JSP页面发送请求给/dosqlaction(即上面DoSqlAction),dosqlaction接收到Post请求后,通过request.getParameter();获得一些有关书籍的信息,存入Book对象,并确认操作的类型。将Book的对象传入BookDao,并调用相应的操作方法。

当然这个解决方法也不是最好的,不过可以将页面,逻辑和数据对象分开。但是看不到MVC的Controller?不是么?那是因为将Controller和actionModel合并在一起了。你可以在把Controller单独划出来。专门用作JSP页面的转发。通过actionModel来处理。

试试吧,我也是这么玩的。

3.Re:救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ [Re: naughty026] Copy to clipboard
Posted by: naughty026
Posted on: 2005-10-21 13:26

可能楼上的朋友有点理解错误了~我是要将图书的信息加入到Mysql中~不是放到
hashmap里~~~~~啊````````~

正如我的项目中`1
只是入了一个记录之后~就入不了第二个记录~!唉~!这是什么问题啊~帮帮我啦~1大虾们~~


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923