主题: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;
}
}