jsp使用javaBean登录oracle问题
找个javabean是在书上抄的,我改了下
package useBean;
import java.sql.*;
public class JavaBean {
private static Connection con;
private Statement state;
private ResultSet rs;
/*数据库创建***************************/
public void init (Connection con)
{
try
{
state=this.con.createStatement();
}catch(Exception e)
{
System.out.println("init");
System.out.println(e.getMessage());
}
}
/*关闭数据库***************************/
public void close()
{
try
{
if(rs!=null)rs.close();
}catch (Exception e)
{
System.out.println("rs");
System.out.println(e.getMessage());
}
try
{
if(state!=null)state.close();
}catch (Exception e)
{
System.out.println("state");
System.out.println(e.getMessage());
}
try
{
if(con!=null)con.close();
}catch (Exception e)
{
System.out.println("close");
System.out.println(e.getMessage());
}
}
/*查询数据*****************************/
public int select (String sql)
{
int k=-10;
this.init(this.getCon());
try
{
k=0;
rs=state.executeQuery(sql);
if (rs.next())
{
k=k+1;
}
}catch(Exception e)
{
k=-1;
System.out.println("Select");
System.out.println(e.getMessage());
this.close();
}
this.close();
return k;
}
/*插入、查询、修改*/
public int update(String sql)
{
int k=-10;
this.init(JavaBean.getCon());
try
{
k=0;
k=state.executeUpdate(sql);
}catch(Exception e){
k=-1;
System.out.println("update");
System.out.println(e.getMessage());
}
this.close();
return k;
}
public static synchronized Connection getCon()
{
try
{ //以下是我改的
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@134.100.31.65:1521:HJTEST";
String user="hjtest";
String password="hjtest";
con= DriverManager.getConnection(url,user,password);
//到此
}catch(Exception e){
System.out.println("getCon");
System.out.println(e.getMessage());
}
return con;
}
public String chStr(String str)
{
if (str==null)
{
str="";
}
else
{
try
{
str=(new String(str.getBytes("iso-8859-1"),"GB2312")).trim();
}catch(Exception e)
{
System.out.println("chStr");
System.out.println(e.getMessage());
}
}
return str;
}
}
------------------------------------------------------------
这是我的index.htm
<form name="form1" method="post" action="main.jsp">
电话号码 <input type="text" name="phonenumber" />
密码 <input type="password" name="phonepass" />
</form>
(简化了)
-----------------------------------------------------------
这是我的main函数
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*"%>
<jsp:useBean id="login" class ="useBean.JavaBean" scope="request"/>
<%
String phonenumber =(String)request.getParameter("phonenumber");
String phonepass =(String)request.getParameter("phonepass");
phonenumber=login.chStr(phonenumber);
phonepass=login.chStr(phonepass);
System.out.println(phonenumber);
System.out.println(phonepass);
String sqls="select * from michelle.v_inquery_huafei where ACC_NBR='"+phonenumber+"' and STRINGFIELD1='"+phonepass+"'";//这是我的sql函数
int temp=-20;
if (phonenumber!=""||phonepass!="")
{
temp=0;
temp=login.select(sqls);
if (temp>0)
{
session.setAttribute("name",phonenumber);
response.sendRedirect("ok.jsp");
}
else
{
session.setAttribute("error","请检察您的输入");
response.sendRedirect("error.jsp");
}
}
else
{
session.setAttribute("error","请检察您的输入");
response.sendRedirect("error.jsp");
}
login.close();
%>
但是不行啊,请帮我看看
这是我的软件配置情况
JDK:j2sdk-1_4_2_09-windows-i586-p.exe
TomCat:Tomcat5.0.28
Eclipse:Eclipse3.1 final
MyEclipse:MyEclipse 4.0GA
问题点数:50、回复次数:5Top
1 楼yyouyou(一塌)回复于 2005-10-25 14:21:00 得分 25
不能连接数据库?
你的WEB-INF/lib下面拷了驱动没?Top
2 楼advancejar(金谜)回复于 2005-10-25 14:25:39 得分 0
考了
我使用
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@134.100.31.65:1521:HJTEST";
String user="hjtest";
String password="hjtest";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from michelle.v_inquery_huafei where ACC_NBR='2222222'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
是可以查询的,我觉得是javabean编错了
请帮忙看看Top
3 楼yyouyou(一塌)回复于 2005-10-25 15:46:22 得分 25
把package useBean;语句删掉,然后将编译的class文件放到WEB-INF\classes目录下?
Top
4 楼advancejar(金谜)回复于 2005-11-08 16:48:14 得分 0
不行的Top
5 楼li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)回复于 2005-11-08 17:10:14 得分 0
但是不行啊,请帮我看看
==============================
错误信息是什么?Top




