|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你总不能说你写框架吧,那无疑会加大工作量,现在大多企业采取的是折中的办法,就是改别人写好的框架,可要改框架,前提是你对这个框架足够的了解,这就更难了。js 基于JDBC有哪些数据库通用会见办法?
1.通用数据库Bean计划
本实例中对数据库毗连和实行SQL语句等通用数据库操纵举行了封装,经由过程完成DBConnBean和DBQueryBean两个JavaBean来完成上述功效。个中DBConnBean卖力Java使用程序和数据库的毗连;DBQueryBean供应了一组实行尺度SQL的功效,能够完成尺度SQL完成的一切功效。其功效代码分离以下所示:
①DBConnBean.Java的源代码以下所示:
packagedbaccess;
importJava.sql.*;
importJava.util.*;
importJava.io.*;
publicclassDBConnBean
implementsSerializable{
privateStringDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
privateStringDBHost="127.0.0.1";
privateStringDBName="demo";
privateStringconp="jdbc:odbc:db_demo";
privateStringusername="";
privateStringpassword="";
privatebooleanxdebug=true;
publicConnectioncon=null;
publicStringsql=null;
Statementstmt=null;
publicResultSetresult=null;
privateintaffectedRows=0;
publicDBConnBean()
{
xdebug=true;
con=null;
sql=null;
}
publicConnectionConnect()
throwsException
{
Stringmsg=null;
try
{
Class.forName(DBDriver).newInstance();
}
catch(Exceptione)
{
msg="加载数据库驱动失利";
if(xdebug)msg+="(驱动"+DBDriver+")";
thrownewException(msg);
}
try
{
StringconStr=conp;
con=DriverManager.getConnection(conStr,username,password);
}
catch(SQLExceptione)
{
msg="!!数据库毗连失利";
if(xdebug)
{
msg+="(毛病信息="+e.getMessage()+"SQL形态值="+e.getSQLState()+"毛病代码="+e.getErrorCode()+")";
}
thrownewException(msg);
}
returncon;
}
protectedvoidfinalize()
throwsThrowable
{
super.finalize();
if(stmt!=null)stmt.close();
if(result!=null)result.close();
}
//比来一次对数据库查询受影响的行数
publicintgetAffectedRows()
{
returnaffectedRows;
}
publicConnectiongetCon()
{
returncon;
}
publicStringgetConp()
{
returnconp;
}
publicStringgetDBDriver()
{
returnDBDriver;
}
publicStringgetDBName()
{
returnDBName;
}
publicbooleangetDebug()
{
returnxdebug;
}
publicStringgetPassword()
{
returnpassword;
}
publicResultSetgetResult()
{
returnresult;
}
publicStringgetSql()
{
returnsql;
}
publicStringgetUsername()
{
returnusername;
}
publicvoidover()
throwsThrowable
{
finalize();
}
publicResultSetquery()
throwsException
{
result=null;
affectedRows=0;
if(con==null)
Connect();
if(stmt==null)
stmt=con.createStatement();
if(sql.substring(0,6).equalsIgnoreCase("select"))
{
result=stmt.executeQuery(sql);
}
else
{
affectedRows=stmt.executeUpdate(sql);
}
returnresult;
}
publicResultSetquery(Strings)
throwsException
{
sql=s;
returnquery();
}
publicvoidsetDBDriver(Strings)
{
DBDriver=s;
}
publicvoidsetDebug(booleanb)
{
xdebug=b;
}
publicvoidsetgetConp(Strings)
{
conp=s;
}
publicvoidsetgetDBName(Strings)
{
DBName=s;
}
publicvoidsetgetUsername(Strings)
{
username=s;
}
publicvoidsetPassword(Strings)
{
password=s;
}
publicvoidsetSql(Strings)
{
sql=s;
}
}
②DBQueryBean.Java的源代码以下所示:
packagedbaccess;
importJava.sql.*;
importJava.util.*;
importJava.io.*;
importJava.lang.reflect.*;
publicclassDBQueryBean
implementsSerializable
{
DBConnBeandbc;
Stringsql=null;
introwcount=0;
intcolcount=0;
//intlimitcount=0;
Vectorresult=null;
publicString_WATCH="";
publicDBQueryBean()
{
dbc=newDBConnBean();
try{
dbc.Connect();
}catch(Exceptione){
handleException(e);
}
}
protectedvoidfinalize()
throwsThrowable
{
super.finalize();
if(dbc!=null)dbc.over();
if(result!=null)result.removeAllElements();
}
publicStringget(introw,intcol)
{
if(result==null||row>=result.size())returnnull;
Stringr[]=(String[])result.elementAt(row);
if(col>=Java.lang.reflect.Array.getLength(r))returnnull;
returnr[col];
}
publicintgetAffRows(){returndbc.getAffectedRows();}
publicintgetColumncount(){
returncolcount;
}
publicString[]getRow(introw)
{
if(result==null||row>=result.size())returnnull;
return(String[])result.elementAt(row);
/*Stringret[]=newString[colcount];
Vectorr=(Vector)result.elementAt(row);
for(inti=0;i<colcount;i++)
ret[i]=(String)r.elementAt(i);
returnret;*/
}
publicintgetRowcount(){
returnrowcount;
}
publicvoidhandleException(Exceptione)
{
_WATCH=e.getMessage();
}
publicvoidinit()
{
rowcount=0;
colcount=0;
//limitcount=0;
result=null;
}
publicvoidover()
throwsThrowable
{
finalize();
}
publicintquery(Stringsql)
{
result=newVector();
intret=0;
try{
ResultSetrs=dbc.query(sql);
if(rs==null)
{
ret=dbc.getAffectedRows();
}
else
{
ResultSetMetaDatarm=rs.getMetaData();
colcount=rm.getColumnCount();
while(rs.next())
{
Stringrow[]=newString[colcount];
for(inti=0;i<colcount;i++)
row[i]=rs.getString(i+1);
result.addElement(row);
rowcount++;
}
rs.close();//toreleasetheresource.
ret=result.size();
}
}
catch(Exceptione)
{
handleException(e);
return-1;
}
returnret;
}
}
2.数据库表布局
本实例中次要呈现了三个数据库表,表名和字段分离以下所示:
企图推销表:jhcg_table
字段称号中文称号范例长度
Goods_no物品编号vchar10
Goods_name物品称号Vchar50
Amount推销数目Int
Price推销单价float
Gold币种Vchar15
Units单元Vchar10
Date工夫Date
Remark备注vchar100
库存统计表:kctj_table
字段称号中文称号范例长度
Goods_no物品编号Vchar10
Goods_name物品称号Vchar50
amount库存数目Int
Date工夫Date
remark备注Vchar100
实践推销表:sjcg_table
字段称号中文称号范例长度
Goods_no物品编号Vchar10
Goods_name物品称号Vchar50
Amount推销数目Int
PricePrice推销单价Float
Gold币种Vchar15
Units推销单元Vchar10
Date工夫Date
Remark备注vchar100
个中营业逻辑十分复杂,即依据企图推销表和库存统计表天生实践推销表。同时,对各表完成数据库的增、删、改、查等通用操纵。
3.JSP计划
①拔出操纵
完成对数据库表的纪录拔出功效,个中企图推销表的拔出主页面(insert_jhcg.htm)为:
insert_jhcg.htm将用户输出传送给demo_insert_jhcg.jsp,完成拔出操纵。改jsp文件的功效代码为:
<html>
<body>
<jsp:useBeanid="DBConn"class="dbaccess.DBConnBean"scope="page"/>
<jsp:useBeanid="DBBean"class="dbaccess.DBQueryBean"scope="page"/>
<hr>
<!--testJavaBean-->
<%
if(DBConn==null||DBBean==null){
out.println("JavaBeannotfound!");
return;
}
%>
<!--trydb_democonnection-->
<%
try{
DBConn.Connect();
}catch(Exceptione){
out.println(e.getMessage());
}
%>
<!--executesqlstatement-->
<%
StringinsGoodno=request.getParameter("ed_jhcg_no");
StringinsGoodname=request.getParameter("ed_jhcg_name");
intinsAmount=(Integer.valueOf(request.getParameter("ed_jhcg_amount"))).intValue();
floatinsPrice=(Float.valueOf(request.getParameter("ed_jhcg_price"))).floatValue();
StringinsGold=request.getParameter("ed_jhcg_gold");
StringinsUnit=request.getParameter("ed_jhcg_unit");
StringinsRemark=request.getParameter("ed_jhcg_remark");
StringsqlStatement="insertintojhcg_table(good_no,good_name,amount,
price,gold,unit,remark)values("+""+insGoodno+""+","+""+insGoodname+""+",
"+insAmount+","+insPrice+","+""+insGold+""+","+""+insUnit+""+","+""+
insRemark+""+")";
try{
DBBean.query(sqlStatement);
}catch(Exceptione){
out.println(e.getMessage());
}
%>
<ahref="demo_main.htm">Recordsinserted...Clickheretoreturn</a></p>
</body>
</html>
②查询操纵
该查询主页面次要供应对三个数据库表的前提查询功效,
query.htm将用户选择查询的数据库表和查询前提发送给demo_query.jsp,由jsp文件完成数据库查询操纵和查询了局集的前往及显现,其功效代码以下:
<html>
<body>
<%
StringsqlStatement;
StringsqlField="";
StringwhichTable="";
StringwhereClause="";
StringqueryNo="";
StringqueryName="";
%>
<jsp:useBeanid="DBConn"class="dbaccess.DBConnBean"scope="page"/>
<jsp:useBeanid="DBBean"class="dbaccess.DBQueryBean"scope="page"/>
<hr>
<!--testJavaBean-->
<%
if(DBConn==null||DBBean==null){
out.println("JavaBeannotfound!");
return;
}
%>
<!--trydb_democonnection-->
<%
try{
DBConn.Connect();
}catch(Exceptione){
out.println(e.getMessage());
}
%>
<!--preparesqlstatement-->
<%
StringqueryRequest=request.getParameter("rb_request");
//out.println("queryRequest:"+queryRequest);
StringwhichCB="";
if(queryRequest.equals("1")){
whichCB="ck_jhcg";
whichTable="jhcg_table";
queryNo=request.getParameter("ed_jhcg_no");
queryName=request.getParameter("ed_jhcg_name");
if(!queryNo.equals(""))
whereClause="wheregood_no="+""+queryNo+"";
if(!queryName.equals("")){
if(!queryNo.equals(""))
whereClause+="andgood_name="+""+queryName+"";
elsewhereClause="wheregood_name="+""+queryName+"";
}
}
if(queryRequest.equals("2")){
whichCB="ck_kctj";
whichTable="kctj_table";
queryNo=request.getParameter("ed_kctj_no");
queryName=request.getParameter("ed_kctj_name");
if(!queryNo.equals(""))
whereClause="wheregood_no="+""+queryNo+"";
if(!queryName.equals("")){
if(!queryNo.equals(""))
whereClause+="andgood_name="+""+queryName+"";
elsewhereClause="wheregood_name="+""+queryName+"";
}
}
if(queryRequest.equals("3")){
whichCB="ck_sjcg";
whichTable="sjcg_table";
queryNo=request.getParameter("ed_sjcg_no");
queryName=request.getParameter("ed_sjcg_name");
if(!queryNo.equals(""))
whereClause="wheregood_no="+""+queryNo+"";
if(!queryName.equals("")){
if(!queryNo.equals(""))
whereClause+="andgood_name="+""+queryName+"";
elsewhereClause="wheregood_name="+""+queryName+"";
}
}
String[]printTitle=request.getParameterValues(whichCB);
%>
<!--createquerysqlstatement-->
<%
sqlStatement="select";
for(inti=0;i<printTitle.length;i++){
sqlField+=printTitle[i]+",";
}
sqlStatement+=sqlField.substring(0,sqlField.length()-1)+"from"+whichTable;
if(!whereClause.equals(""))
sqlStatement+=whereClause;
%>
<!--showqueryresponse-->
<%
try{
DBBean.query(sqlStatement);
}catch(Exceptione){
out.println("DatabaseError!");
}
introws=DBBean.getRowcount();
intcols=DBBean.getColumncount();
%>
<Tablealign="center"width="80%"border=1>
<tralign=center>
<%
for(inti=0;i<printTitle.length;i++){
out.println("<td><b>");
out.println(printTitle[i]);
out.println("</b></td>");
}
%>
</tr>
<%
for(inti=0;i<rows;i++){
out.println("<tr>");
for(intj=0;j<cols;j++)
out.println("<td>"+DBBean.get(i,j)+"</td>");
out.println("</tr>");
}
%>
</Table>
<br>
<hr>
<ahref="demo_main.htm">Clickheretoreturn</a></p>
</body>
</html>
③天生实践推销表
天生数据库表是一个隐式操纵,程序依据企图推销表和库存统计表的响应字段天生实践推销表,不必要用户的任何输出,其功效代码以下(demo_create.jsp):
<%@pageimport="Java.util.*"%>
<html>
<body>
<jsp:useBeanid="DBConn"class="dbaccess.DBConnBean"scope="page"/>
<jsp:useBeanid="DBBean"class="dbaccess.DBQueryBean"scope="page"/>
<hr>
<!--testJavaBean-->
<%
if(DBConn==null||DBBean==null){
out.println("JavaBeannotfound!");
return;
}
%>
<!--trydb_democonnection-->
<%
try{
DBConn.Connect();
}catch(Exceptione){
out.println(e.getMessage());
}
%>
<!--preparesqlstatement-->
<%
intamount_jhcg,amount_kctj;
VectorupdateRs=newVector();
DBBean.query("delete*fromsjcg_table");//deletealloldrecordsinsjcg_table
DBBean.query("selectjhcg_table.good_no,jhcg_table.good_name,jhcg_table.amount,kctj_table.amount,jhcg_table.unitfromjhcg_tableleftjoinkctj_tableonkctj_table.good_no=jhcg_table.good_no");
introws=DBBean.getRowcount();
intcols=DBBean.getColumncount();
for(inti=0;i<rows;i++){
Stringrecord[]=newString[4];
record[0]=DBBean.get(i,0);
record[1]=DBBean.get(i,1);
amount_jhcg=(Integer.valueOf(DBBean.get(i,2))).intValue();
if(DBBean.get(i,3)==null)amount_kctj=0;
elseamount_kctj=(Integer.valueOf(DBBean.get(i,3))).intValue();
record[2]=Integer.toString(amount_jhcg-amount_kctj);
record[3]=DBBean.get(i,4);
updateRs.addElement(record);
}
for(inti=0;i<rows;i++){
StringinsRecord[]=(String[])updateRs.elementAt(i);
StringinsGoodno,insGoodname,insUnit,insAmount;
insGoodno=insRecord[0];
insGoodname=insRecord[1];
insAmount=insRecord[2];
insUnit=insRecord[3];
StringsqlStatement="insertintosjcg_table(good_no,good_name,amount,unit)values?quot;+""+insGoodno+""+","+""+insGoodname+""+","+insAmount+","+""+insUnit+""+")";
DBBean.query(sqlStatement);
DBBean.query("delete*fromsjcg_tablewhereamount<=0");
}
%>
<ahref="demo_main.htm">Databasecreated...Clickheretoreturn</a></p>
</body>
</html>
上述的开辟工具综合使用先容了基于Java开辟电子商务使用体系的全历程,包含使用开辟平台搭建、营业流程剖析、JavaBean封装和JSP开辟等外容,个中JSP开辟中触及到了通用SQL(查询和拔出数据库表)和游标操纵(天生实践推销表),基础能够完成任何收集数据库使用的需求。本实例基础上能够将后面先容的基于Java的电子商务开辟手艺串接起来,引导读者举行电子商务使用开辟。
没有那个大公司会傻了吧唧用.net开发大型项目,开发了,那等于自己一半的生命线被微软握着呢。而.net不行,限制在window系统,又是捆绑,鄙视微软之! |
|