Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~
naughty026





发贴: 3
于 2005-10-21 08:55 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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~




JBuilder使用技巧

话题树型展开
人气 标题 作者 字数 发贴时间
1938 救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ naughty026 4682 2005-10-21 08:55
1753 Re:救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ beyond1984 765 2005-10-21 10:55
1626 Re:救命~~~一个入库系统~~很奇惨怪的问题~MVC的~请高手帮下啊~ naughty026 122 2005-10-21 13:26

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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