jsp处理不能插入数据库,急急,各位大虾,谢谢,尤其曹晓钢(100分)

  • 主题发起人 主题发起人 212
  • 开始时间 开始时间
2

212

Unregistered / Unconfirmed
GUEST, unregistred user!
我的处理是jsp初使页面调用add.jsp,add.jsp调用两个javabean.分别是连接数据库和插入数据库
javabean,执行后报错如下:
Error: 500
Location: /zx/jsp/add.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPC:/tomcat/work/localhost_8080%2Fzx/_0002fjsp_0002fzx_00031_0002ejspzx1_jsp_0.java:74: Can't make static reference to method void insertRecord(java.lang.String, java.lang.String) in class testbean.ExecSql.
ExecSql.insertRecord(testno,testname);
^
代码如下:
初使页面default.jsp:
<html>
<head>
<title>正在测试</title>
</head>
<body bgcolor="white">
<font size = 5 color="#0000FF">
<form type=POST action=zx1.jsp>
<br>
请增加或删除:
<br>
编号:
<input type=text name=userid size=10>
<br>
姓名:
<INPUT TYPE=text name=username size=10>
<br> <br>
<INPUT TYPE=submit name="submit" value="增加">
<INPUT TYPE=reset name="reset" value="取消">
</form>
</FONT>
</body>
</html>
处理增加add.jsp

<%@ page contentType="text/html;
charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="testbean.*"%>
<jsp:userBean id="exec" class="testbean.ExecSql" scop="page">
<%
String testno;
String testname;
testno=(String)request.getParameter("userid");
testname=(String)request.getParameter("username");
ExecSql.insertRecord(testno,testname);
/*response.sendRedirect("zx1.jsp");*/
%>
连接处理javabean

package testbean;
import java.sql.*;
public class DBConnection
{
private String DBConnect="jdbc:odbc:jsptest";
private String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String DBUser="zx";
private String DBPwd="zx";
private ResultSet rs=null;
private Connection Conn=null;
public DBConnection()
{
}
public String DBConnect()
{
String strExc="";
try
{
Class.forName(DBDriver);
Conn=DriverManager.getConnection(DBConnect,DBUser,DBPwd);
}
catch(ClassNotFoundException e)
{
strExc=e.toString();
}
catch(SQLException e)
{
strExc=e.toString();
}
return(strExc);
}
public void setDBConnect(String location )
{
DBConnect=location;
}
public void setDBDriver(String driver)
{
DBDriver=driver;
}
public void setConn(Connection conn)
{
Conn=conn;
}
public String getDBConnection()
{
return(DBConnect);
}
public String getDBDriver()
{
return(DBDriver);
}
public Connection getConn()
{
return(Conn);
}
public ResultSet getRs()
{
return(rs);
}
}
增加处理javabean
package testbean;
import java.sql.*;
import java.io.*;
public class ExecSql
{
private Connection conn=null;
private DBConnection dbConn=null;
private int concurType=ResultSet.CONCUR_READ_ONLY;
private int scrollType=ResultSet.TYPE_FORWARD_ONLY;
public ExecSql()
{
}
public ExecSql(DBConnection dbConn)
{
this.dbConn=dbConn;
}
private void createConnection()
{
if (conn==null)
{
if (dbConn==null)
{
dbConn=new DBConnection();
dbConn.DBConnect();
}
conn=dbConn.getConn();
}
}
public void insertRecord(String testid, String testname)
{
String sql;
byte[] temp={1};
sql="insert into zx.zx value('testid ',' testname')";
try
{
temp=sql.getBytes("iso-8859-1");
}
catch(UnsupportedEncodingException e)
{
}
String sqlCStr=new String(temp);
try
{
if (conn==null)
{
createConnection();
}
if (conn==null)
return;
else
{
Statement s=conn.createStatement(scrollType,concurType);
s.executeUpdate(sqlCStr);
}
}
catch (SQLException e)
{
}
}
}

