我现在是用jdbc+servlet+jsp这种方式做的一个对表中记录的增删改查操作,遇到了些问题 还请大侠们帮我解决解决,我的增删改查方法是这样的:
  public class SearchTycInfo {
public Vector getTyc() 
{
String sql = "select id,fl,tyc from tyc";
Vector tycInfo = Tool.CreateObjectsBySql(sql,
"com.dic.supersearch.bean.TYCInfo");
return tycInfo;
}

public HashMap getTycInfo(){
String sql = "select id,fl,tyc from tyc";
Vector tycInfo = Tool.CreateObjectsBySql(sql,
"com.dic.supersearch.bean.TYCInfo");
HashMap obj = new HashMap();
for(Iterator it = tycInfo.iterator();it.hasNext();)
{
TYCInfo tycObj = (TYCInfo) it.next();
String fl = tycObj.getFl();
ArrayList flObj = (ArrayList) obj.get(fl);
if(flObj == null)
{
flObj = new ArrayList();
flObj.add(tycObj);
obj.put(fl, flObj);
}else{
flObj.add(tycObj);
}
}
return obj;
}

public TYCInfo selectTyc(String fl)
{
String sql = "select * from tyc t where t.tyc="+fl;
DBconOper dbo = new DBconOper();
Connection conn = null;
TYCInfo tycI = null;
List list = null;
try {
conn = dbo.getConnection();
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
tycI = new TYCInfo();
while(rs.next()){
tycI.setId(rs.getString("id"));
tycI.setFl(rs.getString("fl"));
tycI.setTyc(rs.getString("tyc"));
//list.add(tycI);
}

} catch (Exception e) {
e.printStackTrace();
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { e.printStackTrace();
}
}
}
return tycI;
}

public boolean deleteTyc(TYCInfo ti)
{
boolean b = false;
DBconOper dbo = new DBconOper();
Connection conn = null;
String sql = null;
String t = "";
try {
t = ti.getTyc();
sql = "delete from tyc t where t.tyc="+t;
conn = dbo.getConnection();
Statement stm = conn.createStatement();
int rs = stm.executeUpdate(sql);
if(rs>0){
b = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { e.printStackTrace();
}
}
}
return b;
}

public boolean insertTyc(TYCInfo ti)
{
boolean b = false;
DBconOper dbo = new DBconOper();
Connection conn = null;
PreparedStatement stmt = null;
String sql = null;
try {
sql = "insert into tyc t values(?,?,?)";
conn = dbo.getConnection();
stmt =conn.prepareStatement(sql);
stmt.setString(1, ti.getId());
stmt.setString(2, ti.getFl());
stmt.setString(3, ti.getTyc());
int rs = stmt.executeUpdate();
if(rs!=0){
b = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { e.printStackTrace();
}
}
}
return b;
}

public boolean updateTyc(TYCInfo ti)
{
boolean b = false;
DBconOper dbo = new DBconOper();
Connection conn = null;
PreparedStatement stmt = null;
String sql = null;
try {
sql = "update tyc t set t.tyc="+ti.getTyc();
conn = dbo.getConnection();
stmt =conn.prepareStatement(sql);
stmt.setString(3, ti.getTyc());
int rs = stmt.executeUpdate();
if(rs!=0){
b = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { e.printStackTrace();
}
}
}
return b;

}我在servlet里该怎么做 才能在页面上进行增删改查的操作呢?
我在servlet里是这样的:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String tyccondition = request.getParameter("tyccondition");
String deletetyc = request.getParameter("deletetyc");
String updatetyc = request.getParameter("updatetyc");
String inserttyc = request.getParameter("inserttyc");
String selecttyc = request.getParameter("selecttyc");
System.out.print("tyccondition=="+tyccondition);

TYCInfo ti = new TYCInfo();

if(tyccondition==null||tyccondition.equals( "")){
System.out.println("没输入条件");
request.getRequestDispatcher("page/tycInfo.jsp").forward(request, response);
//return ;
}

SearchTycInfo searchTycInfo = new SearchTycInfo();
List list = new ArrayList();
if(selecttyc!=null){ 
TYCInfo tycinfo = searchTycInfo.selectTyc(tyccondition);
System.out.print("tycinfo=="+tycinfo);
list.add(tycinfo);
}
if(updatetyc!=null){
//boolean bl=searchTycInfo.updateTyc(ti);
}
if(deletetyc!=null){

}
if(inserttyc!=null){

}
request.setAttribute("QueryRs", list);
request.getRequestDispatcher("page/tycInfo.jsp").forward(request, response);
}
这样对吗?然后在页面上有增 删 改查4个按钮和一个文本框 该怎么做哦?就是输入一个 点击查的时候回去查询表,显示出来,然后能进行删 改 这样的操作,有哪位大哥知道的帮我下 谢谢!或有做过的能发我个例子吗?要jdbc+servlet这种的 

解决方案 »

  1.   

    业务代码去掉吧,呵呵
    太多了!代码你跑一下,呵呵!
    jdbc+servlet和jsp的思路是一样的!
      

  2.   

    我在Servlet里怎么取我在页面中点击的是哪个按钮呢?我页面里有增 删 改 查4个按钮 点击后要在servlet里取到该怎么取呢 我的油箱是:[email protected] 有例子的发我下好吗 或教教我哦 等待...
      

  3.   

    功能没实现啊 就是想请教大家怎么实现的呢?就是我现在还不知道如何在页面上展现出来啊,只是先把增删改查的功能算法写好了.页面上该怎么做啊?我把页面中那段发出来 大家帮我看看吧
    <form action="/supersearch/searchTycInfo" name="searchTycInfo" method="post">
                  <table width="50" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td width="23"><img src="images/tl1.gif" width="23" height="22"></td>
                      <td background="images/tl2.gif" style="padding-top: 4px">在此输入查找内容并回车</td>
                      <td width="33"><img src="images/tl3.gif" width="33" height="22"></td>
                    </tr>
                  </table> 
                 <input type="text" id="tyccondition" name="tyccondition" style="width:600" class="input" value="">
                 <!--   <select name="select" style="width:98%">
                  </select> -->
                   <table width="50" border="0" cellpadding="0" cellspacing="0">
                    <tr> 
                      <td height="10"></td>
                    </tr>
                  </table> 
                  <table width="98%" border="0" cellpadding="0" cellspacing="0" class="fonts">
                    <tr> 
                      <td width="23"><img src="images/tl1.gif" width="23" height="22"></td>
                      <td background="images/tl2.gif" style="padding-top: 4px">分类</td>
                      
                      <td width="2" background="images/tl2.gif"><img src="images/01.JPG" width="2" height="22"></td>
                      <td width="60" background="images/tl2.gif"> 
                        <input type="button" id="deletetyc" name="deletetyc" value="删 除" class="anniu5" onclick="query();"/>
                      </td>
                      <td width="60" background="images/tl2.gif"> 
                        <input type="button" id="updatetyc" name="updatetyc" value="修 改" class="anniu5" onclick="query();"/>
                      </td>
                      <td width="60" background="images/tl2.gif"> 
                        <input type="button" id="inserttyc" name="inserttyc" value="增 加" class="anniu5" onclick="query();"/>
                      </td>
                      <td width="60" background="images/tl2.gif"> 
                        <input type="button" id="selecttyc" name="selecttyc" value="查 询" class="anniu5" onclick="query();"/>
                      </td>
                      <td width="33"><img src="images/tl3.gif" width="33" height="22"></td>
                    </tr>
                    <%
                            List li = (List)request.getAttribute("QueryRs");
                        
                          for(Iterator it = li.iterator();it.hasNext();)
                        {
                            System.out.print("li==="+li);
                            TYCInfo tycI  =(TYCInfo) it.next();
                            String fl = tycI.getFl();
                   %>
                     <tr> 
                      <td width="23"><img src="images/tl1.gif" width="23" height="22"></td>
                      <td background="images/tl2.gif" style="padding-top: 4px"><%=fl %></td>
                     </tr>
                      <%
                                //List lis = (List)li.get();
                                //for(Iterator obj= lis.iterator();obj.hasNext();)
                                //{
                                //    TYCInfo tycInfo =(TYCInfo)obj.next();
                                    String id = tycI.getId();
                                    String tyc = tycI.getTyc();
                             %>
                            <tr class="jas"> 
                              <td width="40" align="right"><%=id %></td>
                              <td><%=tyc%></td>
                            </tr>
                    <%}%>
                  </table>
                  </form>
    页面主要的就在这了 然后就是点击提交一个form 执行一个servlet 上面发的有这个servlet 在页面上该怎么展现我增 删 改查的操作哦!!!
      

  4.   

    查 就是一个显示列表的页面 
    增 就是一个添加form字段的页面
    改 和增差不多 不过要先取得数据
    删 页面一个按钮就可以 给一个主键值 
    然后都是提交到后台的servlet里 根据method不同实现不同业务
      

  5.   

    方法一:给你的form里添加一个字段,专门用来做判断,在action中做判断
    方法二:在你的页面中再写一段js代码,在点击不同的按钮后,调用一个js方法,然后在js中把你的提交给DispatchAction的不同方法来处理,或者你要是用的是普通的action的话,你可以提交给不同的Action来处理。