JoyiStar的AJAX WebShop虽然是很经典的AJAX IDE,可是文档和示例却比较少,我们是用Java做供销链管理系统的,使用的数据库是mysql,WebShop的示例是用的access,搞不懂他们为什么用这个微软的垃圾数据库,用mysql多好呀,例子倒是很容易跑起来,只要打开projects目录下面的工程,有Java的示例,C#的和VB的,.NET的我用不上,也没有仔细看,打开java_example工程,有个index.htm,打开后,从菜单上启动web server,然后RUN就OK了,例子倒是做得很漂亮,XP 风格的,不知道能不能换肤,有空再研究下,先看看我最关心的数据库操作,找到了simple.htm,打开后似乎是一个单表及数据表格、输入控件的功能,看了下,原来有个DataSet的控件,是用来连接后台数据的,应该是DataSource属性设置了Java类名,因为我在左边工程树里找到了demo包和Simple类:
b; DarT 1=780) window.open('');" src="" onload="javascript:if(this.width>'780')this.width='780';setTimeout('if(document.getElementById(\'\').height>\'700\')document.getElementById(\'\').height=\'700\';',500);" border=0>
WSFbE3ZbL 2=780) window.open('');" src="" onload="javascript:if(this.width>'780')this.width='780';setTimeout('if(document.getElementById(\'\').height>\'700\')document.getElementById(\'\').height=\'700\';',500);" border=0>
&@ tLu]Cx 可是当我打开这个文件,却发现只有这么几行代码,似乎这玩意封装了数据库访问,看方法的名字query_XML是取数据,update_XML是保存数据,是覆盖BusinessObjectBean的接口:
Th~a|c4Z import joyistar.eip.db.*;
F @\)1@= import joyistar.eip.bs.*;
Z%A16c`M import joyistar.eip.vo.*;
r 18WV}g 6aL#kM;Q public class Simple extends BusinessObjectBean {
I %r?\i private String dbName = "demo";
5^(LwNf6 private String SQL = "select * from product";
g2z*oi%> private String tableName = "product";
T|BVY(wKUm #a|MhxvHS public Simple() {
L?;/\ !On} }
8@Z|SD@O g ;Tzq / public joyistar.eip.vo.XmlRequest query_XML(joyistar.eip.vo.XmlRequest xmlRequest) throws Exception {
/D;06 oOle xmlRequest.SetDatabase(dbName);
A {il792 xmlRequest.SetSQL(SQL);
R oiPe\ return super.query_XML(xmlRequest);
yK6h20f5 }
Sk?:Zr8Xe .] 0>,9f a public joyistar.eip.vo.XmlPost update_XML(joyistar.eip.vo.XmlPost xmlPost) throws Exception {
Y={W;X| xmlPost.SetDatabase(dbName);
WvaZ^A2W xmlPost.SetTableName(tableName);
U-`zeqX% return super.update_XML(xmlPost);
<r5BIc& }
.w#(F5W-Mt .;8\_;'e" }
]ZDt 我找了半天,才找到有个dbconfig.xml文件,看名字就知道是配置数据库连接了,可是打开一看,却只有access,oracle,sqlserver的例子,试着改成mysql的连接串,却报连接失败的错误,看来它是不支持mysql了。
i/75YK X"c]`;f# Z4:QCM T!
~936D1ih1
demo RHD3%{]\^
sun.jdbc.odbc.JdbcOdbcDriver s%!
jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=database/demo.mdb i}s4QZ[
b`= pFk
ef:]yZy~
20 +DqgKcZ%0
joyistar.eip.db.AccessSQL 2Jq0t[Q
20 mkDfA3{x|N
2 ~[}|:fMd
10 cw{;piM(
777@VCSJ2
qYzUG3
d"ae{;~t5
X"tS3?UH_
&"H! B/4
dblog.log 5)9>F'qXP,
10M ?y,!,X 0
|X?0l1;QW
#(]9+CL/GC 正在失望的时候,看到demo包里有个Simple_ResultSet的例子,打开看了看,哈哈,原来除了他们封装了数据库连接,也提供了标准的JDBC连接方式,这个例子是这样的:
9R,gMr package demo;
Qx|m^RqU
/** Ewl `
* Title:
8tME
* Description:
H G A=R
* Copyright: Copyright (c)
{$4ij|U
* Company:
ggvj6hf
* @author not attributable zK"Y:v
* @version 1.0 48[ 6>g]
*/ kNm|p3@
r0?$*?_
import joyistar.eip.bs.*; K7@6{i
import joyistar.eip.vo.*; _! Y{5
import java.sql.*; znjuy "
D-~OZcGT
public class Simple_ResultSet extends BusinessObjectBean { 7aL ~JN
String drivers="sun.jdbc.odbc.JdbcOdbcDriver"; n/T:?4*\
String username=""; $u<\J*z|3M
String password=""; L64!M
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=demo.mdb"; M-g{h%7$
)v;|#
public Simple_ResultSet() { F
} ni|')YA&l[
r C,ys(
public joyistar.eip.vo.XmlRequest query_XML(joyistar.eip.vo.XmlRequest xmlRequest) throws Exception { `o" =N:mi
Connection conn = null; 7L~71Ce(F<
String sql = ""; W_b(]JH l
java.sql.Statement stmt = null; m^i5$_HOd
ResultSet rs = null; \wm.OU?&a\
ResultSetMetaData rsmd= null; fy+np6j
int rowCount; 6o ?xC
int maxRow; P5jN"% Z
int recNo; f+vLcXx]I
try{ "{WB&h:hX
System.setProperty("jdbc.drivers",drivers); "Q7Ft.UP
conn=DriverManager.getConnection(url,username,password); bI {a[e0K
maxRow=xmlRequest.MaxRows; ~rX=@f t
recNo=xmlRequest.RecNo; 6#&% 1 [@
sql="select * from PRODUCT"; "Nez'
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); .J+!m1Cn
rs = stmt.executeQuery(sql); aEAz/zZ
rsmd=rs.getMetaData(); TI#8!?ZF
rs.last(); WLfP(igT
rowCount=rs.getRow(); Z>|:s_P<\t
xmlRequest.SetRecordCount(rowCount); ~5@M,
for(int i=1;i<=rsmd.getColumnCount();i++) { 3mmBx O6O<
if(rsmd.getColumnTypeName(i).equals("STRING")) { WH0 XRwlt
xmlRequest.AddField(rsmd.getColumnName(i),rsmd.getColumnTypeName(i),rsmd.getPrecision(i)); nx|X{a%!
} nk)}Jz%>9
else { Q'a ';/b
xmlRequest.AddField(rsmd.getColumnName(i),rsmd.getColumnTypeName(i)); =s#@NgkF
} ]J/BS{j7
} LHF4Om}
for(int i=1;i<=maxRow;i++) { zw%
if(recNo+i>rowCount) { GLUg\U*m
break; ]$7" 2D
} Z~G2EEyQ o
else { ZK.rt)
rs.absolute(recNo+i); wQ}edq "K5
xmlRequest.Append(); EAST?
for(int j=1;j<=rsmd.getColumnCount();j++) { w;"V&@}mH
xmlRequest.SetValue(rsmd.getColumnName(j), rs.getString(j)); DB}4"4)^
} jQ<3<k8
} 'yOJ1`s(,H
} +7 t-^{t
} catch(Exception e) { e][ bD
xmlRequest.Error=e.getMessage(); ]:`'T,Uc
} finally { X-##cVw,L
if(rs != null) f\&ZSwT
rs.close(); 9n&Sjd4
if(stmt != null) f"N1w,ARF[
stmt.close(); 'W{J"?w`
if(conn != null) u`:B#4
conn.close(); FAC ^E1=
} ]/A: T{
return xmlRequest; "Vy/m3P\
} ojO yl]@
O=9VJdv
X@}} 3tA
\Od4iMu
public joyistar.eip.vo.XmlPost update_XML(joyistar.eip.vo.XmlPost xmlPost) throws Exception { N1 5LYb
Connection conn = null; UC"2ZL[R
java.sql.Statement stmt = null; d |4*$5o
ResultSet rs = null; GkDTQ
ResultSetMetaData rsmd =null; }z`OaD
String acttype = null; //action type k%5;~`mZ
XmlAction action = null; //an action o6D|[ 2:
String sql = null; 2N6nQ
String where = null; `BYch-
try { '{F rUlB
System.setProperty("jdbc.drivers",drivers); !.a $sc m
conn=DriverManager.getConnection(url,username,password); }h63G <4
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); O0ce"Sy1
conn.setAutoCommit(false); {3e tV(b|5
for(int i = 0;i Q-(a|Dj
action = xmlPost.GetAction(i); \?wa^-#3
acttype = action.ActionType; 1!pjwr
if(acttype.equals("old")) { //where acton, then update or delete action after it; akY2%YH
where = " where "; %q\-UH@Fn
where = where+"PRODUCT_ID='"+action.getField("PRODUCT_ID").Value+"'"; kR},8
} >U qs\c o
if(acttype.equals("update")){ 7V2`#LE|
String set = " set "; 3ar!{a#n
for(int j=0;j 9-Cr85Gm if(action.getField(j).Name.equals("PRODUCT_PRICE")) {
^S)1IW\u set = set+action.getField(j).Name+"="+action.getField(j).Value;
V37f'el) }
5"S2 [ s else {
|qbqf. set = set+action.getField(j).Name+"='"+action.getField(j).Value+"'";
.]B^[# }
9:U:&^V if(j
&9n3$nW
set = set+ "," ; omWwKNPg
} &q]Yk ]BJ
} HpGS}3f2
sql="update product"+set+where; {`h&q uQ
stmt.executeUpdate(sql); ?FF -N>
} a44q*{T
if(acttype.equals("delete")){ Q=5Xbe
where = " where "; @w +;Q oQ
where = where+action.getField("PRODUCT_ID").Name+"='"+action.getField("PRODUCT_ID").Value+"'"; B 0G`V#q%n
sql = "delete from product"+where; p08g.HzP
stmt.executeUpdate(sql); 5NM1*d^/h
} mNos/i%_h
if(acttype.equals("insert")){ PwToe5iP
String field = new String(); eTv_4F++
String value = new String(); CQqM[`)w!
for(int j=0;j YyE4Qjx
field = field+action.getField(j).Name; @$V9^c+BL]
if(action.getField(j).Name.equals("PRODUCT_PRICE")) { q7f0B3gdn
value = value+action.getField(j).Value; x0{M
} (8G
else { ;\PLR4ZLS
value=value+"'"+action.getField(j).Value+"'"; |A1 &?
} X2Ge"d~o^
if(j yDvzMIN1
field = field+ "," ; d0?Tq
value = value+","; a~o&E ?
} =]-@D;nh
} QU~VY
sql="insert into product ("+field+") values ("+value+")"; } GSFXXvr
stmt.executeUpdate(sql); .N5S5]\
} 9t-/(
} si;V&2o(g
conn.commit(); 8v^8;U\#
} catch(Exception e) { k[~9?u*t
xmlPost.Error=e.getMessage(); ?ccPeu
} finally { MEhm}=k6
if(rs != null) u 2^R1Ap-
rs.close(); R p4ZiR^p+
if(stmt != null) !h S[8 )v
stmt.close(); asevn#g
if(conn != null) \eS\ p
conn.close(); nzadFEnoD
} [QaITUIHv
return xmlPost; )hEfK2>ID
} sH{cukp)
#,7a.+(t
} #LmP1yh
*,?:7N
同样query_XML是取数据,update_XML是保存数据,是覆盖BusinessObjectBean的接口,只不过这个例子使用了标准JDBC连接数据库,虽然代码复杂好多,不过仔细一看,不难理解,我马上做了个测试,修改了以下代码,同时把jdbc-odbc连接改成了我的jdbc连接方式的代码: FV%/f1n
aD||x:
String drivers="org.gjt.mm.mysql.Driver"; ^\6u&o$>]
String username="root"; tVT<J(
String password="root"; _ ^>i"fQal
String url=" jdbc:mysql://localhost:3306/mysql1"; 4:o6Er'L
String sql=”select * from stores”; qr z,D
4z&2>RBN
搞完这些,用Build编译,然后运行页面,哈哈,居然成功了。Update好像有点复杂,偶还在研究中,似乎是有几个标记操作类型的old,insert,update,delete,然后根据这些拼装SQL,等我试好了发上来。