麻烦各位大虾了,我很急[[blue][/blue]
 
处理增加add.jsp,如下:
其中:
不能使用:ExecSql.insertRecord(testno,testname);
而应该用: exec.insertRecord(testno,testname);
因为ExecSql对应的是类,而exec才是对应的实例。

<%@ page contentType="text/html;
charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="testbean.*"%>
<jsp:userBean id="exec" class="testbean.ExecSql" scop="page">
<%
String testno;
String testname;
testno=(String)request.getParameter("userid");
testname=(String)request.getParameter("username");
ExecSql.insertRecord(testno,testname);
/*response.sendRedirect("zx1.jsp");*/
%>
 
agree with hotsoup.
还有,你应该可以自己看出错误的。Tomcat的错误给出挺详细的。
org.apache.jasper.JasperException:
Unable to compile class for JSPC:/tomcat/work/localhost_8080%2Fzx/_0002fjsp_0002fzx_00031_0002ejspzx1_jsp_0.java:74: Can't make static reference to method void insertRecord(java.lang.String, java.lang.String) in class testbean.ExecSql.
ExecSql.insertRecord(testno,testname);
错误的地方何错误描述都指出了。
你上面那个文件名错误也是一样,编译器已经给出了详细的错误信息。
在遇到问题的时候希望能好好多想一想,这样才更有帮助。

 
要不把insertRecord定义为static。
 
多试试,不要怕错,做类的引用时,你最好打成包,作为包来引用。
 
我的类编译没有错,我在作表插入操作时,不用javabean可以成功,但调用javabean,就出错
,是否调用连接javabean时,还需要别的操作。各位给看看上面两个javabean是否有逻辑错误
看看add.jsp调用是否有问题?
谢谢!!!
 
又有一个问题,快来那分
我做了一个从数据库中提前数据加入到下拉列表框的简单jsp总提示出错:
原码如下:帮忙看看:
<%@ page language=java contentType="text/html;charset=gbk"%>
<%@ page contentType="text/html;
charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="dbconnection" class="testbean.DBConnection" scope="page"/>
<html>
<!--Copyright (c) 1999 雄震软件有限公司研发2部-->
<head>
<title>正在测试</title>
</head>
<body bgcolor="white">
<font size = 5 color="#0000FF">
<form method="POST" action="insert2.jsp" name="test">
<br>
请增加或删除:
<br>
编号:
<input type="text" name="userid" size=10>
<br>
姓名:
<INPUT TYPE="text" name="username" size=10>
<br>
<%
String sql2;
ResultSet rs;
sql2="select * from zx.zx";
rs=dbconnection.executeQuery(sql2);
%>

<select>
<%while(rs.next())%>
<option> <%=rs.getString("testname")%> </option>
</select>


<br>
<INPUT TYPE="submit" name="submit" value="增加">
<INPUT TYPE="reset" name="reset" value="取消">
</form>
</FONT>
</body>
</html>
数据库连接javabean没问题:如下:
/*
* Author: zx
* Created: 10/03/2001 09:36:06
* Modified: 10/03/2001 09:36:06
*/
package testbean;
import java.sql.*;
public class DBConnection
{
private String DBLocation="jdbc:odbc:jsptest";
private String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String DBUser="zx";
private String DBPwd="zx";
private Connection conn=null;
private ResultSet rs=null;
public DBConnection()
{
}
public ResultSet executeQuery(String sql)
{
if (conn==null)
{
DBConnect();
}
if (conn==null)
rs=null;
else
{
try
{
Statement s=conn.createStatement();
rs=s.executeQuery(sql);
}
catch (SQLException e)
{
}
}
return(rs);
}
public DBConnection(String DBLocation,String DBDriver)
{
this.DBDriver=DBDriver;
this.DBLocation=DBLocation;
}
public void setDBDriver(String DBDriver)
{
this.DBDriver=DBDriver;
}
public void setDBConnect(String DBLocation)
{
this.DBLocation=DBLocation;
}
public void setDBUser(String DBUser)
{
this.DBUser=DBUser;
}
public void setDBPwd(String DBPwd)
{
this.DBPwd=DBPwd;
}
public void DBConnect()
{
try
{
Class.forName(DBDriver);
conn=DriverManager.getConnection(DBLocation,DBUser,DBPwd);
}
catch(ClassNotFoundException e)
{
}
catch(SQLException e)
{
}
}
public Connection getconnection()
{
if (conn==null)
{
DBConnect();
}
return conn;
}
/*public void setDBConnect(String location )
{
DBConnect=location;
}
public void setDBDriver(String driver)
{
DBDriver=driver;
}
public void setConn(Connection conn)
{
Conn=conn;
}
public String getDBConnection()
{
return(DBConnect);
}
public String getDBDriver()
{
return(DBDriver);
}
public Connection getConn()
{
return(Conn);
}
public ResultSet getRs()
{
return(rs);
}*/
}
提示错误如下:
无效光标 提示odbc管理驱动什么的
但我将循环的数据可以显示在页面上,就不能加入下拉列表框,帮帮忙?
 
getString("")必须按顺序拿
select aaa, bbb ...
getString("aaa")
getString("bbb")
 
〉但我将循环的数据可以显示在页面上,就不能加入下拉列表框,帮帮忙?
En?你是说这里面的数据已经可以取出来了?
 
问题已经解决了,多谢各位,尤其曹晓钢
 
多人接受答案了。
 
后退
顶部