求一个jsp+javabean的例子

lonelycrab 2005-08-09 06:01:52
求一个jsp+javabean操作sqlserver的例子
用javabean写数据库连接和对数据库进行select insert delete update操作,然后jsp调用传参.
求大侠们给一个标准的一定要能用的例子阿,我写的都崩溃好几天了,select能用,剩下的一用jsp传参就不行了,郁闷......
...全文
291 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
大米无糖 2005-08-16
  • 打赏
  • 举报
回复
按下来是我用来处理的BEAN,

/**
* Title BIOS manager system
* @author: rundy.mi
* Company: mitac pca te
* Copyright: Copyright (c) 2005
* @version 1.0
* BIOS control system
*/
package te.beans;

import java.sql.*;
import java.util.*;
import te.db.Te_conn;
import te.beans.*;

public class ControlBios extends Bios {
String error;
Connection con = Te_conn.con; /** initialize the con */
public ControlBios(){};

// insert a now bios version
public void insertBios() throws SQLException, Exception {
if (con != null) {
try {
PreparedStatement updatebios;
updatebios = con.prepareStatement(
"insert into te_bios(Bmode, BKVersion,BSVersion,BKChksum00,BSChksum,BKChksumff,BVgaVersion,BVgaChksum,BpcbPn,BmodeId,BMnStatus,BupdateDate) values(?, ?,?,?,?,?,?,?,?,?,?,?);");
updatebios.setString(1, Bmode);
updatebios.setString(2, BKVersion);
updatebios.setString(3, BSVersion);
updatebios.setString(4, BKChksum00);
updatebios.setString(5, BSChksum);
updatebios.setString(6, BKChksumff);
updatebios.setString(7, BVgaVersion);
updatebios.setString(8, BVgaChksum);
updatebios.setString(9, BpcbPn);
updatebios.setInt(10, BmodeId);
updatebios.setInt(11, BMnStatus);
updatebios.setString(12, BupdateDate);

updatebios.execute();
} catch (SQLException sqle) {
error = "SQLException: insert bios failed, possible pramater wrong";
throw new SQLException(error);
}
} else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
}
//over add bios
public ResultSet allBios() throws SQLException, Exception {
ResultSet rs = null;
try {
String QString = ("SELECT a.* ,b.* from te_bios as a, te_bios_mode as b where BActive=1 and a.BmodeId=b.modeid ORDER by a.BupdateDate desc;");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(QString);
} catch (SQLException sqle) {
throw new SQLException(error);
} catch (Exception e) {
error = "An excepton occured whinle retrieving lines.";
throw new Exception(error);
}
return rs;
}
// all line over

// oneBios retrun one bios rest on Bid
public ResultSet OneBios() throws SQLException, Exception {
ResultSet rs = null;
try {
String QString = ("select * from te_bios where bid="+Bid+";");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(QString);
} catch (SQLException sqle) {
throw new SQLException(error);
} catch (Exception e) {
error = "An excepton occured whinle retrieving Bios.";
throw new Exception(error);
}
return rs;
}

// one Bios over

//search bios
/*
parameter modeName --the result modename will lice modeName

*/
public ResultSet SearchBios(String modeName) throws SQLException, Exception {
ResultSet rs = null;
try {
String QString = ("SELECT a.* ,b.* from te_bios as a, te_bios_mode as b where a.BmodeId=b.modeid and a.BActive=1 and b.modeName like '%"+modeName+"%' ORDER by a.Bid desc;");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(QString);
} catch (SQLException sqle) {
throw new SQLException(error);
} catch (Exception e) {
error = "An excepton occured whinle retrieving Bios.";
throw new Exception(error);
}
return rs;
}


// search over
// all record with the same mode

public ResultSet allVersion(String modeId) throws SQLException, Exception {
ResultSet rs = null;
try {
String QString = ("SELECT a.* ,b.* from te_bios as a, te_bios_mode as b where a.BmodeId=b.modeid and a.BmodeId="+Integer.parseInt(modeId)+" order by Bid desc;");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(QString);
} catch (SQLException sqle) {
throw new SQLException(error);
} catch (Exception e) {
error = "An excepton occured whinle retrieving all version.";
throw new Exception(error);
}
return rs;
}


//update Bios
public void UpdateBios() throws SQLException, Exception {
if ( con!=null) {
try {

PreparedStatement updatebios;
updatebios = con.prepareStatement(
"update te_bios set BSVersion= ?, BSChksum=? ,BKVersion=?,BKChksum00=?,BKChksumff=? ,BVgaVersion=?,BVgaChksum=?,BpcbPn=?,BmodeId=?,BMnStatus=?,BupdateDate=? WHERE Bid=?;");
updatebios.setString(1,BSVersion);
updatebios.setString(2, BSChksum);
updatebios.setString(3, BKVersion);
updatebios.setString(4, BKChksum00);
updatebios.setString(5, BKChksumff);
updatebios.setString(6, BVgaVersion);
updatebios.setString(7, BVgaChksum);
updatebios.setString(8, BpcbPn);
updatebios.setInt(9, BmodeId);
updatebios.setInt(10, BMnStatus);
updatebios.setString(11, BupdateDate);
updatebios.setInt(12,Bid );
updatebios.execute();

} catch (SQLException sqle) {
error="SQLException: update failed, possible pramater wrong";
} catch (Exception e) {
error = "Exception: Connection to database was lost.";

}

}else {
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}

//end if
}//end update bios
lonelycrab 2005-08-13
  • 打赏
  • 举报
回复
感觉都晕了。。。。。怎么离我的问题越来越远....
ccm1980 2005-08-13
  • 打赏
  • 举报
回复
可以看看我在下面题目中发的贴子:
servlet中<jsp:setProperty name="Bean" property="*"/> 如何实现?
maseccc 2005-08-13
  • 打赏
  • 举报
回复
mark
light1437 2005-08-13
  • 打赏
  • 举报
回复
.....楼上的强。。
Javcs 2005-08-12
  • 打赏
  • 举报
回复
哦。不好意思

没看清问题
Javcs 2005-08-12
  • 打赏
  • 举报
回复
一个简单实现的,主要是体现原理:
首先
JAVA文件放在\WEB-INF\classes\com下并生成响应class文件

//hell.java
package com;

public class Hello{


private String str = new String("Hello JavaBean");

public String getStr(){
return str;
}
public void setStr(String str){
if(str!= null){
this.str = str;
}
}

}


web.xml文件放在\WEB-INF\下
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<!--<error-page>
<error-code>404</error-code>
<location>/pageNotFound.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/internalError.html</location>
</error-page>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/NumberFormatException.html</location>
</error-page>
-->
</web-app>
然后就是相应JSP文件:

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<jsp:useBean id="gg" scope = "page" class = "com.Hello" />

<jsp:setProperty name="gg" property="*"/>

<body>
<h1>
A JSP & BEAN SAMPLE</h1>
<form method = "post">
<br>Enter new Value: <input name ="str">
<br>

<input type = "submit" name = "sumbmit" value = "sumbmit">
<input type = "reset" value = "reset" >
<br><br>
VALUE OF BEAN PROPERTY ISdfdsfasdfsa: <jsp:getProperty name="gg" property="str" />


</form>
</body>
</html>
lonelycrab 2005-08-12
  • 打赏
  • 举报
回复
xielinhecrab@163.com 谢谢了
layueer 2005-08-12
  • 打赏
  • 举报
回复
楼主留email 我发给你
lonelycrab 2005-08-12
  • 打赏
  • 举报
回复
miruby(水手)
继续阿

我的

数据库联接
Db.java


package web;

import java.sql.*;
public class Db {
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr="jdbc:microsoft:sqlserver://192.168.200.103:1433;DatabaseName=base";
String user="sa";
String password="789456123";
Connection connQuery=null;
Connection connUpdate=null;
ResultSet rs=null;

public Db(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("connect():"+e.getMessage());
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
connQuery=DriverManager.getConnection(sConnStr,user,password);
Statement stmt=connQuery.createStatement();
//sql=getWord(sql); 1.0注释掉
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
try {
connUpdate = DriverManager.getConnection(sConnStr,user,password);
Statement stmt = connUpdate.createStatement();
sql=getWord(sql);
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeUpdate: " + ex.getMessage());
System.err.println("aq.executeUpadatestrSQL: " + sql);
}
}
public String getWord(String word){
try{
byte b[]=word.getBytes("ISO-8859-1");
word=new String(b);
return word;

}
catch(Exception e){
return word;
}


}

}


执行得时候通过sql语句传参数
大米无糖 2005-08-09
  • 打赏
  • 举报
回复
我有一个JSP+MYSQL的。需要可以给你。
先看一点主要的:
/**
* Title BIOS manager system
* @author: rundy.mi
* Company: mitac pca te
* Copyright: Copyright (c) 2005
* @version 1.0
* BIOS list system
*/
package te.beans;
import java.util.*;


public class Bios {

public String Bmode,BSVersion,BSChksum,BKVersion,BKChksum00,BKChksumff,BVgaVersion,BVgaChksum,BpcbPn;SBid,SBAction,SBMnStatus;
public int Bid,BActive,BMnStatus;


public Bios(){};

/*
get & set property
**/
public String getBmode(){ return Bmode; }
public void setBmode(String Bmode){ this.Bmode=Bmode; }

public String getBSVersion(){ return BSVersion; }
public void setBSVersion(String BSVersion){ this.BSVersion=BSVersion; }

public String getBSChksum(){ return BSChksum; }
public void setBSChksum(String BSChksum){ this.BSChksum=BSChksum; }

public String getBKVersion(){ return BKVersion; }
public void setBKVersion(String BKVersion){ this.BKVersion=BKVersion; }

public String getBKChksum00(){ return BKChksum00; }
public void setBKChksum00(String BKChksum00){ this.BKChksum00=BKChksum00; }

public String getBKChksumff(){ return BKChksumff; }
public void setBKChksumff(String BKChksumff){ this.BKChksum00=BKChksumff; }

public String getBVgaVersion(){ return BVgaVersion; }
public void setBVgaVersion(String BVgaVersion){ this.BVgaVersion=BVgaVersion; }

public String getBVgaChksum(){ return BVgaChksum; }
public void setBVgaChksum(String BVgaChksum){ this.BVgaChksum=BVgaChksum; }

public String getBpcbPn(){ return BpcbPn; }
public void setBpcbPn(String BpcbPn){ this.BpcbPn=BpcbPn; }

public int getBid(){ return Bid; }
public void setBid(String Bid) {
SBid=Bid;
this.Bid=Integer.parseInt(SBid);}

public int getBActive(){ return BActive; }
public void setBActive(String SBactive) {
this.SBActive=SBActive;
this.BActive=Integer.parseInt(SBActive);}

public int getBMnStatus(){ return SBMnStatus; }
public void setBMnStatus(String SBMnStatus) {
this.SBMnStatus=SBMnStatus;
this.BMnStatus=Integer.parseInt(SBMnStatus);}


}

调用设置如下

<jsp:useBean id="Bios" scope="session" class="te.beans.ControlBios"/>
<jsp:setProperty name="Bios" property="*"/>


ControlBios类继承BIOS, 代码现在不在 ,到公司再给你。
做一个测试页,看传参过去没,如果没过去,更改FORM 中INPUT 的 NAME 第一个字母的大小写,我以前也常碰到传不过去的情况 ,试试。

菜手,正在学习中
欢迎交流

MAIL: skysailor@126.com
roypan2008 2005-08-09
  • 打赏
  • 举报
回复
javabean代码写出来看看再说嘛
AlexPark 2005-08-09
  • 打赏
  • 举报
回复
呵呵,我顶
zhang_kan 2005-08-09
  • 打赏
  • 举报
回复
你把你的javabean代码写出来看看。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