回 帖 发 新 帖 刷新版面

主题:jsp插入数据库的问题


jsp1:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>


<body bgcolor="#ffffff">


<form name="form1" method="post" action="jsp2">
<table width="400" border="0" cellspacing="1" cellpadding="1">
<tr>
    <td width="147" align="right">PRODID:</td>
    <td width="246" valign="top"><input type="text" name="PRODID" size="16" maxlength="25" ></td><br>
</tr>
<tr>
    <td width="147" align="right"> PRODUCTNAME  :</td>
    <td width="246" ><input type="text" name=" PRODUCTNAME  " size="16" maxlength="25" ></td><br>
</tr>
<tr>
    <td width="147" align="right"> PRODUCTPRICE:</td>
    <td width="246" ><input type="text" name=" PRODUCTPRICE" size="16" maxlength="25" ></td><br>
</tr>
<tr>
    <td width="147" align="right"> DISCOUNT:</td>
    <td width="246" ><input type="text" name=" DISCOUNT" size="16" maxlength="25" ></td><br>
</tr>
<tr>

<td><input type="submit" name="ok" value="Ok"></td>
<td><input type="reset" name="cancel" value="Cancel"></td>
</tr>


</table>
</form>
</body>
</html>



jsp2:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>
jsp2
</title>
</head>
<jsp:useBean id="handle" scope="session" class="untitled3.Bean1"/>


<body bgcolor="#ffffff">
<h1>
<%

    String i1=request.getParameter("PRODID");
    out.print(i1);
    String i2=request.getParameter("PRODUCTNAME");
    String i3=request.getParameter("PRODUCTPRICE");
    String i4=request.getParameter("DISCOUNT");
    //String query="insert into product values('"+i1+"','"+i2+"','"+i3+"','"+i4+"')";
    //handle.executeUpdate(query);
%>

</h1>
</body>
</html>





Bean1:
package untitled3;

import java.awt.*;
import javax.swing.JPanel;
import java.sql.*;

public class Bean1 extends JPanel implements java.io.Serializable{
  BorderLayout borderLayout1 = new BorderLayout();
  Connection conn=null;
  Statement stmt=null;
  ResultSet rs=null;

  public void getConnection(){
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger");
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
  }
  public void closeConnection(){
    try{
      if(conn!=null){
        conn.close();
      }
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
  }
  public ResultSet executeQuery(String sql){
    rs=null;
    try{
      getConnection();
      if(conn!=null){
        stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);

      }
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
    return rs;
  }

   public int executeUpdate(String sql)    {
     int result=0;
     try{
       getConnection();
       stmt.executeUpdate(sql);
     }
     catch(SQLException ee){
       System.err.println(ee.getMessage());
     }
     return result;
   }

}

回复列表 (共2个回复)

沙发

这么长的代码贴上来,鬼才有心情给你看,好歹您写两句注释啊,真够呛

板凳

伙计,错就错在了//handle.executeUpdate(query);句话上

你写的BEAN里边的修改方法是有返回类型的

你应该写成 int intexecute = handle.executeUpdate(query); 这样估计就没问题了

我来回复

您尚未登录,请登录后再回复。点此登录或注册